【问题标题】:Flutter FirebaseMessaging setupFlutter FirebaseMessaging 设置
【发布时间】:2020-04-02 09:00:21
【问题描述】:

在 Flutter firebase_messaging 6.0.13 的example app_PushMessagingExampleState 类的initState() 中,首先调用_firebaseMessaging.configure,然后调用_firebaseMessaging.requestNotificationPermissions

但是,在 FirebaseMessaging 类的 firebase_messaging 6.0.13 API 参考中,它声明的第一件事是:

您的应用应先调用 requestNotificationPermissions,然后再调用 使用 configure 为传入消息注册处理程序。

哪个是正确的?
我猜 API 参考是正确的,示例应用可以改进吗?

【问题讨论】:

    标签: flutter firebase-cloud-messaging


    【解决方案1】:

    从技术上讲,这并不重要,但我先将其设置为请求权限,然后再进行配置。作为一个工作应用的例子:

      configureFcm() {
        if (!isConfigured) {
          if (Platform.isIOS) {
            _fcm.onIosSettingsRegistered.listen((IosNotificationSettings data) {
            });
    
            _fcm.requestNotificationPermissions(IosNotificationSettings(sound: true, badge: true, alert: true));
          }
          _fcm.configure(
            // in foreground
            onMessage: (Map<String, dynamic> message) async {
              _handleNotification(message);
            },
            // onBackgroundMessage: (Map<String, dynamic> message) async {
            //   print('on background message $message');
            // },
            // in background
            onResume: (Map<String, dynamic> message) async {
              _handleNotification(message);
            },
            // terminated
            onLaunch: (Map<String, dynamic> message) async {
              _handleNotification(message);
            },
          );
          isConfigured = true;
        }
      }
    

    【讨论】:

      猜你喜欢
      • 2021-09-21
      • 1970-01-01
      • 2021-08-09
      • 2020-07-28
      • 2020-10-01
      • 2021-04-30
      • 2021-08-16
      • 2020-09-02
      • 2020-02-09
      相关资源
      最近更新 更多