【问题标题】:how can get paylod data when I using FCM in flutter?当我在颤振中使用 FCM 时如何获取有效负载数据?
【发布时间】:2022-06-29 00:27:40
【问题描述】:

在flutter中使用FCM如何获取paylod数据?

我试过用这种方法,但我仍然无法得到数据......

哪里有问题?

void getInitialMessage() async {
    RemoteMessage? message =
        await FirebaseMessaging.instance.getInitialMessage();
    print(message?.data["type"]);
    if (message != null) {
      if (message.data["type"] == "noti") {
        print("AAAAA");
        Navigator.of(context).push(
          MaterialPageRoute(
            builder: (context) =>
                PostDetailScreen(postid: message.data["postid"]),
          ),
        );
      } else if (message.data["type"] == "active") {
        Navigator.of(context).push(
          MaterialPageRoute(
            builder: (context) => PostDetailScreen(postid: '456'),
          ),
        );
      } 
    }
  }

我去postman测试的时候发这个格式

{
    "registration_ids" : ["eqgizTPoRXaWaUZCchf88k:APA91bERltHDvOPRZy3FpmXyq51HIIc5TBd1v2FhUJYm1sVUTCAwM9TRA6r_lkNd4jz2Ynk8lf7xGoFGSMud0zNcE-hek8lfLG-MIw4PYAlnbKFIpZ4dfGuYPOPIW55n4JAKLlc_Egju"],
    "priority": "high",
    "data": {
        "type": "noti",
        "postid" : "ulEdnFiEZxyyc33UNvJs"
    },
    "notification": {
        "title": "This is title1",
        "body": "This is body1"
    }
}

我可以收到通知,但我设置了"type": "noti"。因此,当我单击通知消息时,应该将 AAAAA 和 Navigator 打印到 PostDetailScreen。但它总是打开应用程序然后转到主页....

【问题讨论】:

  • Android 或 iOS 或两者兼而有之?

标签: flutter firebase-cloud-messaging


【解决方案1】:

以这种结构发送FCM消息,这里我使用NodeJS Firebase Cloud Function,但重要的是添加第二个,options部分:

await admin.messaging().sendToDevice(tokens, {
    data: {
        click_action: 'FLUTTER_NOTIFICATION_CLICK',
        type: 'noti',
        postid: 'ulEdnFiEZxyyc33UNvJs'
    },
    notification: {
        title: 'title',
        body: 'body'
    },
}, {
    contentAvailable: true,
    priority: 'high',
    timeToLive: 24 * 60 * 60, // in seconds
});

在此之后,getInitialMessage 你应该有:

message.data['type'] // 'noti'
message.data['postid'] // 'ulEdnFiEZxyyc33UNvJs'

【讨论】:

    猜你喜欢
    • 2019-07-19
    • 2020-12-10
    • 1970-01-01
    • 1970-01-01
    • 2020-11-19
    • 2022-08-17
    • 2020-11-26
    • 1970-01-01
    • 2017-07-12
    相关资源
    最近更新 更多