-
In your app project, open the file
AndroidManifest.xml
. In the code in the next two steps, replace**my_app_package**
with the name of the app package for your project, which is the value of thepackage
attribute of themanifest
tag. -
Add the following new permissions after the existing
uses-permission
element:<permission android:name="**my_app_package**.permission.C2D_MESSAGE" android:protectionLevel="signature" /> <uses-permission android:name="**my_app_package**.permission.C2D_MESSAGE" /> <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /> <uses-permission android:name="android.permission.GET_ACCOUNTS" /> <uses-permission android:name="android.permission.WAKE_LOCK" />
-
Add the following code after the
application
opening tag:<receiver android:name="com.microsoft.windowsazure.notifications.NotificationsBroadcastReceiver" android:permission="com.google.android.c2dm.permission.SEND"> <intent-filter> <action android:name="com.google.android.c2dm.intent.RECEIVE" /> <category android:name="**my_app_package**" /> </intent-filter> </receiver>
-
Open the file ToDoActivity.java, and add the following import statement:
import com.microsoft.windowsazure.notifications.NotificationsManager;
-
Add the following private variable to the class: replace
<PROJECT_NUMBER>
with the Project Number assigned by Google to your app in the preceding procedure:public static final String SENDER_ID = "<PROJECT_NUMBER>";
-
Change the definition of the MobileServiceClient from private to public static, so it now looks like this:
public static MobileServiceClient mClient;
-
Next we need to add a new class to handle notifications. In the Project Explorer, open the src => main => java nodes, and right-click the package name node: click New, then click Java Class.
-
In Name type
MyHandler
, then click OK. -
In the MyHandler file, replace the class declaration with
public class MyHandler extends NotificationsHandler {
-
Add the following import statements for the
MyHandler
class:import com.microsoft.windowsazure.notifications.NotificationsHandler; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.os.AsyncTask; import android.os.Bundle; import android.support.v4.app.NotificationCompat;
-
Next add this member to the
MyHandler
class:public static final int NOTIFICATION_ID = 1;
-
In the
MyHandler
class, add the following code to override the onRegistered method, which registers your device with the mobile service Notification Hub.@Override public void onRegistered(Context context, final String gcmRegistrationId) { super.onRegistered(context, gcmRegistrationId); new AsyncTask<Void, Void, Void>() { protected Void doInBackground(Void... params) { try { ToDoActivity.mClient.getPush().register(gcmRegistrationId); return null; } catch(Exception e) { // handle error } return null; } }.execute(); }
-
In the
MyHandler
class, add the following code to override the onReceive method, which causes the notification to display when it is received.@Override public void onReceive(Context context, Bundle bundle) { String msg = bundle.getString("message"); PendingIntent contentIntent = PendingIntent.getActivity(context, 0, // requestCode new Intent(context, ToDoActivity.class), 0); // flags Notification notification = new NotificationCompat.Builder(context) .setSmallIcon(R.drawable.ic_launcher) .setContentTitle("Notification Hub Demo") .setStyle(new NotificationCompat.BigTextStyle().bigText(msg)) .setContentText(msg) .setContentIntent(contentIntent) .build(); NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(NOTIFICATION_ID, notification); }
-
Back in the TodoActivity.java file, update the onCreate method of the ToDoActivity class to register the notification handler class. Make sure to add this code after the MobileServiceClient is instantiated.
NotificationsManager.handleNotifications(this, SENDER_ID, MyHandler.class);
Your app is now updated to support push notifications.