【问题标题】:Goto particular path when push notification arrives (Angular, Ionic, ngcordova)推送通知到达时转到特定路径(Angular、Ionic、ngcordova)
【发布时间】:2015-07-27 10:43:19
【问题描述】:

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

【问题讨论】:

  • 你试过监听接收到的事件吗? $rootScope.$on('$cordovaPush:notificationReceived', func(event, notification))
  • 当然。但是,此事件仅用于在通知到达时捕获事件。我的问题:如何在通知抽屉中捕获与单击通知对应的事件?

标签: android angularjs push-notification ionic


【解决方案1】:

好的。事实证明,不需要点击通知点击事件。 您可以使用以下命令检查应用程序是否在前台:

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

【讨论】:

  • 我怎样才能动态实现这个?
  • 你这是什么意思?你能详细说明@KaranSofat 吗?
【解决方案2】:

几天前我回答了一个类似的问题。这是link

我所做的是当通知到达应用程序时,我将播放负载中的一些数据保存到 localStorage 中,然后在应用程序的 resume 事件中阅读此内容来自 localStorage 的 var 并更改应用程序的状态并将此 var 传递给我想要的 url。

当应用处于后台并收到通知时,您可以将播放负载保存到 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);
        }
    }
});

然后在 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});
     }
});

【讨论】:

    【解决方案3】:

    如果您使用的是 phonegap-plugin-push,这可以简单地完成。

    请参阅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
        }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-12
      • 2019-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多