【问题标题】:How to use Firebase Cloud Messaging如何使用 Firebase 云消息传递
【发布时间】:2021-06-30 05:58:03
【问题描述】:

我找不到任何关于新版本的文档。版本 7 和 6 有大量文档,而版本 9 几乎不存在。不仅我,大多数人都找不到。 我只是想向后台发送简单的通知。如果有人分享有关新版本的文档,我会非常高兴。 还是我应该使用旧版本?

【问题讨论】:

    标签: firebase flutter firebase-cloud-messaging


    【解决方案1】:

    我创建了一个示例应用程序,展示了如何在版本 9 上使用 FCM 实现通知系统。

    You can refer to this project,如果您需要更多信息,我会编辑这个答案!

    【讨论】:

    • 非常感谢。我试图添加你的代码,但我无法管理。我对 Flutter 有基本的了解。感觉很复杂,就像一部诺兰的电影。我想我应该只尝试本地通知。旧版本只发送带有几行代码的通知。这太难了,哈哈。再次感谢。
    • 嗨,您的示例仅显示在版本 9 中没有任何问题的 onbackground 处理程序,但 FirebaseMessaging.onMessage.listen 呢?看起来新插件版本 9 有并且未解决问题 github.com/FirebaseExtended/flutterfire/issues/4261
    【解决方案2】:

    我想您知道如何将 firebase 添加到您的应用程序中。如果没有:https://firebase.google.com/docs/flutter/setup?platform=android

    将firebase添加到应用程序后,这就是我所做的:

    void main() async {
      WidgetsFlutterBinding.ensureInitialized();
      await Firebase.initializeApp();
    
      runApp(
        MaterialApp(
          debugShowCheckedModeBanner: false,
          routes: {
          '/': (context) =>  AppStarter(),
          '/message': (context) => NotificationDetails(),
          },
         ),
        );
       }
    
    
    
    class AppStarter extends StatefulWidget{
       @override
       _AppStarterState createState() => _AppStarterState();
      }
    
    
    
    class _AppStarterState extends State<AppStarter>
       {
    
         FirebaseMessaging messaging = FirebaseMessaging.instance;
    
        Future<void> showMeMyToken()
        async {
          var myToken = await messaging.getToken();
          print("My Token is: " + myToken.toString());
        }
    
    
        @override
        void initState() {
           super.initState();
    
           showMeMyToken();
    
          FirebaseMessaging.instance.getInitialMessage().then((value) {
           if(value != null)
            {
              Navigator.push(context,
              MaterialPageRoute(
                  builder: (context){return NotificationDetails();},
              settings: RouteSettings(arguments: value.data,),
             ),
            );
           }
         });
    
    
         FirebaseMessaging.onMessage.listen((RemoteMessage message) {
    
    
            if (message.notification != null) {
               print('Message on Foreground: ${message.notification}');
                  }
             });
    
    
          FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message)
           {
             Navigator.push(
               context,
               MaterialPageRoute(
                   builder: (context) {return NotificationDetails();},
                   settings: RouteSettings(arguments: message.data,)
              ),
            );
         });
    
         FirebaseMessaging.onBackgroundMessage(firebaseMessagingBackgroundHandler);
       }
    
    
       @override
        Widget build(BuildContext context) {
    
          return MaterialApp(
            debugShowCheckedModeBanner: false,
            title: 'Just a Test',
            
            home: AppHome(),
           );
          }
       }
    
    
    
    
       Future<void> firebaseMessagingBackgroundHandler(RemoteMessage message) async {
         await Firebase.initializeApp();
    
          print("Handling a background message :-): ${message.data}");
          //Here you can do what you want with the message :-)
         }
    

    【讨论】:

    • 非常感谢。但我不明白 [NotificationDetails(),] 部分。我做了你写的,令牌到了,但我无法运行它。如果没有 Navigator.push,我如何使用它?
    • NotificationDetails() 只是我为显示通知而创建的一个类。你可以在那里调用你自己的类。你不能运行什么?
    • 我终于成功了,但仍然有问题。通知在后台时出现,但在应用程序打开时不会出现。这个错误:Accessing hidden method Landroid/os/WorkSource;->add(I)Z (greylist,test-api, reflection, allowed)
    • FirebaseMessaging.onMessage 中有哪些功能?在 onMessage 中,您可以编写应用程序在前台收到通知时应执行的操作。
    • 其实我不需要 onMessage 方法。背景通知对我来说已经足够了。
    猜你喜欢
    • 2023-02-24
    • 1970-01-01
    • 1970-01-01
    • 2019-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多