Developing Push Notifications (C2DM) for
Android
Vijai Co-FounderAdhish Technologies, Sweet’N’Spicy apps
What is Push Notification?
Without Push Notification
With Push Notification
Flow
Before we start
It works on Android SDK 2.2+. You need a Google account
Implementing with Device is easier than Simulator
Step 1
Register your email address for Google C2DM.
http://code.google.com/android/c2dm/signup.html
Step 2 – Manifest Changes Permissions to Send/Receive Messages
<manifest package="com.push.app“…>
<!--Permission to register and receive message -->
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<!--Make sure only com.push.app will receive the messages and registration result -->
<permission android:name="com.push.app.permission.C2D_MESSAGE"android:protectionLevel="signature" />
<uses-permission android:name="com.push.app.permission.C2D_MESSAGE" />
Step 2 – Contd. Receiver for Messages
<application...> <receiver android:name=".CustomC2DMReceiver" android:permission="com.google.android.c2dm.permission.SEND"> <!-- Receive the actual message -->
<intent-filter><action android:name="com.google.android.c2dm.intent.RECEIVE" /><category android:name="com.push.app" />
</intent-filter> <!-- Receive the registration id -->
<intent-filter><action android:name="com.google.android.c2dm.intent.REGISTRATION" /><category android:name="com.push.app" />
</intent-filter></receiver>
</application></manifest>
Step 3 – Receiverpublic class CustomC2DMReceiver extends BroadcastReceiver {
@Overridepublic void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("com.google.android.c2dm.intent.REGISTRATION"))
{handleRegistration(context, intent);} else if (intent.getAction().equals(
"com.google.android.c2dm.intent.RECEIVE")) {handleMessage(context, intent);}
}private void handleRegistration(Context context, Intent intent) {
intent.getStringExtra("registration_id");}private void handleMessage(Context context, Intent intent) {
String message = intent.getExtras().getString("payload");//Display Notification
}}
Step 4 – Display Notification Process the message and Display Notification
NotificationManager manager= (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.icon,null, System.currentTimeMillis());
Intent notifyIntent = new Intent(Intent.ACTION_MAIN); notifyIntent.setClass(context, Home.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT | Notification.FLAG_AUTO_CANCEL);
notification.setLatestEventInfo(context, “title", “message”, contentIntent);manager.notify(1, notification);
Step 5 – Ask Registration ID Async Service to get Registration ID Add it to the Launch
if (Build.VERSION.SDK_INT >= 8) {
Intent registrationIntent = new Intent("com.google.android.c2dm.intent.REGISTER");
registrationIntent.putExtra("app", PendingIntent.getBroadcast(this, 0, new Intent(), 0));
registrationIntent.putExtra("sender", “[email protected]");
startService(registrationIntent);}
Send Push Notification to the App – Trigged by
Server
Send HTTP POST to https://android.apis.google.com/c2dm/send
Body : "collapse_key=Push&data.payload=Hi®istration_id=5245rc9e56g648f6.. ”
Header : "Authorization: GoogleLogin auth=23232xx323232..”
Google Login Auth Code: UserName - [email protected] Password-silicon777
http://bit.ly/googleauthcode
Best Practices
Only right amount of Notifications Option to Opt out Track conversions
Questions
Source Code: http://yousend.it/siliconpush
App gets Registration ID
Send message from Server
Shows Notification