4

我已经建立了一个接收 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?

一种解决方法是将所有通知存储在服务器上并让应用程序在打开时检索它们,但我不想通过发出不必要的请求来使应用程序变慢。

4

1 回答 1

0

假设 GCM 是可靠的,你只需要缓存消息。阅读我对相关问题的回答。因此,即使用户关闭了通知,您的应用程序仍将收到通知有效负载。然后您可以保证您要保存的数据localStorage将交付给您。

注意:我还没有测试过它,但是当我有的时候会更新这个答案。

于 2015-04-08T04:09:17.667 回答