如何仅在应用终止时接收 FCM 通知,如果应用在后台运行则无法接收 FCM 通知



我正在处理FCM在我的扑动应用程序,我只想收到一个预定的通知,如果我的应用程序被终止,我不想收到它,如果应用程序现在在后台工作。

这是我的代码,当我在测试时,如果我的应用程序在后台或被终止,我会收到此通知。

Future<void> background_handler(RemoteMessage message) async {
await Firebase.initializeApp();
}
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(options: FirebaseOptions(apiKey: 
"apikey", appId: "appid", messagingSenderId: "senderid", projectId: "projid"));

//handle msg in terminate case
RemoteMessage? message = await FirebaseMessaging.instance.getInitialMessage();
if(message != null){
PushNotification notification = new PushNotification(title: 
message.notification?.title?? "Title",
body: message.notification?.body?? "Body", dataTitle: message.data["title"], 
dataBody: message.data["body"] );
}
FirebaseMessaging.onBackgroundMessage(background_handler);
runApp(const MyApp());
}

我如何改变这段代码,使应用程序只接收此通知,如果它被终止,或者它不可能分离背景和终止的情况?

我不认为这是可能的,因为onBackgroundMessage处理消息时,应用程序在后台和终止状态。此外,Flutter只能检测应用程序是否在后台,而不能检测它何时被终止。

最新更新