FCM Support for Android in MobileFirst Platform v7.1
Anantha Krishnan, Kapil Powar January 23, 2019
MobileFirst_Foundation Announcement AndroidFollow the steps in this post to use Firebase Cloud Messaging (FCM) in your android app with MobileFirst Platform v7.1 Android Push SDK
Google Cloud Messaging (GCM) has been deprecated and is integrated with Firebase Cloud Messaging (FCM). Google will turn off most GCM services by April 2019.
For now, the existing applications using GCM services will continue to work as-is.
Note: Custom notifications support is available with only FCM. GCM custom notifications are not supported for devices using API 26 or above.
Get google-services.json
for your application from here. Add the json file to your app
folder.
Steps for Native application
Step 1: Open the AndroidManifest.xml
file and follow the steps below:
a. Inside the <application>
-> <activity>
tag add the following intent-filter
:
<intent-filter>
<action android:name="<applicationId>.IBMPushNotification" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
b. Add the following <activity>
after the above step:
<activity android:name="com.worklight.wlclient.fcmpush.MFPFCMPushNotificationHandler"
android:theme="@android:style/Theme.NoDisplay"/>
c. Now, add the following services:
<service android:exported="true" android:name="com.worklight.wlclient.fcmpush.MFPFCMPushIntentService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
<service android:exported="true" android:name="com.worklight.wlclient.fcmpush.WLFCMPush">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
d. Update the application
attributes as below:
<application android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
android:launchMode="singleTask" >
e. Remove the <permission>
,<services>
and <intent-filter>
shown below:
<permission android:name="<ApplicationId>.permission.C2D_MESSAGE" android:protectionLevel="signature" />
<uses-permission android:name="<ApplicationId>.permission.C2D_MESSAGE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<service android:name="com.worklight.wlclient.push.GCMIntentService" />
<receiver android:name="com.worklight.wlclient.push.WLBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE"/>
<category android:name="<ApplicationId>" />
</intent-filter>
<intent-filter>
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="<ApplicationId>" />
</intent-filter>
</receiver>
<meta-data
android:name="com.google.android.gms.version"
android:value="15.0.0" />
Step 2: Open the project gradle file build.gradle
and add the following dependencies:
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.3'
classpath 'com.google.gms:google-services:3.0.0'
}
}
allprojects {
repositories {
google()
jcenter()
}
}
Step 3: Open the App gradle file build.gradle
and follow the steps below:
a. Add the dependencies below:
dependencies {
implementation 'com.google.firebase:firebase-messaging:10.2.6'
implementation 'com.android.support:multidex:1.0.3'
compile 'com.android.support:support-v4:28.0.0'
}
apply plugin: 'com.google.gms.google-services'
b. Enable multidex in app gradle file build.gradle
under android tag.
multiDexEnabled true
dexOptions {
javaMaxHeapSize "4g"
}
For more info on multidex follow see here.
Step 4: In the main acitivity file MainActivity.java
perform the following changes:
a. Add the following code in oncreate
method (you can add it anywhere , but make sure this is getting called when app opens):
final WLClient client = WLClient.createInstance(this);
push = client.getPush();
push.setWLNotificationListener(wlNotificationListener);
b. Add the following code for handling the resume and pause scenarios:
@Override
protected void onPause() {
super.onPause();
if (push != null) {
push.setForeground(false);
}
}
@Override
protected void onResume() {
super.onResume();
if (push != null)
push.setForeground(true);
push.setWLNotificationListener(wlNotificationListener);
}
Steps for Hybrid application
Step 1: Open the AndroidManifest.xml
file and follow the steps below:
a. Inside the <application>
-> <activity>
tag add the following intent-filter
:
<intent-filter>
<action android:name="<applicationId>.IBMPushNotification" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
b. Update the <application>
tag as below:
<application android:label="@string/app_name" android:icon="@drawable/icon">
c. Add below acivity in <application>
:
<activity android:name="com.worklight.wlclient.fcmpush.MFPFCMPushNotificationHandler"
android:theme="@android:style/Theme.NoDisplay"/>
d. Add the following services:
<service android:exported="true" android:name="com.worklight.wlclient.fcmpush.MFPFCMPushIntentService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
<service android:exported="true" android:name="com.worklight.wlclient.fcmpush.WLFCMPush">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
e. Remove the <permission>
,<services>
and <intent-filter>
shown below:
<permission android:name="com.HybridTagNotifications.permission.C2D_MESSAGE" android:protectionLevel="signature"/>
<uses-permission android:name="com.HybridTagNotifications.permission.C2D_MESSAGE"/>
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE"/>
<service android:name=".GCMIntentService"/>
<service android:name="com.worklight.wlclient.push.MFPPushService" android:permission="android.permission.BIND_JOB_SERVICE" android:exported="true"/>
<receiver android:name="com.worklight.wlclient.push.WLBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE"/>
<category android:name="com.sample.tagnotificationsandroid" />
</intent-filter>
<intent-filter>
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.sample.tagnotificationsandroid" />
</intent-filter>
</receiver>
Step 2: Open the project gradle file and add the following dependencies:
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.3'
classpath 'com.google.gms:google-services:3.0.0'
}
}
allprojects {
repositories {
google()
jcenter()
}
}
Step 3: Open the App gradle file build.gradle
and add the following:
dependencies {
//compile files('libs/appcompat-v4.jar')
implementation 'com.google.firebase:firebase-messaging:10.2.6'
implementation 'com.android.support:multidex:1.0.3'
compile 'com.android.support:support-v4:28.0.0'
}
apply plugin: 'com.google.gms.google-services'
Step 4: Enable multidex in app gradle file build.gradle
under android tag:
multiDexEnabled true
dexOptions {
javaMaxHeapSize "4g"
}
For more info on multidex see here.
Inclusive terminology note: The Mobile First Platform team is making changes to support the IBM® initiative to replace racially biased and other discriminatory language in our code and content with more inclusive language. While IBM values the use of inclusive language, terms that are outside of IBM's direct influence are sometimes required for the sake of maintaining user understanding. As other industry leaders join IBM in embracing the use of inclusive language, IBM will continue to update the documentation to reflect those changes.