Download the free trial version
Basic4android Video
Features
Tutorials and manuals
Showcase
Screenshots

Go Back   Android Development Forum - Basic4android > Basic4android > Basic4android Getting started & Tutorials
Documentation Wiki Register Members List Windows Mobile Search Today's Posts Mark Forums Read

Basic4android Getting started & Tutorials Android development starts here. Please do not post questions in this sub-forum.

Android push notification (GCM) framework and tutorial

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-03-2012, 09:17 AM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 25,792
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default Android push notification (GCM) framework and tutorial

Background

Google provides a service named Google Cloud Message (GCM) which allows developers to send data to their applications running on Android devices. Unlike most solutions which involve polling some server, this service is a "push" service (similar to SMS messages).

GCM replaces the previous service named C2DM. This framework is a modified version of the previous C2DM framework that works with the new service.
Note that new GCM service is simpler than C2DM and doesn't require a special approval from Google.

Developers should implement three components in order to use this service.
Client code - The client code which is responsible for registering the specific device and application with Google service and is responsible for handling new messages that arrive.

Web server - When the device registers with Google service it receives a registration id that allows you, the account holder, to send messages to your application running on the user device. The device needs to send this id to a web server that you can access.

Messages sending tool - This is a command line tool that is responsible for fetching the user id from the web server and then sending the message to the device. This is done by sending a request to Google servers.

The attached framework includes these components.
It is recommended to read the formal documentation about GCM to better understand the flow: GCM Architectural Overview | Android Developers

Configuration and installation instructions

In order to use GCM service you need to get two values: API key and project id.
This is done by following the steps mentioned here: GCM: Getting Started | Android Developers up until the "Install the Helper Libraries" section (there is no need to install anything).

Client code
In order to get it working, we will modify the device example application. Later you can integrate this code in your own application.
1. SenderId variable should be set in the Main activity, under Sub Process_Globals to match project ID you previously received.

2. You should add the following code to the manifest editor (this is already set in the attached example):
Code:
'C2DM Permissions
AddManifestText(<permission android:name="$PACKAGE$.permission.C2D_MESSAGE" android:protectionLevel="signature" />)
AddPermission($PACKAGE$.permission.C2D_MESSAGE)
AddPermission(com.google.android.c2dm.permission.RECEIVE)
' Push Service Receiver Attribute
SetReceiverAttribute(PushService, android:permission, "com.google.android.c2dm.permission.SEND")
' Service Receiver Text
AddReceiverText(PushService,
<
intent-filter>
<action android:name=
"com.google.android.c2dm.intent.RECEIVE" />
<category android:name=
"$PACKAGE$" />
</
intent-filter>
<
intent-filter>
<action android:name=
"com.google.android.c2dm.intent.REGISTRATION" />
<category android:name=
"$PACKAGE$" />
</
intent-filter>)

Sending messages desktop tool
1. Open config.txt and update the api_key field.

You are now ready to test it!
Run the B4A project, enter a name in the text field and press on the Register button. You should see a toast message saying Registration successful. Messages are also printed to the logs. It can take several seconds.
This means two things. Your device has registered with Google servers and also that the registration id was sent to the developer web service (currently configured with the service hosted here) with the specified name.
The name will be used to reference the device. Note that each device must have a unique name.

Now go to the command line tool folder and open a command window (this can be done by pressing Shift together with right clicking on the folder icon).
The sending messages tool is a java app. For your convenience a batch file is included to run this tool. Note that this app can run from Linux or Mac as well.

