简体   繁体   English

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

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

Consider this scenario. 考虑这种情况。 I have an ionic / angular app and I am using the the ngcordova plugin for push notifications. 我有一个离子/角度应用程序,并且正在使用ngcordova插件进行推送通知。 Now let us say a push notification arrives when the app is in the background. 现在让我们说当应用程序在后台时推送通知到达。 User, views the notification in the App drawer and then clicks the notification for further information. 用户,在应用程序抽屉中查看通知,然后单击通知以获取更多信息。 I would like the user to navigate to a particular path using $location.path(). 我希望用户使用$ location.path()导航到特定路径。 How can I tap into the notification click event? 如何利用通知点击事件?

Ok. 好。 Turns out that there is no need to tap into the notification click event. 事实证明,无需点击通知单击事件。 You could check if the app is in foreground using: 您可以使用以下方法检查应用程序是否处于前台:

notification.foreground

so, if 因此,如果

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');
}

I answered a similar question some days ago. 几天前,我回答了类似的问题。 Here's the link 这是链接

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

When the app is in the background and receive the notification you can save the playload to the localStorage : 当应用程序在后台运行并收到通知时,您可以将播放负载保存到localStorage

$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);
        }
    }
});

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

$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});
     }
});

This can simply be done if you are using phonegap-plugin-push. 如果您正在使用phonegap-plugin-push,则只需完成此操作即可。

Refer the blog article from here . 从此处参考博客文章

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