android - Cordova/Phonegap/Ionic - 通过 PushPlugin 接收推送消息并将数据保存到 localStorage,即使用户关闭通知也是如此

标签 android cordova google-cloud-messaging ionic-framework phonegap-pushplugin

我已经建立了一个接收 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,但当应用程序关闭时,这些方法将不会被调用。只有当用户点击通知然后打开应用程序时才会发生这种情况。

有没有一种方法可以将通知数据保存到应用的本地存储中,即使用户关闭了传入的推送通知?

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

最佳答案

假设 GCM 是 reliable ,你只需要缓存消息。阅读我的 answer在相关问题上。因此,即使用户关闭了通知,您的应用程序仍会收到通知负载。然后你可以保证你想保存在localStorage中的数据会被传送给你。

注意:我还没有测试过,但会在测试后更新这个答案。

关于android - Cordova/Phonegap/Ionic - 通过 PushPlugin 接收推送消息并将数据保存到 localStorage,即使用户关闭通知也是如此,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24354156/

相关文章:

android - adb安装apk文件问题

android - 在 Android 环境中单击文本框时,页脚导航栏会向上移动

android - java.lang.NoClassDefFoundError : com. 谷歌.android.gcm.GCMRegistrar

android - 我对 Android 中 Activity 、服务和广播之间的主要区别是否正确?

android - 执行 doInBackground() 时出错

android - 使用 Ionicframework 创建本地存储(设备,而非缓存)

javascript - 控制台日志不适用于 Netbeans(Android 模拟器和 Cordova)

android - Android GCM 的设备消息速率限制?

android - 如何从 ADB 向设备发送 FCM(firebase 云消息传递)推送通知

android - 如何从另一个 Activity 中完成一个 Activity