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 B4P 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 (C2DM) framework and tutorial

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-31-2011, 02:49 PM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,689
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default Android push notification (C2DM) framework and tutorial

Background

Google provides a service named Android Cloud to Device Messaging Framework (C2DM) 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).

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.
At this point it is recommended to read the formal documentation about C2DM to better understand the flow: Android Cloud to Device Messaging Framework - Google Projects for Android

Configuration and installation instructions

First you should sign up with a Google account in the above link.
Once your account is activated (from my experience it took several hours) you can start sending messages.
During signup you will need to enter account mail address (sender id) and the application package name.

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.
Two variables should be set in the Main activity, under Sub Process_Globals to match the package name and sender id you previously registered.
The application package name should also be set to the same package name (Project - Package Name).
Code:
    'both these fields should be set to match your application package and SenderId.    
Package = ""
SenderId = 
""
You should now edit AndroidManifest.xml which is located under Objects folder and replace all occurrences of anywheresoftware.b4a.samples.push with your package name.
With the new manifest editor you should instead add the code posted here (and make sure that "do not overwrite manifest file" is NOT selected): http://www.basic4ppc.com/forum/basic...html#post80463

Sending messages desktop tool
Open config.txt and update the two fields:
Code:
sender_mail_id=
sender_mail_password=
You should enter the same mail address and the password for this account.

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.



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.

C2DM limitations
- Only Android 2.2 and above are supported.
- Message length should not exceed 1024 bytes. This means that if you need to send more data then the push message should only be used to notify the device that it should connect to some server and grab the data.

Tips
- If the push notification is a critical component in your application then you should set the android:minSdkVersion field in the manifest file to 8 so older devices will not be able to download the application.
Attached Files
File Type: zip Desktop.zip (6.5 KB, 238 views)
File Type: zip Device.zip (7.9 KB, 238 views)
File Type: zip web_service.zip (841 Bytes, 221 views)
Reply With Quote
  #2 (permalink)  
Old 07-31-2011, 03:54 PM
Basic4ppc Veteran
 
Join Date: Jun 2011
Posts: 298
Default

Quote:
Originally Posted by Erel View Post
Background

Google provides a service named Android Cloud to Device Messaging Framework (C2DM) 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).
Although It may not be related to this, I'd love to be able to understand how it can be a "push" - how would an Android device magically know there is something? Surely it must check at intervals to see if there is something.
Reply With Quote
  #3 (permalink)  
Old 07-31-2011, 04:18 PM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,689
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default

See the discussion starting from post #7: http://www.basic4ppc.com/forum/basic...lications.html
Reply With Quote
  #4 (permalink)  
Old 08-18-2011, 06:13 PM
Junior Member
 
Join Date: Jun 2011
Location: Tbilisi, Georgia
Posts: 20
Default C2DM sign up delay.

C2DM, this seems exactly what we need for our app. I have signed up about 8 days ago with 1 account and 3 days ago with another one to get access to the services, I guess it was a google automated email that said "you will have access within the next day or so". Do, anybody know how to speed up the process? Are there any specific criterias that I would met for getting an account? I know it has nothing to do with B4A business, but I am looking for clues if there is someone with some info to get an account. As this is something we would really need! I have been trying to find answers else where... But nothing, this is my last resort.....

Cheers,
Petri

PS. Yes, I am looking into xtify as well but would prefeer google DS.
Reply With Quote
  #5 (permalink)  
Old 08-18-2011, 06:38 PM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,689
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default

When I registered it took about 2 hours for my account to be active. I don't have a clue why it takes longer now.
Reply With Quote
  #6 (permalink)  
Old 09-02-2011, 05:45 PM
Knows the basics
 
Join Date: Mar 2011
Location: Denver, CO USA
Posts: 91
Smile SUCCESS: xxxxxxxx

WOW! It worked (my first server app)! What does "success: xxxxx"
mean ? That the message arrived at the Device ? That the message
was sent only ? That the message was read ? What does the number
mean ?
Thank you Erel,
Jerry
Reply With Quote
  #7 (permalink)  
Old 09-02-2011, 07:26 PM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,689
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default

Congratulations!

Success means that Google server responded with response code 200 and without any errors: Android Cloud to Device Messaging Framework - Google Projects for Android
This means that your message is waiting to be sent. Once the device is online it will be sent.
The number is the message id assigned by Google server.
Reply With Quote
  #8 (permalink)  
Old 09-10-2011, 12:01 PM
Newbie
 
Join Date: Sep 2011
Posts: 1
Default code sample

@myriaddev
Kindly give me your example to test with, as every time i got 401 unauthorized error
Reply With Quote
  #9 (permalink)  
Old 09-11-2011, 05:43 AM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,689
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default

For some reason you appear as an unlicensed user in the forum. Please contact support@basic4ppc.com in order to fix it.
Reply With Quote
  #10 (permalink)  
Old 10-03-2011, 07:59 AM
Knows the basics
 
Join Date: Jan 2011
Posts: 68
Default

Hello,

When i tried to send a message, i got this result :

Code:
java.lang.RuntimeException: java.io.IOException: Server returned HTTP response
 code: 
401 for URL: https://android.apis.google.com/c2dm/send
        at anywheresoftware.b4a.c2dm.C2DM.sendMessageTo(C2DM.java:
66)
        at anywheresoftware.b4a.c2dm.C2DM.sendMessageTo(C2DM.java:
63)
        at anywheresoftware.b4a.c2dm.C2DM.main(C2DM.java:
228)
this is a ClientLogin Auth token problem but i don't understand what i must do.

Do you know ?
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 FTP tutorial Erel Basic4android Getting started & Tutorials 104 Yesterday 05:51 PM
Android multitouch tutorial Erel Basic4android Getting started & Tutorials 10 05-02-2012 09:17 AM
Android Charts Framework Erel Basic4android Getting started & Tutorials 27 03-20-2012 07:37 AM
Android FTP Tutorial klaus German Tutorials 1 02-04-2012 04:01 PM
Push notification (Google C2DM) framework - BETA Erel Additional libraries and official updates 11 07-29-2011 11:57 AM


All times are GMT. The time now is 09:58 AM.


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