Phonegap: GCM Push Notification registration call returns OK but device is not even connected to the internet

孤者浪人 提交于 2019-12-04 04:53:50

问题


I have followed this tutorial and I have the following code:

onDeviceReady I execute:

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

The Handlers:

// 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;
        }
    }

This code works perfectly if my device is connected to the internet on the first time I open the app.

If my device is not connected the successHandler is being called with "OK" and the onNotificationGCM is never called. Is this normal?

I was expecting that the registration should fail and call the errorHandler or the onNotificationGCM with e.event = 'error' so I would be able to postpone the registration, but this is not happening.

I would appreciate any help, thanks.


回答1:


  1. register

    When you call pushNotification.register():

    • successHandler is called if there is no error in parsing your ecb and senderID. Your senderID and application are registered with GCMRegistrar.
    • errorHandler is called only if there is a JSON error while parsing the ecb or senderID

    Once your setup is correct, your errorHandler will probably never be called.

    From the docs, you have to call the register method every time you start the application. This will not create duplicate registrations. The plugin will manage all that itself.

  2. onNotificationGCM

    Only called when:

    • You receive a notification from GCM.
    • Your application is successfully registered with GCMRegistrar
  3. GCMRegistrar

    • Returns registration ID (for your application and senderID) if already registered.
    • If not, it registers your application and senderID and then returns registration ID. This requires an Internet connection (AFAIK)

    So when you call pushNotification.register the first time, if you are not connected to the Internet, PushPlugin returns OK but GCMRegistrar has no registration ID to return to you.



来源:https://stackoverflow.com/questions/28748903/phonegap-gcm-push-notification-registration-call-returns-ok-but-device-is-not-e

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!