【问题标题】:Cloud Messaging onMessage callback never gets called when app is in the foreground but onResume and onLaunch do get called?当应用程序在前台时,云消息传递 onMessage 回调永远不会被调用,但 onResume 和 onLaunch 会被调用吗?
【发布时间】:2020-08-16 17:32:26
【问题描述】:

我实现了 firebase 云消息传递并遵循以下教程 https://www.youtube.com/watch?time_continue=304&v=2TSm2YGBT1s&feature=emb_logo。我让它大部分工作。每当应用程序在后台或关闭时,我都会收到通知,它将打开应用程序并执行回调 onResume 或 onLaunch。

但我似乎无法让 onMessage 回调工作。每当应用程序在前台并且我发送通知时,不会调用 onMessage 吗?我得到以下日志到我的控制台

E/FlutterFcmService(18141): Fatal: failed to find callback
W/FirebaseMessaging(18141): Unable to log event: analytics library is missing
W/FirebaseMessaging(18141): Unable to log event: analytics library is missing

我也没有看到 onBackgroundMessage 被调用。

通知服务

class CloudMessagingService extends NotificationService
{
  final FirebaseMessaging firebaseMessaging = FirebaseMessaging();

  CloudMessagingService()
  {
    if(Platform.isIOS)
      firebaseMessaging.requestNotificationPermissions(IosNotificationSettings());

    firebaseMessaging.configure(
      onMessage: (Map<String, dynamic> message) async {
        print('I have a cloud messaging message yay');
        print('onMessage: $message');
      },
      onBackgroundMessage: (Map<String, dynamic> message) async {
        print('onBackgroundMessage: $message');
      },
      onLaunch: (Map<String, dynamic> message) async {
        print('onLaunch: $message');
      },
      onResume: (Map<String, dynamic> message) async {
        print('onResume: $message');
      },
    );
  }

  Future<void> sendNotificationToGroup(GroupModel group, NotificationType type)
  {
    print('Send ${type.valueString} notification to ${group.id}');
    return null;
  }
}

我正在使用 Pixel 3XL 的 android 模拟器上对此进行测试

【问题讨论】:

    标签: flutter dart firebase-cloud-messaging


    【解决方案1】:

    于是我仔细查看了文档,发现

    注意:当您在 Android 上进行调试时,请使用具有以下功能的设备或 AVD 谷歌播放服务。否则您将无法进行身份验证。

    切换到支持 playstore 的设备后,调用了 onMessage 回调!

    【讨论】:

    • 谢谢。你救了我。
    猜你喜欢
    • 2021-03-31
    • 2019-12-30
    • 1970-01-01
    • 1970-01-01
    • 2012-02-18
    • 1970-01-01
    • 1970-01-01
    • 2020-06-03
    • 2020-06-21
    相关资源
    最近更新 更多