How to get device token for Android for push notification?

Hello,

I have been reading over and over all of the tutorials for push notifications for both Android and Apple. For the Android how do I get the device token for my app users? How is it sent to me? I thought I would enter a URL to receive it but I do not see.

I was reading the tutorial page on here at: http://www.coronalabs.com/resources/tutorials/all-corona-sdk-tutorials/?category=nativesystem-events and read the one for Android.

And for sending the push notification if I wanted to send it to all of my users and suppose there are 10K of them, I have to add all 10K device tokens in the file to send to?

Thanks,

Warren

I believe it is just automatically handled for you by Corona if you setup correctly everything explained in the blog posting. Corona notifies Google Play when the app is launched and it will provide you with a token through the OnNotification event. That’s more or less how it works on iOS too.

As for the 10k user push, yes more or less. If you are doing it yourself you will have to loop through and send each push notification and you will need to know their tokens to do so. Using 3rd party services does make such a process easier. For example using Parse (and it’s probably similar with Corona Cloud), users can be put into a “channel” and I can send a push to the channel. Parse then handles sending it to all those subscribed to that channel. I can even just send it to everyone.

Thanks for the reply. But the one thing I did not see anywhere is how you “get” the device token? During the setup do you specify a web URL that is posted to when they install the app on their phone? I assume the token is sent to me somehow so I can save it in a table.

I know that once I have the device tokens that I can send a message to them be looping through the tokens like you mentioned. I did read the iOS blog and it does sound a bit more complicated. But does it basically work the same where I would loop through the device tokens to send a message? And is the token sent to me when the app is installed on a phone?

I have a VPS with windows hosting and a mssql database to use for this.

Thanks again,

Warren

I don’t know the specifics of how Corona communicates with Apple/Google, but this is how it works on the code side.

This code sets up the runtime event that receives notifications from the OS.

Runtime:addEventListener( "notification", OnNotification )

This is the listener function that processes any notifications.

local function onNotification(event) if event.type == "remoteRegistration" then -- This device has just been registered for push notifications. -- Store the Registration ID that was assigned to this application by Google or Apple. myRegistrationId = event.token -- Display a message indicating that registration was successful. local message = "This app has successfully registered for push notifications." native.showAlert("Information", message, { "OK" }) -- Print the registration event to the log. print("### --- Registration Event ---") -- -- Here is where you put code to register with your push notification service -- else -- A push notification has just been received. Print it to the log. print("### --- Notification Event ---") -- here is where you respond to a push notification that comes in while the -- app is running. end end  

Now when your application is started, Corona tells Apple or Google this app uses push. Then Apple/Google return a token and the token is passed to your app via the OnNotification listener. You will then get the “remoteRegistration” event. As per the code above, you then save that token.

The Corona code is more or less identical for Apple and Google. Google requires a little more work on the setup though. But once set up, your Corona code can handle either pretty seamlessly. As for sending pushes, I have not looked into how Google expects them to be delivered to them. I am sure it’s much different than how Apple does it. However once setup (server-side code), delivering it from Corona to your server script again will be pretty much the same.

I originally had my own server and was able to send Apple pushes fine. In the end I just decided to use a 3rd party service as it handles the load, the automatic removal of expired tokens, setting up the connection and as mentioned prior, sending multiple pushes.

If you need server code examples for Google push, perhaps someone else will chime in with how in that department.

Wow, that really helps me understand it a lot better! It looks like after the registration is done the first time that I can send the device token to my server by the app itself.  What 3rd party service do you use for push notifications? Does it handle both Android and Apple?

I went with Parse to handle my needs. They do handle Google, however they use there own backend and code for it. So no GCM. The pro of their Google setup is it works with any Android device, including Kindles and Nooks. The con is you need to use their SDK to implement Google push. So that would require Corona Enterprise and creating a plugin. I have not went that far yet to invest in Enterprise. I do plan to try, it’s just an investment I am not ready for yet.

Someone posted back when iOS push was first implemented that they got it working with Urban Airship. However I don’t think they ever explained how. However if you simply want to just do push, Corona Cloud will do that easy enough. Only if you want or need more, then look elsewhere.

The main thing I like about Parse is they give me free reign on how I use the server. At the core, it’s just a database with extra capabilties. I can use it however I like. However more freedom and control requires more planning on how to do things with your app.

Have a look at sample project “Notifications/GooglePushNotifications” that is included with the Corona SDK.  That sample project sends a push notification to itself by using the registration ID that is received from Google upon app startup.  The registration ID is received via the “notification” event listener.  Also have a look at that sample project’s onTap() function on how to set up a JSON packet for sending a notification to Google’s servers.  Note that the registration ID identifies the user on the device and is needed by the JSON packet to deliver a notification to that user.

Anyways, I hope this helps.

I looked at Parse just a few ago and $199 per month is a bit much for me. I see they have a free version too but did not see how many push notifications I get with that. And I’m not going to invest in doing the enterprise version of Corona either at this point. I just want to push some simple messages saying there is new content available on our website or to notify users of upcoming events.

I’m looking at the new corona cloud services but still trying to find more information on how to do this.

Thanks Joshua, I’ll do this.

Warren

Joshua,

I just went through setting up the GooglePushNotifications sample and entered the API key and project number and it worked great! When i ran the app again it gave the same message that the device has just been registered for Google push notifications. How come it did this again? I thought it only needs to register once?

