Phonegap:GCM 推送通知注册调用返回正常,但设备甚至没有连接到互联网

Posted

技术标签:

【中文标题】Phonegap:GCM 推送通知注册调用返回正常,但设备甚至没有连接到互联网【英文标题】:Phonegap: GCM Push Notification registration call returns OK but device is not even connected to the internet 【发布时间】:2015-02-26 17:27:37 【问题描述】:

我已关注this tutorial,我有以下代码:

onDeviceReady 我执行:

var pushNotification = window.plugins.pushNotification;
pushNotification.register(app.successHandler, app.errorHandler,"senderID":"824841663931","ecb":"app.onNotificationGCM");

处理程序:

// result contains any message sent from the plugin call
successHandler: function(result) 
    alert('Callback Success! Result = '+result)
,

errorHandler:function(error) 
    alert(error);
,

onNotificationGCM: function(e) 
        switch( e.event )
        
            case 'registered':
                if ( e.regid.length > 0 )
                
                    console.log("Regid " + e.regid);
                    alert('registration id = '+e.regid);
                    localStorage.regid = e.regid
                
            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;
        
    

如果我的设备在我第一次打开应用程序时已连接到互联网,则此代码可以完美运行。

如果我的设备未连接,则使用“OK”调用successHandler,并且永远不会调用onNotificationGCM。这正常吗?

我原以为注册会失败,并使用 e.event = 'error' 调用 errorHandler 或 onNotificationGCM,这样我就可以推迟注册,但这并没有发生。

如果有任何帮助,我将不胜感激,谢谢。

【问题讨论】:

遇到了同样的问题,有解决办法吗? 【参考方案1】:

    注册

    当您拨打pushNotification.register()时:

    如果解析ecbsenderID 没有错误,则调用successHandler。您的senderID 和应用程序已在GCMRegistrar 注册。 仅当解析 ecbsenderID 时出现 JSON 错误时才会调用 errorHandler

    一旦您的设置正确,您的errorHandler 可能永远不会被调用。

    从docs,每次启动应用程序时都必须调用register 方法。这不会创建重复的注册。该插件将自行管理所有这些。

    onNotificationGCM

    仅在以下情况下调用:

    您会收到来自 GCM 的通知。 您的应用程序已成功注册到GCMRegistrar

    GCMRegistrar

    如果已注册,则返回注册 ID(用于您的应用程序和 senderID)。 如果没有,它会注册您的应用程序和senderID,然后返回注册 ID。这需要互联网连接 (AFAIK)

    所以当你第一次调用pushNotification.register时,如果你没有联网,PushPlugin返回OK但是GCMRegistrar没有注册ID返回给你。

【讨论】:

以上是关于Phonegap:GCM 推送通知注册调用返回正常,但设备甚至没有连接到互联网的主要内容,如果未能解决你的问题,请参考以下文章

Phonegap 使用 GCM 向 ios 推送通知

未收到 Phonegap iOS 推送通知

如何在phonegap上使用GCM处理android中的多个推送通知

如何通过phonegap通过GCM服务器获取推送通知

用于单一注册的cordova、Phonegap 推送插件和c#?

推送示例android gcm,已注册设备但未收到推送通知