Firebase Messaging 无法发送后台消息 Flutter

时间:2021-01-28 17:41:39

标签: firebase flutter google-cloud-messaging

我的 index.ts 中有以下代码

const payload: admin.messaging.MessagingPayload = {
      notification: {
        title: 'New Order!',
        body: you sold a ${order.day} for ${order.time},
      },
    }; 
    return fcm.sendToDevice(tokens, payload);

这在我的 Flutter 中:

 messaging.configure(
      onMessage: (Map<String, dynamic> message) async {
        print("onMessage: $message");
      },
      onLaunch: (Map<String, dynamic> message) async {
        print("onLaunch: $message");
      },
      onResume: (Map<String, dynamic> message) async {
        print("onResume: $message");
      },
      onBackgroundMessage: myBackgroundMessageHandler
    );
  }
  static Future<dynamic> myBackgroundMessageHandler(Map<String, dynamic> message) {
    var data = message['data'] ?? message;
    print(data);
    // Or do other work.
  }

虽然 onMessage 部分运行良好,但 onLaunch 和 onResume 不起作用。 Netierh 负责 onBackgroundMessage。

1 个答案:

答案 0 :(得分:0)

您可以使用 if 方法和 onBackgroundMessagehttps://pub.dev/packages/flutter_local_notifications enter image description here

您正在检查数据而不是通知

flutter_local_notifications