【问题标题】:Accessing local storage at the time of a push notification在推送通知时访问本地存储
【发布时间】:2016-01-10 19:30:50
【问题描述】:

在 ionic 应用程序中,我试图在推送通知到达时将一些数据保存到本地存储,但似乎我在推送通知到达时保存到本地存储的数据始终为空。离子推送的 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') 时,它始终为空。

【问题讨论】:

  • 您是否确认notification.payload.somedata 在您的回调中不是null
  • 是的,我已经验证它不为空
  • 即使我做了window.localStorage.setItem('someData', 20),它仍然是null

标签: javascript android angularjs push-notification ionic


【解决方案1】:

嗨,我正在使用ngCordova 插件为PushNotification 并简单地存储在本地存储中

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

如果你想从本地存储中获取 pushID ====

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

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

【讨论】:

  • 谢谢阿努拉格。这正是我正在做的事情,但想知道为什么它在我的情况下不起作用。是否有任何原因无法在 onRegister 事件处理程序中访问它?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-01-28
  • 2017-01-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多