我已经设置了一个接收GCM推送通知的Ionic Framework项目。我想将传入的通知保存在应用程序的window.localStorage中。
这是我到目前为止所尝试的:
function onNotificationGCM(e) {
switch( e.event )
{
case 'registered':
if ( e.regid.length > 0 )
{
//call back to web service in Angular
var elem = angular.element(document.querySelector('[ng-app]'));
var injector = elem.injector();
var myService = injector.get('PushProcessingService');
myService.registerID(e.regid);
}
break;
case 'message':
if (e.foreground)
{
//no problem here, this works as the app is already open
window.localStorage['notifications'] = e.payload.message;
}
else
{
//saving to localStorage in here works only when the user opens the app via the received notification
if (e.coldstart)
window.localStorage['notifications'] = e.payload.message;
else
window.localStorage['notifications'] = e.payload.message;
}
break;
case 'error':
break;
default:
break;
}
}
保存到window.localStorage可以在通知到达时已经打开应用程序,但是当应用程序关闭时,这些方法将不会被调用。只有当用户点击通知然后打开应用程序时,才会发生这种情况。
是否有办法将通知数据保存到应用的localStorage,即使用户解除了传入的推送通知?
解决方法是将所有通知存储在服务器上并让应用程序在打开时检索它们,但我不想通过提出不必要的请求来使应用程序变慢。