Another question, the googleRegistrationId which is the event.token - is this what i would need to send to my server and save and then use for sending messages?

Thanks,

Warren

I believe you get the token everytime the app starts. You just need to update it if it changes because they can expire.

Corona does *not* register for push notifications with Google on every app startup.  It just provides the registration ID on every app startup to match iOS’ behavior.  Plus, it gives you the added convenience of not having to store this ID to file/database since Corona will do that for you.

And yes, you need to send that registration ID to your server because it is needed to send notifications.  Notice in the GooglePushNotifications sample project that it is used in the network.request() that sends the notification.

I believe it is just automatically handled for you by Corona if you setup correctly everything explained in the blog posting. Corona notifies Google Play when the app is launched and it will provide you with a token through the OnNotification event. That’s more or less how it works on iOS too.

As for the 10k user push, yes more or less. If you are doing it yourself you will have to loop through and send each push notification and you will need to know their tokens to do so. Using 3rd party services does make such a process easier. For example using Parse (and it’s probably similar with Corona Cloud), users can be put into a “channel” and I can send a push to the channel. Parse then handles sending it to all those subscribed to that channel. I can even just send it to everyone.

Thanks for the reply. But the one thing I did not see anywhere is how you “get” the device token? During the setup do you specify a web URL that is posted to when they install the app on their phone? I assume the token is sent to me somehow so I can save it in a table.

I know that once I have the device tokens that I can send a message to them be looping through the tokens like you mentioned. I did read the iOS blog and it does sound a bit more complicated. But does it basically work the same where I would loop through the device tokens to send a message? And is the token sent to me when the app is installed on a phone?

I have a VPS with windows hosting and a mssql database to use for this.

Thanks again,

Warren

I don’t know the specifics of how Corona communicates with Apple/Google, but this is how it works on the code side.

This code sets up the runtime event that receives notifications from the OS.

Runtime:addEventListener( "notification", OnNotification )

This is the listener function that processes any notifications.

local function onNotification(event) if event.type == "remoteRegistration" then -- This device has just been registered for push notifications. -- Store the Registration ID that was assigned to this application by Google or Apple. myRegistrationId = event.token -- Display a message indicating that registration was successful. local message = "This app has successfully registered for push notifications." native.showAlert("Information", message, { "OK" }) -- Print the registration event to the log. print("### --- Registration Event ---") -- -- Here is where you put code to register with your push notification service -- else -- A push notification has just been received. Print it to the log. print("### --- Notification Event ---") -- here is where you respond to a push notification that comes in while the -- app is running. end end  

Now when your application is started, Corona tells Apple or Google this app uses push. Then Apple/Google return a token and the token is passed to your app via the OnNotification listener. You will then get the “remoteRegistration” event. As per the code above, you then save that token.

The Corona code is more or less identical for Apple and Google. Google requires a little more work on the setup though. But once set up, your Corona code can handle either pretty seamlessly. As for sending pushes, I have not looked into how Google expects them to be delivered to them. I am sure it’s much different than how Apple does it. However once setup (server-side code), delivering it from Corona to your server script again will be pretty much the same.

I originally had my own server and was able to send Apple pushes fine. In the end I just decided to use a 3rd party service as it handles the load, the automatic removal of expired tokens, setting up the connection and as mentioned prior, sending multiple pushes.

If you need server code examples for Google push, perhaps someone else will chime in with how in that department.

Wow, that really helps me understand it a lot better! It looks like after the registration is done the first time that I can send the device token to my server by the app itself.  What 3rd party service do you use for push notifications? Does it handle both Android and Apple?

I went with Parse to handle my needs. They do handle Google, however they use there own backend and code for it. So no GCM. The pro of their Google setup is it works with any Android device, including Kindles and Nooks. The con is you need to use their SDK to implement Google push. So that would require Corona Enterprise and creating a plugin. I have not went that far yet to invest in Enterprise. I do plan to try, it’s just an investment I am not ready for yet.

Someone posted back when iOS push was first implemented that they got it working with Urban Airship. However I don’t think they ever explained how. However if you simply want to just do push, Corona Cloud will do that easy enough. Only if you want or need more, then look elsewhere.

The main thing I like about Parse is they give me free reign on how I use the server. At the core, it’s just a database with extra capabilties. I can use it however I like. However more freedom and control requires more planning on how to do things with your app.

Have a look at sample project “Notifications/GooglePushNotifications” that is included with the Corona SDK.  That sample project sends a push notification to itself by using the registration ID that is received from Google upon app startup.  The registration ID is received via the “notification” event listener.  Also have a look at that sample project’s onTap() function on how to set up a JSON packet for sending a notification to Google’s servers.  Note that the registration ID identifies the user on the device and is needed by the JSON packet to deliver a notification to that user.

Anyways, I hope this helps.

I looked at Parse just a few ago and $199 per month is a bit much for me. I see they have a free version too but did not see how many push notifications I get with that. And I’m not going to invest in doing the enterprise version of Corona either at this point. I just want to push some simple messages saying there is new content available on our website or to notify users of upcoming events.

I’m looking at the new corona cloud services but still trying to find more information on how to do this.

Thanks Joshua, I’ll do this.

Warren