繁体   English   中英

我如何仅在应用程序终止时接收 FCM 通知,如果应用程序在后台运行则不接收它

[英]How I can receive FCM notification only when app is terminated and doesn't receive it if app is working in background

我在我的 flutter 应用程序中处理 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在应用程序处于后台和终止时处理消息 state。此外,Flutter 只能检测应用程序是否在后台,但不能检测它何时终止

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM