Open seth100 opened 9 years ago
Perhaps it was already registered and you didn't see/store the ID...?
pushNotification.unregister(successHandler, errorHandler);
Try again. :smile: :thumbsup:
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
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.
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;
}
}
};
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).
Please make sure your wi-fi is on. If possible you can check with my code by replacing project-id.
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.
May i know how to get this plugin which is upto specific revision.
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.
Uninstall your app and get the fresh one, reset your phone(delete cache) if it does not works.
ok thanks, I tried to do that and to clear my phone cache before and after but no success.
Can you check 'Google Cloud Messaging For Android' is 'ON' in your google developer console.
yes it's already ON
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;
}
}
Unfortunately my problem is that the onNotificationGCM
function is never called so I never get that alert: alert("In the onNotificationGCM " + e.event);
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/
I already followed that guide.
Hi same problem :) did you solve it?
Unfortunately I did not solve the problem.
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
See my fix in #309
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
Putting the ecb callback function outside the onDeviceReady solved my problem. thank you AmirMeirbekov
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
onNotificationGCM must be defined in the window scope:
window.onNotificationGCM = function() { // Get The reg ID }
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.
I'm working on a cordova 3.5 application and I'm using this plugin for push notification.
Here is my code:
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?