繁体   English   中英

推送通知到达时转到特定路径(Angular,Ionic,ngcordova)

[英]Goto particular path when push notification arrives (Angular, Ionic, ngcordova)

考虑这种情况。 我有一个离子/角度应用程序,并且正在使用ngcordova插件进行推送通知。 现在让我们说当应用程序在后台时推送通知到达。 用户,在应用程序抽屉中查看通知,然后单击通知以获取更多信息。 我希望用户使用$ location.path()导航到特定路径。 如何利用通知点击事件?

好。 事实证明,无需点击通知单击事件。 您可以使用以下方法检查应用程序是否处于前台:

notification.foreground

因此,如果

if(notification.foreground) {
  //do something for the case where user is using the app.
  $popup('A notification just arrived');
} else {
  //do something for the case where user is not using the app.
  $location.path('/tab/somepage');
}

几天前,我回答了类似的问题。 这是链接

and then in the event of the app i read this var from and change the state of the app and passing this var to the url i want. 我所做的是当通知到达应用程序时,我将播放数据中的一些数据保存到了 ,然后在应用程序的事件中,我从读取了此并更改了应用程序的状态并将此传递给url我想要。

: 当应用程序在后台运行并收到通知时,您可以将播放负载保存到

$rootScope.$on('$cordovaPush:notificationReceived', function(event, notification) {
   if (ionic.Platform.isAndroid() && notification.event == 'message') {
        var newsid=notification.payload.newsid;
        if (typeof newsid != 'undefined') {
            // save the newsid to read it later in the resume event
            $window.localStorage.setItem('goafterpush',newsid);
        }
    }
});

event you can read the previous saved playload and use that data to change the state of the app and redirect to another route: 然后,在事件中,您可以读取之前保存的播放负载,并使用该数据来更改应用程序的状态并重定向到另一条路线:

$ionicPlatform.on("resume",function(event){
     // read the newsid saved previously when received the notification
     var goafterpush=$window.localStorage.getItem('goafterpush');
     if (goafterpush) {
         $window.localStorage.removeItem('goafterpush');
         $state.go('app.newsdetail',{id:goafterpush});
     }
});

如果您正在使用phonegap-plugin-push,则只需完成此操作即可。

从此处参考博客文章

push.on('notification', function(data) {
    if(data.additionalData.foreground){
        //Do stuff you want to do if app is running on foreground while push notification received
    }else{
        //Do stuff you want to do if the app is running on background while the push notification received
    }
});

暂无
暂无

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

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