Lilás
Lilás

Reputation: 1201

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

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.

Upvotes: 2

Views: 1744

Answers (1)

Ajoy
Ajoy

Reputation: 1848

  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.

Upvotes: 1

Related Questions