【问题标题】:FirebaseMessaging.onMessageOpenedApp and FirebaseMessaging.instance.getInitialMessagenot working in firebase messaging flutterFirebaseMessaging.onMessageOpenedApp 和 FirebaseMessaging.instance.getInitialMessagenot working in firebase messaging flutter
【发布时间】:2022-12-29 18:29:40
【问题描述】:

我正在使用带有本地通知的 firebase 消息传递,但 FirebaseMessaging.onMessageOpenedAppFirebaseMessaging.instance .getInitialMessage 无法正常工作,因为我想在通知点击时打开应用程序。 这是我的代码:

 FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) async {
  log("onMessageOpenedApp is fired");
  await flutterLocalNotificationsPlugin.cancelAll();
  LoginStorage storage = LoginStorage();
  log("storage.getIsLogin() = ${storage.getIsLogin()}");
  if (storage.getIsLogin() == "true") {
    Get.off(() => ChatPage(
          roomId: message.data["room_id"],
          roomName: message.data["roomname"],
          totalMsgs: "0",
          roomType: message.data["room_type"],
        ));
  } else {
    Get.off(() => LoginPage());
  }
  log('A new onMessageOpenedApp event was published!');

});

【问题讨论】:

    标签: firebase flutter dart firebase-cloud-messaging


    【解决方案1】:

    试试这个代码

    FirebaseMessaging.instance.getInitialMessage().then((RemoteMessage? message){
        if(message!=null){
          log("onMessageOpenedApp is fired");
      await flutterLocalNotificationsPlugin.cancelAll();
      LoginStorage storage = LoginStorage();
      log("storage.getIsLogin() = ${storage.getIsLogin()}");
      if (storage.getIsLogin() == "true") {
        Get.off(() => ChatPage(
              roomId: message.data["room_id"],
              roomName: message.data["roomname"],
              totalMsgs: "0",
              roomType: message.data["room_type"],
            ));
      } else {
        Get.off(() => LoginPage());
      }
      log('A new onMessageOpenedApp event was published!');
        }
     });
    

    【讨论】:

    • 此方法适用于后台通知点击吗?我用过但同样的问题。此方法不会在通知点击时触发。
    • 然后你可能会遇到一些异常,将 listen 方法的所有代码放在 try catch 块中并调试它,你需要最小化你的应用程序然后你可以在单击通知时进行调试。
    • 我试过但没有任何例外。我认为后台通知没有任何作用。
    • await flutterLocalNotificationsPlugin.cancelAll();,这一行是做什么的,试着评论一下
    • 这是为了取消通知。删除它不起作用。
    【解决方案2】:

    您编写了完美的代码,但这个问题与代码无关。发生这种情况是因为当您发出通知时您的应用程序处于前台, 此时需要终止该应用程序或将其置于后台。

    警告:此代码不足以在应用程序终止时导航到您想要的屏幕。

    希望这可以帮助你...

    【讨论】:

      猜你喜欢
      • 2022-12-27
      • 2020-06-22
      • 2020-04-23
      • 2020-06-30
      • 2018-10-18
      • 2021-08-09
      • 1970-01-01
      • 1970-01-01
      • 2020-04-28
      相关资源
      最近更新 更多