Search code examples
androidconnectiongoogle-cloud-messagingtokenuser-management

Android gcm registration database management


I'm trying to build an app using Google Cloud Messaging.

I used the google gcm samples, with almost no modifications, and so far, I can send message between my client app and my personnal little php server. (no code problem here)

In samples, google let a sendRegistrationToServer() method (click here) for me to write when InstanceId is set, but I don't find any clue on the net about how to manage such things.

How should my database look like to handle users and tokens ?

(if someone could provide a little database structure example, it would make my day !)

Since tokens register an app or a phone and users a user (which imply a registration activity to get username and password, and I haven't made it yet), I kind of don't want to mix them in the same table, but I might have misconceptions about the purpose of token.

For instance, I wonder why nobody seem to save the token in SharedPreferences...


EDIT : now I get the whole process :

  1. Registration Activity --> Register your user
    • Send email adress and secured password to your server
  2. Connection Activity --> Connect your user
    • Get a gcm_token and send it to your server and then map it with connection informations

This seems so simple and clear right now, but really didn't back then, so maybe it could help someone else...


Solution

  • The token may change so it is not useful to store it in shared preferences its better to call InstanceID.getToken so you are sure you always get the correct token value. The important thing is to know whether or not you have sent the latest token to your application server, this boolean should be stored in shared preferences.

    You should have a class that extends InstanceIDListenerService in which you implement onTokenRefresh to handle new tokens.

    You should map your app-generated user IDs to tokens, note that a single user may have many valid tokens associated with them. You don't have to store the tokens with your user's username's and passwords but there should be some mapping of user IDs to tokens.

    Eg:

    User (uid, uname, pword)

    Token (uid, token_value)