phonegap-build / PushPlugin

This repository is deprecated head to phonegap/phonegap-push-plugin
https://github.com/phonegap/phonegap-plugin-push
MIT License
1.32k stars 999 forks source link

Can't get reg ID - callback from register function returns "OK" #276

Open seth100 opened 9 years ago

seth100 commented 9 years ago

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?

appastair commented 9 years ago

Perhaps it was already registered and you didn't see/store the ID...?

pushNotification.unregister(successHandler, errorHandler);

Try again. :smile: :thumbsup:

nanderson94 commented 9 years ago

From what I've read on a PhoneGap support thread, it looks like cases where you receive "OK" in place of a registrationId is related to a missing or invalid Sender ID. Double check that the number you're passing to register is your Android Project ID/Number (ID on the old developer console, Number on the new developer console, but it should just be a large number).

Additionally, a coworker who was building the project on his machine was getting both "OK" and the registrationId, while apks built on my machine were getting only the Registration ID. We tested this and proved it across various devices (ranging from 4.1.2 to 4.4.4 to the "L" preview). Turns out the only difference in our process/code was how we were building the package. My coworker used cordova platform update android before each build while I did a full cordova platform rm android; cordova platform add android. We don't know exactly why, but after my coworker ran an rm/add instead of an update, he stopped receiving the "OK" messages.

In summary: Double check your Sender ID, and if that doesn't work, run:

cordova platform rm android
cordova platform add android
seth100 commented 9 years ago

thanks for your help, I tried to run: cordova platform rm android cordova platform add android and double check the Sender ID but still no luck. I also tried to run unregister first but same result.

surinaidu commented 9 years ago

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

};

seth100 commented 9 years ago

Hi, thanks for the answer. I already checked and I'm sure the plugin code starts when device is ready. Also I already was running my app on a phone (android v4.2.2 and 4.1).

surinaidu commented 9 years ago

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

seth100 commented 9 years ago

yes I tried with wi-fi ON and also with only 3G ON, same result. Also my code is almost the same as your, I do document.addEventListener("deviceready", run, false); and my push_init(); starts only when run function starts, so only when device is ready.

surinaidu commented 9 years ago

May i know how to get this plugin which is upto specific revision.

seth100 commented 9 years ago

sorry what do you mean? Btw I have read in here: http://community.phonegap.com/nitobi/topics/problem_with_pushnotification_register that you solve the "OK" problem by resetting your phone, but I don't understand what you mean with "resetting the phone", another user asked you the same thing on that site.

surinaidu commented 9 years ago

Uninstall your app and get the fresh one, reset your phone(delete cache) if it does not works.

seth100 commented 9 years ago

ok thanks, I tried to do that and to clear my phone cache before and after but no success.

surinaidu commented 9 years ago

Can you check 'Google Cloud Messaging For Android' is 'ON' in your google developer console.

seth100 commented 9 years ago

yes it's already ON

surinaidu commented 9 years ago

onNotificationGCM: function(e) { alert("In the onNotificationGCM " + e.event); // Are you getting this alert ? 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;
}

}

seth100 commented 9 years ago

Unfortunately my problem is that the onNotificationGCM function is never called so I never get that alert: alert("In the onNotificationGCM " + e.event);

surinaidu commented 9 years ago

I think you better to clear every thing and create your app again with the following steps. http://devgirl.org/2013/07/17/tutorial-implement-push-notifications-in-your-phonegap-application/

seth100 commented 9 years ago

I already followed that guide.

boynet commented 9 years ago

Hi same problem :) did you solve it?

seth100 commented 9 years ago

Unfortunately I did not solve the problem.

boynet commented 9 years ago

I checked the same app on two different galaxy s2. on one its works the another return OK. there is app in play store https://play.google.com/store/apps/details?id=com.firstrowria.pushnotificationtester to test push notifaction, as excepted on one it workd on another not.

so here is the facts: 1:the big guys(facebook,whatsapp,gmail) push notifaction works on all devices 2.the bug is not in the js code 3.there something worng in the devices but there is a way to workaround it as the big apps work fine. 4.so the solution is somewhere in the plugin java code

I add the console code from both devices running the pushnotificationtester this devices push not coming(success: OK) https://github.com/boynet/push-notifaction-bug-logs/blob/master/push-not-working-log

this push working good: https://github.com/boynet/push-notifaction-bug-logs/blob/master/push-working

matteocarde commented 9 years ago

See my fix in #309

gabriel-anglada commented 9 years ago

Look at this! --> http://stackoverflow.com/questions/27207588/only-getting-string-ok-instead-of-the-device-registration-id-from-cordova-push

AmirMeirbekov commented 9 years ago

PushPlugin started working for me when I put "onNotification" function (the one responsible for receiving notification messages, including "registered" message) outside the scope of "onDeviceReady" event

3B00D commented 8 years ago

Putting the ecb callback function outside the onDeviceReady solved my problem. thank you AmirMeirbekov

braianj commented 8 years ago

Maybe you are looking for the token on the function

function successHandler (result) {
    console.log('success:'+ result );
}

Try inside the onNotification(e) function when e.event == registered, e.regid is the token.

Good luck

farshidbeheshti commented 8 years ago

onNotificationGCM must be defined in the window scope: window.onNotificationGCM = function() { // Get The reg ID }

NishanthKabra commented 8 years ago

In my case the issue was not having internet connection. Once i switched on wifi, i started getting regid instead of "OK". I felt it strange as to how my regid has anything to do with internet in my device.