首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Android Phonegap应用程序中处理推送通知消息

在Android Phonegap应用程序中处理推送通知消息
EN

Stack Overflow用户
提问于 2014-01-15 13:00:09
回答 2查看 3.6K关注 0票数 6

我正在实现Phonegap Android应用程序中的推送通知。我正在学习这里教程。在本教程中,onDeviceready函数如下所示:

代码语言:javascript
复制
onDeviceReady: function() {
    app.receivedEvent('deviceready');
    var pushNotification = window.plugins.pushNotification;
    pushNotification.register(app.successHandler, app.errorHandler,{"senderID":"836557454855","ecb":"app.onNotificationGCM"});
},

这意味着,每当应用程序启动时,它都会在谷歌注册,以获得推送通知。我想这只需要做一次。所以在我的,我有:

代码语言:javascript
复制
onDeviceReady: function() {

        var device_id = window.localStorage.getItem('mountmercy_device_id');

        //if device_id is in local storage, then it means registration 
        // with google has already taken place. If not, then register
        if(typeof(device_id)==='undefined' || device_id===null){

            var pushNotification = window.plugins.pushNotification;
            if (window.device.platform == 'android' || window.device.platform == 'Android') {
                pushNotification.register(app.successHandler, app.errorHandler,{"senderID":"475226855592","ecb":"app.onNotificationGCM"});                        
            }
            else{
                //so its apple
                 pushNotification.register(app.tokenHandler,app.errorHandler,{"badge":"true","sound":"true","alert":"true","ecb":"app.onNotificationAPN"});
            }

        } 

}

然后,在onNotificationGCM中,设置本地存储,以便不再注册设备:

代码语言:javascript
复制
onNotificationGCM: function(e) {

    switch( e.event )
    {
        case 'registered':
            if ( e.regid.length > 0 )
            {
                  /*
                   save reg id to server and store response in local storage
                   ...
                   ...
                   ...
                  */

                  window.localStorage.setItem('mountmercy_device_id', data.id);
                  window.localStorage.setItem('mountmercy_api_key', data.get('api_key'));

            }
            break;

        case 'message':
            // this is the actual push notification. its format depends on the data model from the push server
            alert('message = '+e.message+' msgcnt = '+e.msgcnt);

            break;

        case 'error':
            //alert('GCM error = '+e.msg);
            break;

        default:
           // alert('An unknown GCM event has occurred');
            break;
    }
}, 

当电话收到新的推送通知时,会出现问题。在本教程的原始项目中,当用户单击通知消息时,应用程序将打开,用户将看到警告:“message = blachblah msgcnt = blahblah”。这是因为执行了onNotificationGCM()中"message“情况下的代码。

在我的应用程序中,应用程序会打开,但"message“情况下的代码不会执行。这是因为,在onDeviceReady()中,我只向谷歌注册了一次设备。如果我移除条件:

代码语言:javascript
复制
if(typeof(device_id)==='undefined' || device_id===null){

每次注册设备时,都会执行“消息”案件。但是,每次在onDeviceReady()中注册设备似乎都是错误的。还有别的解决办法吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-01-17 20:51:20

我在Google消息传递的文档中看到它说

Google可以定期刷新注册ID

在这种情况下,每次打开应用程序时,我都会向Google注册,并更新返回的注册ID,以防其更改。这还解决了在通知栏中单击推送消息时处理该消息的问题。

票数 3
EN

Stack Overflow用户

发布于 2016-01-07 10:21:18

您还可以获得GCM中的canonical_id,即您的服务器(或您用来发送通知的设备)的答案,如下所述:https://developers.google.com/cloud-messaging/registration#canonical-ids

规范ID是设备从GCM获得的最新注册ID。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21138105

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档