如何在收到通知后迅速导航到特定屏幕?

时间:2020-06-21 00:31:23

标签: firebase flutter notifications navigation firebase-cloud-messaging

我已经成功地将Firebase作为后端在我的flutter应用程序上收到了两种通知,并在云功能中实现了FLUTTER_NOTIFICATION_CLICK以提供点击功能,

现在我想要的是,因为有两种不同的通知,并且我希望根据通知值来导航特定屏幕。我该怎么办?

下面是我正在使用的代码data payloadsflutter code

有效载荷1

const payload = {

              notification : {
                  title: item title,
                  body: notificaionmessage,
                  icon:"default"
                              },

                              data: {
                                       click_action: 'FLUTTER_NOTIFICATION_CLICK',
                                       notification_id: notification_id,
                                       //senderAvatarURL: messageRecieverSenderAvatar,
                                       category: 'default'
                                     }
          };

有效载荷2

const payload = {
                    notification: {
                      title: `NOTIFICATION2 `,
                      body: contentMessage,
                      badge: '1',
                      sound: 'default'
                    },
                    data: {
                             click_action: 'FLUTTER_NOTIFICATION_CLICK',
                             notification2: notificationid2,
                             //senderAvatarURL: messageRecieverSenderAvatar,
                             category: 'default'
                           }
                  }

处理通知的代码

firebaseMessaging.configure(onMessage: (Map<String, dynamic> message) {
      print('onMessage: $message');
      Platform.isAndroid ? showNotification(message['notification']) : showNotification(message['aps']['alert']);
      Navigator.push(context, MaterialPageRoute(builder: (context)=> SCREEN1()));
      return;
    }, onResume: (Map<String, dynamic> message) {
      print('onResume: $message');
      Navigator.push(context, MaterialPageRoute(builder: (context)=> SCREEN1()));
      return;
    }, onLaunch: (Map<String, dynamic> message) {
      print('onLaunch: $message');
      Navigator.push(context, MaterialPageRoute(builder: (context)=> SCREEN1()));
      return;
    });

像上面那样配置了导航,但是我希望它根据有效负载进入不同的屏幕,例如PAYLOAD 1 to SCREEN1PAYLOAD 2 to SCREEN 2

我应该如何处理?

1 个答案:

答案 0 :(得分:0)

您可以通过多种方式进行操作。

例如,您可以检查

click_action:'Move_To_Screen1'

if (message['data']['click_action']=='Move_To_Screen1') 
Navigator.push(context, MaterialPageRoute(builder: (context)=> SCREEN1()));
else if (message['data']['click_action']=='Move_To_Screen2') 
Navigator.push(context, MaterialPageRoute(builder: (context)=> SCREEN2()));

希望有帮助。