【问题标题】:FirebaseMessaging.instance.getInitialMessage() is not called in terminated stateFirebaseMessaging.instance.getInitialMessage() 未在终止状态下调用
【发布时间】:2022-07-14 13:46:38
【问题描述】:

当我们单击包含此类消息数据的通知时,FirebaseMessaging.instance.getInitialMessage() 不会在终止状态下调用,而是从初始屏幕显示屏幕

{ “registration_ids”:[ ""

],


"data": {
    "title": "Flutter9 Object Notification.3...",
    "body": "this is flutter Data Object notification test message from",
    "android_channel_id": "dgdgsdfgs",
},
"android":{
    "priority":"high"
}

}

【问题讨论】:

  • 我有同样的问题。有更新吗?

标签: flutter


【解决方案1】:

鉴于您没有展示如何设置 fcm ,这里是如何去做

/// Create a [AndroidNotificationChannel] for heads up notifications
late AndroidNotificationChannel channel;

/// Initialize the [FlutterLocalNotificationsPlugin] package.
late FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin;

/// To verify things are working, check out the native platform logs.
Future<void> firebaseMessagingBackgroundHandler(RemoteMessage message) async {
  // If you're going to use other Firebase services in the background, such as Firestore,
  // make sure you call `initializeApp` before using other Firebase services.
  await Firebase.initializeApp(
    options: DefaultFirebaseOptions.currentPlatform,
  );
  print('Handling a background message ${message.messageId}');
}

在 main.dart 中

WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp(
    options: DefaultFirebaseOptions.currentPlatform,
  );
  FirebaseMessaging.onBackgroundMessage(firebaseMessagingBackgroundHandler);


// setup displaying notifications 

channel = const AndroidNotificationChannel(
      'high_importance_channel', // id
      'High Importance Notifications', // title// description
      importance: Importance.high,
    );

    flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();

    /// Create an Android Notification Channel.
    ///
    /// We use this channel in the `AndroidManifest.xml` file to override the
    /// default FCM channel to enable heads up notifications.
    await flutterLocalNotificationsPlugin
        .resolvePlatformSpecificImplementation<
            AndroidFlutterLocalNotificationsPlugin>()
        ?.createNotificationChannel(channel);

    /// Update the iOS foreground notification presentation options to allow
    /// heads up notifications.
    await FirebaseMessaging.instance
        .setForegroundNotificationPresentationOptions(
      alert: true,
      badge: true,
      sound: true,
    );


在你的 initstate 中处理消息调用这个

FirebaseMessaging.instance
        .getInitialMessage()
        .then((RemoteMessage? message) {
      if (message != null) {
        // do message things
      }
    });
    FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
      showMessage(message: message);
    });
    FirebaseMessaging.onMessage.listen((RemoteMessage message) {
      RemoteNotification? notification = message.notification;
      AndroidNotification? android = message.notification?.android;

      if (notification != null && android != null) {
        flutterLocalNotificationsPlugin.show(
            notification.hashCode,
            notification.title,
            notification.body,
            NotificationDetails(
                android: AndroidNotificationDetails(
                  channel.id,
                  channel.name,
                ),
                iOS: IOSNotificationDetails(subtitle: notification.title)));
      }
    });

如果您需要在收到消息时导航

void showMessage({required RemoteMessage message}) async {
    if (message.data['route'] == "mypage") {
      var myparams =  message.data['payload'];
      Navigator.of(context).push(MaterialPageRoute(
          fullscreenDialog: true,
          builder: (_) {
            return MyPage(
             params:myparams,
            );
          })); ...

【讨论】:

  • 是的,但是当我们点击该通知时 FirebaseMessaging.instance.getInitialMessage() 没有触发,因为我将其显示为本地通知
  • 应用在前台还是后台?
  • 完全终止或关闭
  • 仍然面临这个问题。任何更新@Saikiran
【解决方案2】:

"使用 FCM,您可以向客户端发送两种类型的消息:

  • 通知消息,有时被认为是“显示消息”。这些由 FCM SDK 自动处理。
  • 数据消息,由客户端应用程序处理。 "

https://firebase.google.com/docs/cloud-messaging/concept-options

您要发送的是数据消息,我最好的猜测是 firebase sdk 无法识别数据消息,因此它不会触发 firebase 消息事件侦听器。您的通知必须有一个“通知”字段供 firebase sdk 收听。

【讨论】:

    猜你喜欢
    • 2021-04-01
    • 2021-12-25
    • 1970-01-01
    • 2022-08-18
    • 1970-01-01
    • 1970-01-01
    • 2017-11-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多