I'm looking to create for my app some authentication. This will basically consist of a user registering (preferably with their google account, although not necessary) and providing a username that is then sent to my server and then having the ability to log in. I then from there would like to be able for the client app to communicate with my server by sending data and receiving push notifications via GCM (google cloud messaging). It is important that the server is able to send a push notification directly to a certain user not to all registered devices.
Would someone be able to point me in the right direction for what I must do? I've had a look at a few tutorials but what I've seen has always sent messages to all registered devices not to a specific user.
Some extra information about the app:
-The client app (user) will need to send a piece of information to the server. This information will also include the user that it needs to be sent to. Once the server receives the piece of information it should send a push notification through GCM to the specified user's client app.
I'm clueless as to how I need to go about setting up the authentication system, any help would be very appreciated!
Thank you very much,
Daniel
Offcourse you can send push notifications to selected users. This process would roughly comprise the following steps:
1) send the User's GCM registration id to your own server along with a unique user id (what ever id you have assigned to that user; could be email or sim id or something like that). Save it in your database.
2) To send Push notifications to selected users, choose users from the database based on some criteria, retrieve their GCM registration ids from the database and forward them the GCM server.
but there is one important thing you must handle. GCM registration ids could change e.g if user uninstalls and re-installs the application. You must update you corresponding database entry in that case.
Related
I'm working on a new app and I want to give the possibility to my user to use more then one device.
The app can receive push notification, my question is how to save more device/token for each user, this for send push notification to all the user's devices.
My idea is to retrive "some device ID" and then store a id/token for each device but I don't know which device ID I can use...
Another idea is to create a random id the first time and then send this id with token, this work (partially) until the user clear application data...in this case I need to resend a new id (no problem) but I can't know if I can remove the old id/token
any solution?
You don't need "some device ID"; the push token is the device ID. You need to ensure that your backend database can associate more than one push token with a given user account. This is typically through a one->many relationship between user "users" table and your "installations" table.
You can't rely on your app to tell your backend to remove an old token, since the user may simply delete your app and it doesn't get a chance to communicate with your back-end.
The APNS service will deliver feedback as to which push tokens were invalid (I assume that the equivalent Android service also has something like this). You use this feedback to purge invalid installations from your installations table.
I am developing an app which has login functionality.I am generating only one gcm registration ID per app. If other user sign's in in same app he will start receiving notifications intended for the previous user.
How to handle this situation, so that each user will get notification intended for his/her?
Should I generate gcm reg id for each user?
What is the standard way to handle this situation?
You could try the following things:
When the user logs off, send a request to delete the token on your server, and erase it on your app;
Once the user logs off, you could simply remove the association of "User ID" to "GCM Token" (Or Registration ID) on your server. And when someone logs in again, you make a new association to that Token.
The GCM Token is app specific, but the association you make on your server is totally up to you.
And I can't stress it enough, the token generated by GCM is APP SPECIFIC. If your user logs in on multiple devices, your server should handle that, associating the user ID to multiple Registration ID Tokens.
An ID issued by the GCM servers to the Android application that allows
it to receive messages. Once the Android application has the
registration ID, it sends it to the 3rd-party application server,
which uses it to identify each device that has registered to receive
messages for a given Android application. In other words, a
registration ID is tied to a particular Android application running on
a particular device.
And from the docs, your server should also:
Before you can write client apps that use GCM, you must have an
application server that meets the following criteria:
Able to communicate with your client.
Able to send properly formatted requests to the GCM connection server.
Able to handle requests and resend them using exponential back-off.
Able to store the API key and client registration tokens.
Able to generate message IDs to uniquely identify each message it sends. Message IDs should be unique per sender ID.
EDIT: You can read more about GCM here.
For further reading, you could also read more on Device Group Messaging.
You can also download some code samples here.
You should store regID as per user.
This is because there is test cases that user1 logs out and user2 logs in. In that case if you have stored regID app specific and not binding with user then user2 will also get notification.
So you need to store regID as per user , app and as well as device specific.
Of course yes every time someone downloads your app, your app should register them with the GCM and be given a reg token, this needs to be sent to your app server, on your app server you need to sort out who you are sending the notification too. you should probably send some login info and reg token when they login so you can identify each person, then write some php or Jquery to send to individual users or all. you should also send the reg token every time they login as these can change without warning.
Your app server should be able to handle removing unused reg tokens and adding new ones
Each android device generate a Unique GCM reg id. push notification from server side will send according to the device id. So at Login time you can send GCM reg id to your server. So from server side push notification will send to device.., that is actual functionality.
Hope this will help
I am learning how to implement the GCM both from client side (Android) and from server side (ASP.NET). I spent a bit of time on reading the whole google documentation related to GCM and I also tried the sample that they provided.
Because I need to integrate the GCM in an already existing app, I would like to know some specific stuff.
As I got it, the registration id is a token which ties the app on a specific device to the GCM service and the app server; so, the backend can send downstream messages directly to that device.
In my scenario, I could have multiple users who can use the app on the same device, that means they need to login in the app and they have an account on a database in the server side. Do I need to store a different registration id for each user on that device? Or still the registration id refers to only the app?
And what about the same scenario but distributed on multiple device, because a user can have multiple devices?
Thanks in advance.
This relates a lot to the context of your app and what you want to do with notifications
Having one registration ID per device will be okay.
I manage my multiple users by using subscription tags handled by my server.
So take the scenario if you have a sports app:
User A & User B share the same device. Each user subscribes to a tag.
User A is subscribed to two tags (Basketball & Football)
User B is subscribed to two tags (Tennis & Basketball)
When User A logs out you clear the tags associated with that user and when User B logs in you fetch his/her tags.
Your server knows notifications to send to the device based on the tags the user has subscribed for.
Multiple device scenario:
The same goes, when your user logs In you get their tags. The device also has its own token from GCM.
The registration id is the app's id, it may be changed if app version was updated. Your push notification will be sent on device with your app, regardless user account. So every user on every device will receive your notification, you must store only one refistration id per device.
For multiple users: If your messages are user specific, you would want to retrieve the user's token and subscribe to it only while they are logged in. When you switch users remove/unregister the previous token, then save/register the new user's token.
You should not only do this if you have multiple users per device, but when a user signs out. This will prevent user-specific messages being shown at wrong times to wrong users.
For multiple devices: it sounds like you are looking for Device Group Messaging.
With device group messaging, app servers can send a single message to multiple instance of an app running on devices belonging to a group. Typically, "group" refers a set of different devices that belong to a single user.
This is also nice because of the collapse_key. When one of the devices on the same account opens a notification, it will dismiss the notification on the other devices..
For the application that I am working I need to integrate Google Cloud Messaging. After playing a little with different examples, I was able to send and receive a notification on my deivce.
However, I have come across an interesting situation. As I know (please correct me if I'm wrong), the registration_id is issued per device and per application.
The application that I'm working supports login functionality. When the application is installed and the user logs in for the first time (let it be "UserA"), I request the registration_id from GCM which then I send it to my server.
Now imagine that UserA logs out and gives his device to some UserB to log in. With other words, UserB logs in using UserA's device.
The problem is that if meanwhile UserA receives a notification, UserB will be able to intercept it. And if UserB receives a notification, he won't be able to receive it.
This seems normal because the registration_id is per device and per application, but it does not seem reasonable for my case.
So I'm asking if there is a way I could make the registration_id to be dependent of some user id (besides the device and app)? Or how could I make such that the logged in user to receive only his own notifications?
Yes its true that you have one google registration ID for the app per device.
But you can always register and unregister users at your own Server that will actually send messages to GCM and GCM will send this to the registered devices.
Define some interfaces for your Server like registerOnServer and unRegisteronServer , send some unique value for each user on this interface.
So, in your case, when A use Log ins , regitration is done on GCM first and register the user on your Server with registerOnServer inteface and while the user is logged in send notifications pertaining to him to GCM to be send to the device.
When A logs out, unregister him using unRegisterServer and do not send any messages from your Server to GCM as A is unregistered.
So,now if B Logs in even with the same device , register him at your Server and send his messages.
This shall solve your problem!
I am trying to write an application which notifies a cellphone using GCM. I was able to implement basic functionality, but there is one question I'm still unsure of
As I understand, the flow goes like this:
Device start up
Device registers, obtains registration id
Server gets the knowledge of registration id on device
Server is using it's API key + registration id to send payload to device
Device receives payload.
One thing which is unclear to me is: what is a proper methodology to implement step #3?
I could not find any way to obtain a list of registered devices. Lets imagine, I am using GCM for a purpose of posting to a known device, and I could email that ID to myself and then register it on the server manually. But as I understand, it expires.
So, it there an expectation that I have to keep re-registering and somehow notifying server every time it happens?
Yes, your app on device must send registration ID to your server application in order for server to be able to send GCM messages. See third point in Enabling GCM.