点击推送通知后如何在应用程序中打开特定页面?

3

我正在使用cordvoa开发一个安卓应用,并且使用cordova的$PushPlugin实现了推送通知系统。

以下是我的PushPlugin代码 (源代码):

module.run(function($rootScope,$cordovaPush) {

var androidConfig = {
"senderID": "replace_with_sender_id",
};

document.addEventListener("deviceready", function(){
$cordovaPush.register(androidConfig).then(function(result) {
  // Success
}, function(err) {
  // Error
})

$rootScope.$on('$cordovaPush:notificationReceived', function(event, notification) {
  switch(notification.event) {
    case 'registered':
      if (notification.regid.length > 0 ) {
        alert('registration ID = ' + notification.regid);
      }
      break;

    case 'message':
      // this is the actual push notification. its format depends on the data model from the push server
      alert('message = ' + notification.message + ' msgCount = ' + notification.msgcnt);
      break;

    case 'error':
      alert('GCM error = ' + notification.msg);
      break;

    default:
      alert('An unknown GCM event has occurred');
      break;
  }
});


// WARNING: dangerous to unregister (results in loss of tokenID)
$cordovaPush.unregister(options).then(function(result) {
  // Success!
}, function(err) {
  // Error
})

}, false);
});

当点击接收到的推送通知时,我希望将用户重定向到我的应用程序中的指定页面。我参考了这个(基于phonegap pushplugin),但是没有找到解决方案。任何帮助都将受到赞赏。

2个回答

3

这是一种简单的解决方案,不需要使用任何angular路由。

case 'message':
if (notification.foreground === false){
   window.location = '#/abc';
   window.location.reload();
}

1
如果您检查对象 notification 的结构,您会发现有一个属性 foreground,当您从点击通知进入应用程序时,该属性将为 false。您可以使用这个属性来放置您的逻辑。
case 'message':
 if(notification.foreground === false){
   //add logic for navigation to your specific page i.e $state.go('myPage')
 }

我尝试了这个代码 if(notification.foreground === false){ $location.path = ('/abc');},并且在module.run函数中也添加了$location。但是当我点击通知时,重定向失败,我被重定向到了主页面。顺便说一下,我正在使用ngRouter。 - Lalitha Ramkumar

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接