Developing Push Notifications (C2DM) for Android Vijai Co-Founder Adhish Technologies, Sweet’N’Spicy apps
What is Push Notification?
Without Push Notification
With Push Notification
Flow
Before we start It works on Android SDK You need a Google account Implementing with Device is easier than Simulator
Step 1 Register your address for Google C2DM. signup.html
Step 2 – Manifest Changes Permissions to Send/Receive Messages <permission android:name="com.push.app.permission.C2D_MESSAGE" android:protectionLevel="signature" />
Step 2 – Contd. Receiver for Messages <receiver android:name=".CustomC2DMReceiver" android:permission="com.google.android.c2dm.permission.SEND">
Step 3 – Receiver public class CustomC2DMReceiver extends BroadcastReceiver public 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", startService(registrationIntent); }
Send Push Notification to the App – Trigged by Server Send HTTP POST to Body : "collapse_key=Push&data.payload=Hi®istration_id=5245rc9e56g648f6.. ” Header : "Authorization: GoogleLogin auth=23232xx ” Google Login Auth Code: UserName -
Best Practices Only right amount of Notifications Option to Opt out Track conversions
Questions Source Code:
App gets Registration ID
Send message from Server
Shows Notification