How-tos

Migrate your Android Push Notifications Service from Azure to Bluemix

Share this post:

Using Azure Android Push Documentation as a starting point, this blog will guide you through the steps to migrate your existing Azure push notification enabled Android application to use Push Notifications on Bluemix.

Follow these Steps to Migrate Your Android App

Create a New Push Notifications Service Instance on Bluemix

To start, create an account on Bluemix and provision a new Push Notifications Service instance. Once created, navigate to the Manage tab and click the Configure Now button.

PushManageConfigNow

Underneath GCM/FCM Push Credentials on the right, input your FCM/GCM Project ID and API Key (same key used in Azure) and click Save.

Once you have successfully configured your Push Notifications instance, navigate to the Service Credentials tab, click View Credentials and take note of your appGuid and clientSecret values. You will need to add them to your Android Studio project later.

PushCredentials

Update FCM Project Settings

Now, let’s make some changes to your Firebase Cloud Messaging project.

Note: If you are using a legacy Google Cloud Messaging project (created around or prior to Sept 20th, 2016), skip down to Update build.gradle to Include Bluemix Push Notifications Dependencies.

Once you navigate to your FCM project, open the Project Settings dashboard and select ADD APP.  You should have your existing package name here already, but we need to add the Bluemix Push Notifications package here to handle proper interactions between the SDK, FCM, and Bluemix.

FCMProjSettingsAdd

Select Add Firebase to your Android app and enter the package name com.ibm.mobilefirstplatform.clientsdk.android.push then click ADD APP.

AddBluemixPushNotificationPackage

The google-services.json that you are prompted to download needs to replace the existing .json in your Android Studio project at YourAndroidProject\app\google-services.json. You can simply copy and paste the entire contents of the new google-services.json over the existing if Android Studio gives you problems when attempting to copy in the file directly.

Update build.gradle to Include Bluemix Push Notifications Dependencies

Now that you have updated your google-services.json, let’s update your build.gradle file at YourAndroidProject\app\build.gradle.

First, replace the existing repository:

 repositories {
maven {
url "http://dl.bintray.com/microsoftazuremobile/SDK"
}
}

With:

repositories {
mavenCentral()
jcenter()
}

Then, replace the Azure and google play services dependencies:

compile 'com.microsoft.azure:notification-hubs-android-sdk:0.4@aar'
compile 'com.microsoft.azure:azure-notifications-handler:1.0.1@aar'
compile 'com.google.android.gms:play-services-gcm:9.2.0'

With the Bluemix Push Notifications SDK dependency:

compile ('com.ibm.mobilefirstplatform.clientsdk.android:push:3.+')

Update AndroidManifest.xml

First, let’s update the permissions to include the following for Bluemix Push Notifications:

<uses-permission android:name="android.permission.USE_CREDENTIALS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>

The necessary INTERNET and GET_ACCOUNTS permissions should already exist. The com.google.android.c2dm.permission.RECEIVE is not needed and can be removed.

Next, remove the meta data tag for google play services; it’s not needed.

<meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />

Now, the intent filter added for Azure needs to be changed from:

<service android:name=".MyInstanceIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>

To:

<intent-filter>
<action android:name="Your_Android_Package_Name.IBMPushNotification"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>

Note that the instanceIDservice is not needed, you can remove the entire service wrapper. Also, be sure to update Your_Android_Package_Name with your specific Android application’s package. Be aware that whichever activity you include this intent-filter within is the activity that will start when your users tap the notification.

Next, find the registration service tag within the application tag:

<service android:name=".RegistrationIntentService" android:exported="false">
</service>

Replace that block with the following:

<service android:name="com.ibm.mobilefirstplatform.clientsdk.android.push.api.MFPPushIntentService"
android:exported="true" >
<intent-filter>
<action android:name"com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<service
android:name="com.ibm.mobilefirstplatform.clientsdk.android.push.api.MFPPush"
android:exported="true" >
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>

Lastly, find the code for the Azure broadcast receiver:[/code]

 <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="<your package name>" />
</intent-filter>
</receiver>

And completely replace with the Bluemix Push Notifications notification handler:

<activity android:name="com.ibm.mobilefirstplatform.clientsdk.android.push.api.MFPPushNotificationHandler" android:theme="@android:style/Theme.NoDisplay" />

Updating Java Code

