-
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>
-
Download and unzip the Mobile Services Android SDK, open the notifications folder, copy the notifications-1.0.1.jar file to the libs folder of your Eclipse project, and refresh the libs folder.
NoteThe numbers at the end of the file name may change in subsequent SDK releases.
-
Open the file ToDoItemActivity.java, and add the following import statement:
import com.microsoft.windowsazure.notifications.NotificationsManager; import com.microsoft.windowsazure.mobileservices.Registration; import com.microsoft.windowsazure.mobileservices.RegistrationCallback;
-
Add the following private variable to the class, where
<PROJECT_NUMBER>
is the Project Number assigned by Google to your app in the preceding procedure:public static final String SENDER_ID = "<PROJECT_NUMBER>";
-
In ToDoActivity.java, add the following method to the ToDoActivity class to allow registering for notifications.
/** * Registers mobile services client to receive GCM push notifications * @param gcmRegistrationId The Google Cloud Messaging session Id returned * by the call to GoogleCloudMessaging.register in NotificationsManager.handleNotifications */ public void registerForPush(String gcmRegistrationId) { mClient.getPush().register(gcmRegistrationId,null,new RegistrationCallback() { @Override public void onRegister(Registration registration, Exception exception) { if (exception != null) { // handle exception } } }); }
-
Next we need to add a new class to handle notifications. In the Package Explorer, right-click the package (under the
src
node), click New, click Class. -
In Name type
MyHandler
, in Superclass typecom.microsoft.windowsazure.notifications.NotificationsHandler
, then click FinishThis creates the new MyHandler class.
-
Add the following import statements for the
MyHandler
class:import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.support.v4.app.NotificationCompat; import com.microsoft.windowsazure.mobileservices.Registration; import com.microsoft.windowsazure.mobileservices.RegistrationCallback;
-
Next add the following members for the
MyHandler
class:public static final int NOTIFICATION_ID = 1; private NotificationManager mNotificationManager; NotificationCompat.Builder builder; Context ctx;
-
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, String gcmRegistrationId) { super.onRegistered(context, gcmRegistrationId); ToDoActivity toDoActivity = (ToDoActivity)context; toDoActivity.registerForPush(gcmRegistrationId); }
-
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) { ctx = context; String nhMessage = bundle.getString("message"); sendNotification(nhMessage); } private void sendNotification(String msg) { mNotificationManager = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE); PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0, new Intent(ctx, ToDoActivity.class), 0); NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(ctx) .setSmallIcon(R.drawable.ic_launcher) .setContentTitle("Notification Hub Demo") .setStyle(new NotificationCompat.BigTextStyle() .bigText(msg)) .setContentText(msg); mBuilder.setContentIntent(contentIntent); mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build()); }
-
Back in 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.