【问题标题】:Flutter FCM onMessage doesn't work when listen to itFlutter FCM onMessage 在听的时候不起作用
【发布时间】:2021-06-29 10:19:22
【问题描述】:

我有这个问题...发送通知时,在收听 onMessage 时的回调没有被调用,而通知显示在设备上。

初始化时

FirebaseMessaging.instance.requestPermission(
    sound: true,
    badge: true,
    alert: true,
    provisional: false,
  );

String? token = await _firebaseMessaging.getToken();


FirebaseMessaging.instance.setForegroundNotificationPresentationOptions(
    alert: true,
    badge: true,
    sound: true,
  );

在听

 FirebaseMessaging.onMessage.handleError((error) {
      print("Erorrrrrr : ${error.toString()}");
    }).listen((event) {
      print(event.data);
      _showNotification({
        "title": event.notification?.title ?? "",
        "body": event.notification?.body ?? "",
      });
    });

发布规范:
sdk: ">=2.12.0 firebase_core:^1.0.0
firebase_messaging:^9.0.0

医生总结

Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel master, 2.1.0-13.0.pre.214, on macOS 11.2.3 20D91 darwin-x64, locale en-EG)
[!] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
    ! Some Android licenses not accepted.  To resolve this, run: flutter doctor --android-licenses
[✓] Xcode - develop for iOS and macOS
[✓] Chrome - develop for the web
[✓] Android Studio (version 4.1)
[✓] Connected device

【问题讨论】:

  • 嗨!您尝试在前台而不是在后台处理传入消息,对吗?
  • 在任何一种情况下,即使是 onMessageOpenedApp,也不会打印任何内容
  • 我在 ^9.1.3 版本中遇到了同样的问题,我需要管理通知点击事件,但前台点击消息不起作用。到达 msg ai 在控制台中得到这个:“访问隐藏方法 Landroid/os/WorkSource;->add(I)Z (greylist,test-api,reflection, allowed)”,并且 FirebaseMessaging.onMessage.listen 没有被触发,我的 onmessage 函数只有一个 print(¨msg¨)

标签: flutter push-notification firebase-notifications


【解决方案1】:

如果您从 firebase 控制台发送通知并尝试打印
打印(事件。数据); 这将引发异常,因为在这种情况下数据将为空,您必须调用 event.title 或 body

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-08-19
    • 2021-01-21
    • 2018-11-30
    • 2019-11-25
    • 2023-03-25
    • 2020-10-27
    • 2020-10-14
    • 1970-01-01
    相关资源
    最近更新 更多