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.
register
When you call
pushNotification.register():successHandleris called if there is no error in parsing yourecbandsenderID. YoursenderIDand application are registered with GCMRegistrar.errorHandleris called only if there is a JSON error while parsing theecborsenderID
Once your setup is correct, your
errorHandlerwill probably never be called.From the docs, you have to call the
registermethod every time you start the application. This will not create duplicate registrations. The plugin will manage all that itself.onNotificationGCM
Only called when:
- You receive a notification from GCM.
- Your application is successfully registered with
GCMRegistrar
GCMRegistrar
- Returns registration ID (for your application and
senderID) if already registered. - If not, it registers your application and
senderIDand then returns registration ID. This requires an Internet connection (AFAIK)
So when you call
pushNotification.registerthe first time, if you are not connected to the Internet, PushPlugin returnsOKbut GCMRegistrar has no registration ID to return to you.- Returns registration ID (for your application and
来源:https://stackoverflow.com/questions/28748903/phonegap-gcm-push-notification-registration-call-returns-ok-but-device-is-not-e