I followed a tutorial about GCM Notifications located on the link below:
http://techlovejump.com/android-push-notification-using-google-cloud-messaging-gcm-php-google-play-service-library/
Everything worked fine for me, but, it works only on the emulator.
When i transfer the same apk file to mobile devices, they do not receive notifications while the same app on emulator receives them.
Is there any way i can fix this issue?
The only reason I see that this can happen is when the registration_ID is changed. When this happens the device which is supposed to receive the notification as per the expectations does not receives it because the registration_ID is tied to a different device and account.
Make sure you have a distinct registration_ID for the new test device. And even if the versions of Android OS are different on both you need to re-register. Also you need to check that the Registration_ID is getting refreshed everytime in the Broadcast receiver Bundle when it receives a new Registration_ID from Google.
Since gcm.register() is deprecated make use of the new concept Instance_ID by Google.
Related
Is GCM the only way for push notifications in android? And is it advisable over other methods (if there are any)?
So let's say my app from a device registers on GCM. Is it the app itself which is assosciated with the device that registers itself, or the user and his email id that register? If my user switches devices in future, will the same 'registration token' work for him ? Or in a similar scenario if a user with another g-mail id registers to my app from the same device, do i push notifications to the same device with 2 different registration tokens?
Extending to what i asked in 2, suppose a single user has his g-mail id working on my app on different devices. Does GCM push the notification to all those devices, or only one of them? And if one, then how does it prioritize?
No, there are other push notification services in Android, like Parse. However, it is strongly recommended to use the default push notification service that is present in any Android device that has Google Play Store, because that reduces battery usage. If you are pushing your app somewhere else though, like Amazon Store, keep in mind that Kindle Fire does not comply with this and doesn't have Google Play Services -- at least as far as I know.
The registration token is very volatile and is not bound at all to an email. Actually, sometimes the same device may get a new GCM ID out of the blue. There are many tutorials on GCM's best practices, but Google's is the best way to start. What you should do to prevent problems is store the GCM id of your device in your database, so you can always know which ID is currently associated to your device. GCM may give you new IDs for no reason, so always parse its response properly and store the new ID it may provide, linking it to the device.
GCM doesn't really care which Google account is your app using. That means you can register as many devices as you want with the same account. You have to manage to which devices you push notifications, because the ID is managed by device.
Be careful: there's been some changes with the API lately, so a lot of unnoficial tutorials you find on the internet may point to deprecated methods.
There are other ways of sending notifications, Tokodu and RabbitMQ for example. I wont go over if X is better than Y, since it's not allowed on StackOverflow.
A unique device token is generated for the device, how you use this device token is totally up to you. You can make it unique for the user by, for example, implementing a login system, so that the device token is created and stored in a database (together with the user information). Let's say if two different users uses your application on a shared device, they can both receive notifications on the same device if you allow them to. It depends on your own implementation.
This again depends on how you chose to implement the GCM features on your client and server side. For example, you can choose to store device tokens for each and every device the user has installed the app on, or you can store the latest device token so that notifications are only sent to one of the user's devices.
I hope this helps clear some things up.
Besides good answers above, if you need a push notification solution for a working environment without Internet access, I suggest SignalR of Microsoft, which has already supported Android and Java (you can find out more at GitHub).
Hope this helps little!
I have several apps on Google Play that extensively use GCM and they have all been working very well for quite some time now. But, all of a sudden (Aug 12, 2015), GCM is sometimes returning "NotRegistered" when my server posts a message to it.
This is happening just occasionally but that is often enough to cause big problems as the messaging can no longer be trusted.
The last part of the web response stream is: [{"error":"NotRegistered"}]}
I log the registration id along with the response stream and the registration id that I attempted to use is the same one that has always been used for that phone. My apps get a new registration id when they are first ran so it is necessary for the application to be uninstalled and reinstalled to make the app work again. Has something changed?
Oddly, (or not) the problem has only occurred on phones running 5.1 lollipop. Did I possibly miss another breaking change?
Thanks,
Dean
If you are using GoogleCloudMessaging to register, one possibility is that these users received an upgrade of your app. See the latest edit to Eran's answer here.They will need to re-register with GCM. Consider using the latest registration method using Instance IDs, which has a listener for token refresh when needed.
I am having a weird issue with my app - I have two devices on which I test it. One is 4.4.4, second one 4.0.4. I have made one app which used GCM. It worked without a problem on both phones. Then I made a second one, which had the GCM module copy-pasted from the first one, without any changes.
Now, when I send GCM message to the KitKat phone, it receives it without problems. But I cannot get it on the second phone. It doesn't register it at all (BroadcastReceiver doesn't receive anything). All the while the first app is still working ok. Both phones have the exact apk installed via adb.
I store the GCM registration id in the phone, yet when I run the app on the KitKat, receive an ID, close app, erase data, run it again - I get a different ID second time. This doesn't happen with the second phone, which leads me to believe there has been some kind of error on the side of Google.
I stumbled on this thread, probably dealing with the same issue - GCM not working on Gingerbread, but is working on Ice Cream Sandwich. I tried reinstalling the app, but that didn't help, I am still getting the old (non working) GCM ID.
tl;dr I am unable to receive GCM messages on one phone, while the same app works on another. Probably a problem with registering GCM. Upon reinstallation I am still getting the same GCM ID. How to force Google to get me a new one?
EDIT: The answer below provided the info needed, yet it didn't fix the issue. I will open another thread to avoid misleading title.
I tore my hair out over this for two days... If you are using a device for testing, you need to delete the InstanceID before obtaining the token because once you overwrite your APK it de-registered that InstanceId for some reason... or something along those lines, it ended up working for me. So in your RegisterIntentService class, in the onHandleIntent function do the following:
InstanceID instanceID = InstanceID.getInstance(this);
try
{
instanceID.deleteInstanceID();
} catch(IOException e)
{
e.printStackTrace();
}
instanceID = InstanceID.getInstance(this);
I don't think getting a new registration ID would solve your problem, but here's the way I use to get a new registration ID :
Uninstall the app.
Send a GCM message to old registration ID. Repeat this step until you get a NotRegistered error in the response from Google.
Install the app again, which should re-register to GCM, and this time you should get a new registration ID.
I am using Uniqush to send GCM messages to an app I'm developing, and so far this is working well. However, for obvious reasons I am uninstalling and reinstalling the app as I develop, and it's re-registering for GCM with each new install.
I understand GCM is supposed to handle this automatically:
http://developer.android.com/guide/google/gcm/adv.html#unreg
if a message can't be delivered to a device, it sends an error to the server, which handles it. Unfortunately (even after sending a test push message while the app wasn't installed) I am getting duplicate messages to my device.
Is this a failure in Uniqush, my program, GCM, or some combination of the three?
If notifications are sent to a device while it has the app uninstalled, eventually the duplicates clear.
Given your description, I am unable to reproduce your situation. Here are the steps to try to reproduce it:
Install the test app on my phone and push notification to it using uniqush. Correct. Uniqush's log shows it is successfully delivered and my phone shows the correct result.
Uninstall the test app.
push another notification again using uniqush. Uniqush's log shows the message saying the user is unsubscribed the service: [Unsubscribe][Info] 2012/12/21 19:18:08 [UnsubscribeRequest] RequestId=3992a14e4987e94e3ce16bb7394ee06d2d9e7231 Success
DeliveryPoint=gcm:47e6551857be173b1418e56b63dbdb8cd58c94a2
Then push one more notification to the device, uniqush says it cannot find the device (because it has already been removed.)
Check the database using redis-cli, the device's information is no longer stored. It is successfully unsubscribed.
However, I did found some problem: if you check the error message returned from uniqush from HTTP, then you will see Success on the second notification. I have already fixed this bug in next release. It will be online soon. (If you are interested in that, check the .deb package )
In fact, uniqush is designed to be able to handle such situation so that the user don't need to care about unsubscription, device token/registration id update, etc.
If you have any future question, please feel free to comment or contact me.
When the user uninstalls the app from his android device, it means that the registration_id for C2DM is no more valid. Now, how does the server which sends the push notifications know this. With Apple, there is something called Apple-feedback which lists out all the device-tokens (Android folks, read as registration-id) that are invalid.
Please, help me out with this.
I'm also looking for a sollution to this, so far I've only found one useful sollution which is mentioned here: http://groups.google.com/group/android-c2dm/browse_thread/thread/8e58ed95a0818716
In short: When you SEND the message to the device, send an UID with it (eg. generated on first install). When a message is received in the application, check if the UID is the same, if it is, do you thing (eg create notification), otherwise ignore it, and send a msg to your C2DM server that this Google C2DM registration ID is not valid anymore.
I think that is the reason C2DM are refreshing the Registration ID after random time. If user uninstall the App from his device, he is not not going to update his/her Registration ID either. Then he will not getting any Push Notifications anymore.