Reputation: 406
I think I misunderstood something. Sorry for that.
I'm trying to implement push messages to my app.
In the GCM web console ("API Access") I've added a project and retrieved an API key which seems to the same for the whole project (whatever project means in this context) and a clientID ending with apps.googleusercontent.com for each of my apps.
Then I simply copied the gcm-client example from google, removed the compiling errors, added the libs and made it run on phone, but not on the emulator. Here a senderID is required, which seems to be the "projectid" from the API access Url, right?
So far, so good.
For implementing the server I need an API key. This is the one out of the GCM API Project page at the bottom ("Simple API Access"). Furthermore I need a registrationID. Seems to be having calculated by my app at the very first run, right? So I need to copy this ID from my app into my server?
But is there a way to retrieve the registrationIDs from my app directly via GCM? E. g. when I've got my app in the play store, how do I get the IDs of devices that downloaded my app? Sorry, but I'm kind of confused by the whole GCM mechanism :(
Edit: How does the process of un-registration work. How do I learn, that a phone does not need any messages any more, what could happen under numerous various reasons?
Upvotes: 0
Views: 2791
Reputation: 393771
There is no way for the server to get the Registration IDs from GCM (except for the special case of getting Canonical Registration ID in a response from GCM, but in order to get that your server must already know the older Registration ID used by your app on that device). Each instance of your app, installed on a device, must send the Registration ID to your server, and your server should save these IDs in some persistent store (DB, file, etc...).
If your app un-registers from GCM explicitelly, it can notify your server that it un-registered from GCM (in a similar way to the call in which you send the Registration ID to the server), and your server will delete its Registration ID. However, the more common case is that the app is un-registered implicitelly by being uninstalled from a device. In that case it can't notify your server that it was uninstalled. When your server first sends a notification to a device from which the app was uninstalled, it will get a successfull response (since GCM doesn't know yet that the app was uninstalled), but later attempts to send notifications to that device will result in NotRegistered
error, which tells the server to delete the relevant Registration ID.
Upvotes: 1
Reputation: 699
First, you should create your GCM instance:
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(HomeScreenActivity.this);
Then, you should obtain you registrationID like this:
String regId = gcm.register("xxxxxxx"); // xxxxxx is your ProjectNumber wich can be found in your GCM console
Store this regId somewhere in shared preferences so you can reuse it.
Upvotes: 0
Reputation: 7108
Simply have you app send the regID to your server as soon as it gets is.
Then on your server you can store it however you like, a list, a flat file, database or whatever.
I have used this approach to store multiple regIDs in MySQL and have a 'master' phone know all the server id's to all the phones by simply sending the id's together with a message. As oppose to having the phone know the regIDs which would be impossible to manage as these may change over time.
With this setup I could fairly easy let the master phone multicast a message to all the other phones that it was responsible for.
Hope this makes sense.
Upvotes: 0