简体   繁体   English

Firebase FCM 后台通知 click_action

[英]Firebase FCM background notifications click_action

i want to navigate to a specific screen route when i click on the background notification for now the default behavior is just launching my app, so how and where do i change the default behavior of the click action当我点击背景通知时,我想导航到特定的屏幕路线现在默认行为只是启动我的应用程序,那么我如何以及在哪里更改点击操作的默认行为

i'm sending the notification using cloud functions, here is the code我正在使用云功能发送通知,这是代码

const payload: admin.messaging.MessagingPayload = {
  notification: {
    title: doc["senderName"],
    body: doc["msg"],
    sound: "default",
    badge: "1",
  },
  data: {
    type: "chat",
  },
};
return fcm
  .sendToDevice(tokens, payload);

somehow i want to access the type "chat" so from there i can navigate to a chat screen不知何故,我想访问“聊天”类型,所以我可以从那里导航到聊天屏幕

Use onMessageOpenedApp to handle when a user presses a notification.当用户按下通知时,使用onMessageOpenedApp处理。 RemoteMessage has data property which holds the custom parameters. RemoteMessage具有保存自定义参数的数据属性。

  • When you click on the notification from background state, onMessageOpened stream function is called.当您点击来自后台 state 的通知时,会调用onMessageOpened stream function。

Initialize this stream function in the initState of first page of the app and check the data in payload you received from the notification just like this:在应用程序首页的 initState 中初始化此 stream function 并检查您从通知中收到的有效负载中的数据,如下所示:

FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) async {

      if (message != null) {
            String screenName = message.data['screenName'];
            print("Screen name is: $screenName");

            if (screenName == 'chat') {
                    //Navigate to your chat screen here
                 }
       }
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM