developing push notifications (c2dm) for android vijai co-founder adhish technologies,...

18
Developing Push Notifications (C2DM) for Android Vijai Co-Founder Adhish Technologies, Sweet’N’Spicy apps

Upload: barbara-powers

Post on 11-Jan-2016

213 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Developing Push Notifications (C2DM) for Android Vijai Co-Founder Adhish Technologies, Sweet’N’Spicy apps

Developing Push Notifications (C2DM) for

Android

Vijai Co-FounderAdhish Technologies, Sweet’N’Spicy apps

Page 2: Developing Push Notifications (C2DM) for Android Vijai Co-Founder Adhish Technologies, Sweet’N’Spicy apps

What is Push Notification?

Page 3: Developing Push Notifications (C2DM) for Android Vijai Co-Founder Adhish Technologies, Sweet’N’Spicy apps

Without Push Notification

Page 4: Developing Push Notifications (C2DM) for Android Vijai Co-Founder Adhish Technologies, Sweet’N’Spicy apps

With Push Notification

Page 5: Developing Push Notifications (C2DM) for Android Vijai Co-Founder Adhish Technologies, Sweet’N’Spicy apps

Flow

Page 6: Developing Push Notifications (C2DM) for Android Vijai Co-Founder Adhish Technologies, Sweet’N’Spicy apps

Before we start

It works on Android SDK 2.2+. You need a Google account

Implementing with Device is easier than Simulator

Page 7: Developing Push Notifications (C2DM) for Android Vijai Co-Founder Adhish Technologies, Sweet’N’Spicy apps

Step 1

Register your email address for Google C2DM.

http://code.google.com/android/c2dm/signup.html

Page 8: Developing Push Notifications (C2DM) for Android Vijai Co-Founder Adhish Technologies, Sweet’N’Spicy apps

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" />

Page 9: Developing Push Notifications (C2DM) for Android Vijai Co-Founder Adhish Technologies, Sweet’N’Spicy apps

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>

Page 10: Developing Push Notifications (C2DM) for Android Vijai Co-Founder Adhish Technologies, Sweet’N’Spicy apps

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

}}

Page 11: Developing Push Notifications (C2DM) for Android Vijai Co-Founder Adhish Technologies, Sweet’N’Spicy apps

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);

Page 12: Developing Push Notifications (C2DM) for Android Vijai Co-Founder Adhish Technologies, Sweet’N’Spicy apps

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);}

Page 13: Developing Push Notifications (C2DM) for Android Vijai Co-Founder Adhish Technologies, Sweet’N’Spicy apps

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&registration_id=5245rc9e56g648f6.. ”

Header : "Authorization: GoogleLogin auth=23232xx323232..”

Google Login Auth Code: UserName - [email protected] Password-silicon777

http://bit.ly/googleauthcode

Page 14: Developing Push Notifications (C2DM) for Android Vijai Co-Founder Adhish Technologies, Sweet’N’Spicy apps

Best Practices

Only right amount of Notifications Option to Opt out Track conversions

Page 15: Developing Push Notifications (C2DM) for Android Vijai Co-Founder Adhish Technologies, Sweet’N’Spicy apps

Questions

Source Code: http://yousend.it/siliconpush

Page 16: Developing Push Notifications (C2DM) for Android Vijai Co-Founder Adhish Technologies, Sweet’N’Spicy apps

App gets Registration ID

Page 17: Developing Push Notifications (C2DM) for Android Vijai Co-Founder Adhish Technologies, Sweet’N’Spicy apps

Send message from Server

Page 18: Developing Push Notifications (C2DM) for Android Vijai Co-Founder Adhish Technologies, Sweet’N’Spicy apps

Shows Notification