Now, on to the easy part! Unlike Azure, with Bluemix Push Notifications you do not need to write additional classes to integrate push into your mobile app. Therefore, you can remove the NotificationSettings, the MyInstanceIDService, the RegistrationIntentService, and  MyHandler classes entirely.

You can also remove the following imports from your MainActivity :

 import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GoogleApiAvailability;
import com.microsoft.windowsazure.notifications.NotificationsManager;

And replace them with:

import com.ibm.mobilefirstplatform.clientsdk.android.core.api.BMSClient;
import com.ibm.mobilefirstplatform.clientsdk.android.push.api.MFPPush;
import com.ibm.mobilefirstplatform.clientsdk.android.push.api.MFPPushException;
import com.ibm.mobilefirstplatform.clientsdk.android.push.api.MFPPushResponseListener;
import com.ibm.mobilefirstplatform.clientsdk.android.push.api.MFPPushNotificationListener;
import com.ibm.mobilefirstplatform.clientsdk.android.push.api.MFPSimplePushNotification;

Once removed, you will need to remove the remaining erroneous code within your MainActivity. Remove the following static variables:

 public static Boolean isVisible = false;
private static final int PLAY_SERVICES_RESOLUTION_REQUEST = 9000;

And replace with:

private MFPPush push;
private MFPPushNotificationListener notificationListener;

Now, look for the following methods that you added for Azure and remove them completely; checkPlayServices(), registerWithNotificationHubs(), and ToastNotify().

Once removed, find the code within your onCreate():

    NotificationsManager.handleNotifications(this, NotificationSettings.SenderId, MyHandler.class);
registerWithNotificationHubs();

And replace with Bluemix Core and Push Notifications initialization:

    // Initialize the SDK for Android
BMSClient.getInstance().initialize(this, BMSClient.REGION_US_SOUTH);

//Initialize client Push SDK for Java
push = MFPPush.getInstance();
push.initialize(getApplicationContext(), "appGUID", "clientSecret");

Be sure to input the correct REGION, appGUID, and clientSecret values gathered from the Bluemix Push Notifications dashboard earlier.

Underneath push initialization, insert the following code to register the device with your Bluemix Push Notifications instance when the app starts:

//Register Android devices
push.registerDevice(new MFPPushResponseListener<String>() {
@Override
public void onSuccess(String response) {
//handle success here
//This is where you would call the subscribe to tag api
}
@Override
public void onFailure(MFPPushException ex) {
//handle failure here
}
});

You will also need to create a NotificationHandler, so add this code as well to your onCreate:

//Handles the notification when it arrives
notificationListener = new MFPPushNotificationListener() {
@Override
public void onReceive (final MFPSimplePushNotification message){
// Handle Push Notification
// This is where you would display a toast, alert, or
// create a new intent to a desired activity etc...
}
};

Lastly, we need to update the <em>onResume() </em>and <em>onPause() </em> functions to manage the SDK listener. First, remove all references to <em>isVisible = true </em>and <em>isVisible=false</em>. You will not need to override the <em>onStart()</em> and <em>onStop() </em>function callls anymore, but you will need the following:


@Override
protected void (){
super.onResume();
if(push != null) {
push.listen(notificationListener);
}
}
@Override
protected void onPause() {
super.onPause();
if (push != null) {
push.hold();
}
}

This code let’s the SDK know when to handle an inbound push notification and when to hold it for the next time the app is brought to the foreground.

Sending a Push Notification

To send a push notification to your registered Android device you can use the push dashboard’s Send Notifications option or use the Push Notifications REST api to make REST requests to your instance.

For more information on Bluemix Push Notifications, be sure to check out tagging and other advanced documentation.

More How-tos stories
April 5, 2019

IBM Cloud App ID: Updated Runtime APIs Provide Tighter Interoperability for Your Cloud-Native Apps

As part of our efforts to tighten interoperability and broaden the frameworks that are able to use IBM Cloud App ID, we've updated the runtime APIs.

Continue reading

February 11, 2019

IBM Cloud Mobile Foundation: Top-10 Features to Boost Your App Rating

The Mobile Foundation service offers many hidden gems that developers often gloss over. In this post, we highlight the top 10 features that increase user engagement as well as improve app ratings. 

Continue reading

February 8, 2019

Distribute CoreML Models Securely Using Mobile Foundation Service

IBM Mobile Foundation is excited to introduce a feature to manage the distribution of CoreML models.

Continue reading