react-native-fcm点击托盘通知后如何在特定页面上重定向

Posted

技术标签:

【中文标题】react-native-fcm点击托盘通知后如何在特定页面上重定向【英文标题】:react-native-fcm how to redirect on specific page after click on notification from tray 【发布时间】:2017-08-25 04:09:43 【问题描述】:

在 react-native-fcm 中单击托盘通知后,我试图重定向到特定页面,但无法这样做。 React native fcm 文档没有关于处理点击监听器的正确文档

I follow this tutorial for react-native-fcm

 this.notificationListener = FCM.on(FCMEvent.Notification, notif => 
  console.log("Notification recived", notif);
  if(notif.local_notification)
     console.log("Notification recived local", "local notification");

    return;
  
  if(notif.opened_from_tray)

     console.log("Notification recived tray ", "opened from tray");

    return;
  

  if(Platform.OS ==='ios')
          //optional
          //iOS requires developers to call completionHandler to end notification process. If you do not call it your background remote notifications could be throttled, to read more about it see the above documentation link.
          //This library handles it for you automatically with default behavior (for remote notification, finish with NoData; for WillPresent, finish depend on "show_in_foreground"). However if you want to return different result, follow the following code to override
          //notif._notificationType is available for iOS platfrom
          switch(notif._notificationType)
            case NotificationType.Remote:
              notif.finish(RemoteNotificationResult.NewData) //other types available: RemoteNotificationResult.NewData, RemoteNotificationResult.ResultFailed
              break;
            case NotificationType.NotificationResponse:
              notif.finish();
              break;
            case NotificationType.WillPresent:
              notif.finish(WillPresentNotificationResult.All) //other types available: WillPresentNotificationResult.None
              break;
          
  

任何帮助将不胜感激。

提前致谢

【问题讨论】:

我认为您可以在if(notif.opened_from_tray) 语句中单击通知时添加您想做的任何事情。 【参考方案1】:

大家好,问题已通过使用通知opens_from_tray 块中的setTimeout 函数修复。

示例代码

redirect() this.props.navigator.push(name:"DetailPage", data:this.state.pageData[1]); 

`

this.notificationListener = FCM.on(FCMEvent.Notification, notif => 
console.log("Notification recived", notif);
if(notif.local_notification)
console.log("Notification recived local", "local notification");

  if(notif.opened_from_tray)

     console.log("Notification recived local ", "opened from tray");

    setTimeout(() => 

    //    console.log('setting page call!'); 
    console.log('this is props',props);
      this.redirect()
      // this.props.navigator.push(name:"Sports");
    //  // this.props.navigator.push(name:'DetailPage',data:Data);
    , 5000);

     //this.props.navigator.push(name:"Settings");

  

return;

if(notif.opened_from_tray)

 console.log("Notification recived tray ", "opened from tray");
 //this.props.navigator.replacePrevious(name:"Settings");
return;


if(Platform.OS ==='ios')
//optional
//iOS requires developers to call completionHandler to end notification process. If you do not call it your background remote notifications could be throttled, to read more about it see the above documentation link.
//This library handles it for you automatically with default behavior (for remote notification, finish with NoData; for WillPresent, finish depend on "show_in_foreground"). However if you want to return different result, follow the following code to override
//notif._notificationType is available for iOS platfrom
switch(notif._notificationType)
case NotificationType.Remote:
notif.finish(RemoteNotificationResult.NewData) //other types available: RemoteNotificationResult.NewData, RemoteNotificationResult.ResultFailed
break;
case NotificationType.NotificationResponse:
notif.finish();
break;
case NotificationType.WillPresent:
notif.finish(WillPresentNotificationResult.All) //other types available: WillPresentNotificationResult.None
break;

`

【讨论】:

以上是关于react-native-fcm点击托盘通知后如何在特定页面上重定向的主要内容,如果未能解决你的问题,请参考以下文章

如何以编程方式关闭通知托盘

如何知道应用程序是不是从 android 通知托盘打开?

Ionic 2:点击推送通知

React Native Push Notification - react-native-fcm - ios无法构建

C# winform 启动后自动最小化至托盘,如何去掉屏幕左下角缩小的那部分

在 Windows 10 中,如何翻译托盘通知气球的页脚?