PushPlugin: Can't get reg ID - callback from register function returns "OK"

I’m working on a cordova 3.5 application and I’m using this plugin for push notification.

Here is my code:

push_init: function(){
var SENDER_ID = "my GCM project number";
var pushNotification;
var txt;
var successHandler = function( result ) {
     navigator.notification.alert('Push: win -> ' + result);
};
var errorHandler = function( error ) {
     navigator.notification.alert('Push: error -> ' + error );
};

var push_android = function(e) {

     navigator.notification.alert('push_android() - connection established...');

      switch( e.event ) {
        case 'registered':
            if ( e.regid.length > 0 )
            {
               navigator.notification.alert("Regid " + e.regid);
               navigator.notification.alert('registration id = '+e.regid);
            }
            break;

       case 'message':
         navigator.notification.alert('message = '+e.message+' msgcnt = '+e.msgcnt);
            break;

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

         default:
         navigator.notification.alert('An unknown GCM event has occurred');
         break;
       }
 };

 var onNotificationAPN = function( e ) {
        navigator.notification.alert('onNotificationAPN()');
 };

        try {

            pushNotification = window.plugins.pushNotification;   

            if ( device.platform == 'android' || device.platform == 'Android' || device.platform == "amazon-fireos" ) {

                pushNotification.register(
                    successHandler,
                    errorHandler,
                    { 
                        "senderID": SENDER_ID,
                        "ecb": "push_android" 
                    }
                );

            } else {
                pushNotification.register(
                    successHandler,
                    errorHandler,
                    { 
                        "badge": "true",
                        "sound": "true",
                        "alert": "true",
                        "ecb": "onNotificationAPN"
                    }
                );
            }

        } catch (error) {
            txt = "There was an error on this page.\n\n";
            navigator.notification.alert(txt);
        }

    },

My problem is that when I run my app on my android phone (HTC one with kitkat 4.4.2) it just alerts Push: win -> OK instead it should alert the registration ID I will use later to send notifications.

I have already created a new project on [Android Developers Console][2] site and activeted GCM and I got a Project ID and a Project Number (the one I used above), I also created a API Key for server applications and I got an API key string. My google account is the same I use on my android phone (I don’t know if it’s required).

So what do you think? Am I missing something?

About this issue

  • Original URL
  • State: open
  • Created 10 years ago
  • Comments: 27

Most upvoted comments

Initially i also had the same problem when i was running in the android emulator. Try to run on the real device.The message you are getting(OK) is plugin message not from GCM. You should use your above code once the device is ready like below(working code).

var app = { // Application Constructor initialize: function() { this.bindEvents(); }, // Bind Event Listeners // // Bind any events that are required on startup. Common events are: // ‘load’, ‘deviceready’, ‘offline’, and ‘online’. bindEvents: function() { document.addEventListener(‘deviceready’, this.onDeviceReady, false); }, // deviceready Event Handler // // The scope of ‘this’ is the event. In order to call the ‘receivedEvent’ // function, we must explicity call ‘app.receivedEvent(…);’ onDeviceReady: function() { alert(“The device is ready to use”); app.receivedEvent(‘deviceready’); }, // Update DOM on a Received Event receivedEvent: function(id) { alert(“Entered in the received event”);

    alert("Sending project id to GCM server to register with the gcm");
    var pushNotification = window.plugins.pushNotification;
    pushNotification.register(app.successHandler, app.errorHandler,{"senderID":"776989336731","ecb":"app.onNotificationGCM"});

    var parentElement = document.getElementById(id);
    var listeningElement = parentElement.querySelector('.listening');
    var receivedElement = parentElement.querySelector('.received');

    listeningElement.setAttribute('style', 'display:none;');
    receivedElement.setAttribute('style', 'display:block;');

    console.log('Received Event: ' + id);
},
// 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) {
    alert("In the onNotificationGCM " + e.event);
    switch( e.event )
    {
        case 'registered':
            if ( e.regid.length > 0 )
            {
                console.log("Regid " + e.regid);
                alert('registration id = '+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;
    }
}

};

Please make sure your wi-fi is on. If possible you can check with my code by replacing project-id.