在推送通知时访问本地存储

时间:2016-01-10 19:30:51

标签: javascript android angularjs push-notification ionic

在离子应用程序中,我试图在推送通知到达时将一些数据保存到本地存储,但似乎我在推送通知到达时保存到localstorage的数据始终为null。离子推送的onNotification事件处理程序中是否无法访问window.localStorage?

这是我的代码:

$ionicPush.register({
    canShowAlert: true, //Can pushes show an alert on your screen?
    canSetBadge: true, //Can pushes update app icon badges?
    canPlaySound: true, //Can notifications play a sound?
    canRunActionsOnWake: true, //Can run actions outside the app,
    onNotification: function(notification) {
    window.localStorage.setItem('someData', notification.payload.somedata);
   }
})

稍后在应用中,当我尝试window.localStorage.getItem('someData')时,它始终为空。

1 个答案:

答案 0 :(得分:0)

您好我正在为ngCordova使用PushNotification插件,只是存储在localstorage中

$cordovaPush.register(androidConfig).then(function(result) {
console.log(result);
try {
    window.localStorage.setItem("pushID",result);
    } catch (e) {
    console.log(e);
    }
   }, function(err) {
    console.log(err);
  })

如果你想从localstorage获取pushID ====

 var pushId = window.localStorage['pushID'] || false;

返回值或false [如果不存在pushID]。