The sending tool expects several arguments:
Code:
send <device name> <message text> [<collapse_key> [<delay while sleeping>]]
Device name is the name that you previously chose in the client app.
Message text is the text that will be sent. Remember to wrap it with quotes if it contains spaces.
The last two arguments are optional. See the formal documentation for more information about them.
Instead of 'send' command, you can use 'SendTextFile'. It is the same as send, however the second argument is the path to a text file that will be sent.
Another command is 'GetAll'. It will display all the registered device names (this command is not available in the web service hosted here.

The command line tool can be automated. It returns an exit code of 0 if the operation succeeded and 1 otherwise.

Try sending a message to your device. It should arrive to the device almost immediately.



Note that the batch file name is b4a_gcm.bat.

Handling of messages is done in Sub MessageArrived in PushService module.

Web server configuration
The device application and the sending messages tool are preconfigured to use the web server hosted here. This server should only be used during development.
The web server is built from a PHP script which communicates with a MySQL database.
In order to host it yourself, you should upload it to a server that supports PHP and create a database and user that is able to access the database.
The script creates the table on the first run.
You will need to edit the PHP script and set the database name / user / password.
There are additional two passwords in this script: devicepassword and serverpassword. The device password should match the DeviceBoardPassword field in the device application and the serverpassword should match the server_password field in the sending messages tool configuration. Both these passwords should only contain letters and numbers.
You should also change the board url value in the desktop tool and device app to match the location of your hosted php file.
Attached Files
File Type: zip Device.zip (8.3 KB, 387 views)
File Type: zip web_service.zip (841 Bytes, 341 views)
File Type: zip Desktop.zip (5.7 KB, 326 views)
Reply With Quote
  #2 (permalink)  
Old 07-06-2012, 10:30 PM
Newbie
 
Join Date: Jun 2012
Posts: 8
Default

Hi,

When I try to compile the example, I get the following error:

Compiling code. Error
Error parsing program.
Error description: Unknown type: reflector
Are you missing a library reference?
Occurred on line: 43
Dim r As Reflector

What am I missing?

Thanks in advance
Reply With Quote
  #3 (permalink)  
Old 07-06-2012, 10:39 PM
NJDude's Avatar
Basic4android Veteran
 
Join Date: Mar 2011
Location: [Classified]
Posts: 2,567
Default

You are missing the Reflection library
__________________
Resources for Newbies: Documentation - Libraries - Forum Search - Chat Room

Do you find my samples useful?

no donation is too small, no donation is too big.
Reply With Quote
  #4 (permalink)  
Old 07-06-2012, 10:55 PM
Newbie
 
Join Date: Jun 2012
Posts: 8
Default

Damn it!

Ok, I got it to compile and run on the emulator.

Now two issues:

1.- I dont see any ToastMessage saying that I got registered
2.- When I try to send a message, Java complains that I am missing anywheresoftware.b4a.c2dm.C2DM

Any ideas?

Thanks in advance
Reply With Quote
  #5 (permalink)  
Old 07-06-2012, 11:30 PM
Newbie
 
Join Date: Jun 2012
Posts: 8
Default

Ok,

I got it running on my Atrix4G. First I got an INVALID SENDER Toast Message, After changing the senderId to the email address used while creating the API Project, it was able to register.

But I'm still getting the error that anywheresoftware.b4a.c2dm.C2DM can't be found whenever I try to send a message.

Thanks in advance
Reply With Quote
  #6 (permalink)  
Old 07-07-2012, 10:03 AM
Junior Member
 
Join Date: Dec 2010
Posts: 35
Default

Quote:
Originally Posted by choliz View Post
Ok,

I got it running on my Atrix4G. First I got an INVALID SENDER Toast Message, After changing the senderId to the email address used while creating the API Project, it was able to register.

But I'm still getting the error that anywheresoftware.b4a.c2dm.C2DM can't be found whenever I try to send a message.

Thanks in advance
There is an error in the b4a_gcm.bat. You have to change jar file to b4a_gcm.jar
Reply With Quote
  #7 (permalink)  
Old 07-07-2012, 03:46 PM
cutstone's Avatar
Junior Member
 
Join Date: May 2012
Posts: 15
Default

Thanks for the guide Erel.

I just succeed changing my project from C2DM in to GCM - now it finally works. However, I did have some troubles to figure out what IDs to place where. Searching on the web gave me the answers, so in case others are struggling with the same issue, I'd like to share. From the C2DM-discussions, people had asked for native PHP code to send messages. I found a piece of code from Stackoverflow that serves my need.

First issue was the SenderID. In the description was mentioned 'Project ID'. When I look the Google APIs Console/Dashboard, it states that Project ID is something like 'cutstone.dk:myproject'. This is not the project ID to user in the SenderID variable in your B4A code. Instead, look at the number in your browsers URL: It says something like /#project:523812998263. The 12-digit number is your SenderID.

Next issue was the API-key. Googles documentation states you have to generate a Server Key as you API-key. That didn't work for me, but as mentioned several places on the web, Google might have a bug in the documentation. Instead the Browser key works for me.

In my PHP-code, I call enclosed code to send a Push message to my phone device.

This might be obvious for many of you guys, but for me it took quite some hours to figure out. BTW currently this PHP code sent an empty message. I didn't dig into this, because I just needed a Push message to know my device need to contact my server.
Attached Files
File Type: zip sendGCM.zip (700 Bytes, 152 views)

Last edited by cutstone : 07-27-2012 at 06:51 PM. Reason: Spell bugs
Reply With Quote
  #8 (permalink)  
Old 07-08-2012, 06:45 AM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 25,792
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default

Thank you for sharing this script.

Quote:
There is an error in the b4a_gcm.bat. You have to change jar file to b4a_gcm.jar
Fixed.
Reply With Quote
  #9 (permalink)  
Old 07-16-2012, 10:45 AM
Newbie
 
Join Date: Jun 2012
Posts: 2
Default

Quote:
Originally Posted by cutstone View Post
Thanks for the guide Erel.

I just succeed changing my project from C2DM in to GCM - now it finally works. However, I did have some troubles to figure out what IDs to place where. Searching on the web gave me the answers, so in case others are struggling with the same issue, I'd like to share. From the C2DM-discussions, people had asked for native PHP code to send messages. I found a piece of code from Stackoverflow that serves my need.

First issue was the SenderID. In the description was mentioned 'Project ID'. When I look the Google APIs Console/Dashboard, it states that Project ID is something like 'cutstone.dk:myproject'. This is not the project ID to user in the SenderID variable in your B4A code. Instead, look at the number in your browsers URL: It says something like /#project:523812998263. The 12-digit number is your SenderID.

Next issue was the API-key. Googles documentation states you have to generate a Server Key as you API-key. That didn't work for me, but as mentioned several places on the web, Google might have a bug in the documentation. Instead the Browser key works for me.

In my PHP-code, I call enclosed code to send a Push message to my phone device.

This might be obvious for many of you guys, but for me it took quite some hours to figure out. BTW currently this PHP code sent an empty message. I didn't dig into this, because I just needed a Push message to know my device need to contact my server.
I got the same no message error. In the sendGCM file you need to change the fields array for data from "message" to "data" then it will shoot the message.
Reply With Quote
  #10 (permalink)  
Old 07-23-2012, 01:30 AM
Junior Member
 
Join Date: Jul 2011
Posts: 21
Default

Hi,Erel

When I try to compile the example, I have some question:

I can get the RegistrarID,but when i send to My server .It can`t Work!
And then ,I used some GCM client example APP to My server.It`s Working!

Your example like to use Registration by C2DM,not use GCM?

In C2DM SenderID use Google Mail
In GCM SenderID use Project ID(From the Google API console)

Your example just can use Google Maill,can`t use Project ID

And I have no idea to fix your example code
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Android push notification (C2DM) framework and tutorial Erel Basic4android Getting started & Tutorials 130 03-28-2013 12:30 PM
Push Notification Framework Problem Bill Norris Basic4android Updates and Questions 5 04-19-2012 10:44 AM
Push Notification Framework Bill Norris Basic4android Updates and Questions 1 04-15-2012 03:08 AM
Android push notification (C2DM) framework JATAIDE Basic4android Updates and Questions 2 11-20-2011 10:07 AM
Android push notification (C2DM) framework JATAIDE Basic4android Updates and Questions 2 11-17-2011 07:36 PM


All times are GMT. The time now is 10:01 PM.


Powered by vBulletin® Version 3.6.12
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0