0

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...

Dan Chaltiel
  • 7,811
  • 5
  • 47
  • 92

2 Answers2

1

A gcm should be associated with a user object. Once a user gets his gcm token, its a user token and not device token as you might think.

For a simple database integration within your app i suggest you to use Parse.com and their android guide for getting started.

The token is getting authorized each time you open the app, and it might change along time - thats why nobody saves it and reusing the same token evreytime

Juvi
  • 1,078
  • 1
  • 19
  • 38
  • So for you the proper way is 1) register the user as name-password 2) associate the token as a field of the user, am i right ? – Dan Chaltiel Oct 25 '15 at 09:21
  • I'm looking at parse.com right now, and the fact I don't understand anything to their website probably means it's overcomplicated for me and my project... Maybe I'll try much later – Dan Chaltiel Oct 25 '15 at 09:31
1

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)

Arthur Thompson
  • 9,087
  • 4
  • 29
  • 33
  • I have a InstanceIDListenerService, almost the same as in google samples. Thanks for the database structure ! I'm building the connection/registration now, but what I don't understand is that I need a token to communicate my name and password, but I need a account (name and password) to map with the token. Which one come first ? – Dan Chaltiel Oct 25 '15 at 10:37
  • Juvi said a token is a user and not a device, but you say a user can have several tokens, this is confusing... – Dan Chaltiel Oct 25 '15 at 10:41
  • You can think of a token as representing a single install of your app on a single device. If the user installs your app on multiple devices then the user will have multiple tokens, one for each install. You should have users with application specific IDs before you generate your InstanceID token. That way when you are ready to send a message to a user, you look up the user's ID then send messages to all the tokens associated with that user. – Arthur Thompson Oct 25 '15 at 10:43
  • Sorry for not understanding the whole thing, I'm still a beginner. When I register a user to my server, I need the server to send a response to the user (like "congrats, you're registered"), so I need the token no ? I understand about the token being an install now, thanks ! – Dan Chaltiel Oct 25 '15 at 10:53
  • Within your app your user has an ID, which is separate from GCM. When you get an InstanceID token you send it to the server and the server will associate the token with the appropriate user ID. How you send the token to the server is a different question. – Arthur Thompson Oct 25 '15 at 10:57
  • @ArthurThompson you are right, but i still think that a user should have a signle unique gcm token. Once theres an updated token associated with user, you'll be able to reach him no mater what. – Juvi Oct 25 '15 at 11:15
  • Single token per install, but multiple installs should and does result in different tokens. User IDs should be maintained separately by the application. – Arthur Thompson Oct 25 '15 at 11:19
  • Great, I think I understand now, thanks ! Additionnaly, during my researches, I found about `AccountManager`, seems very interesting ! – Dan Chaltiel Oct 25 '15 at 12:07