Add Push Notification Support to Your Android App in Just 4-Steps

If you want to boost user engagement with your Android mobile app using push notifications, you’re going to need to initialize support for notification receiving in your app.

Research has found that when users opt in to push messages, they average 3x more app launches than those who opted out. Good news: for Android apps, opt-in rates are effectively 100%! Permission to send notifications is granted by the user at the same time they install your Android app, so users must agree to receive notifications, or choose not to install. That’s in contrast to iOS, where users are explicitly prompted to allow notifications, and opt-in rates have been found to be a much lower 41%.

It’s still possible for Android users to disable notification after installing, but chances are you’ll be able to reach a very high percentage of your user base after initial install. All the more reason to do notifications well!

In our post 3-Step Guide to Unlocking Benefits of Android Push Notifications, we showed you how to add push notification capability to your Android app using Firebase Cloud Messaging (FCM). Here we provide an example of how to support receiving push notifications in your Android mobile App, along with a downloadable starter project with support for Firebase Cloud Messaging.

FCM require devices running Android 4.0 or higher that also have the Google Play Store app installed, or an emulator running Android 4.0 with Google APIs. Our steps are based on the latest beta version of Android Studio – Android Studio 2.3 RC 1 at the time of writing – and Gradle build tools.

1. Add the FCM SDK to your Android

In Android Studio, add the FCM dependency to your app-level build.gradle file:

dependencies {
...
     compile ‘com.google.firebase:firebase-messaging:10.2.0'
...
}

If your Android app cannot function without FCM, then be sure to set minSdkVersion 14 (Ice Cream Sandwich) or higher in your build.gradle file. In any case, you should check to make sure Google Services are installed.

2. Configure FCM in your app’s manifest

First, configure a service that extends FirebaseMessagingService. This service allows your app to receive notifications when open in the foreground (i.e. on screen), to receive custom data properties in notification messages and/or send notification messages back to FCM. Add the following to your AndroidManifest.xml file:

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

Next, add a service that extends FirebaseInstanceIdService. This is needed to handle creating and updating FCM registration tokens, which FCM users for sending notifications to specific devices. Add the following to your AndroidManifest.xml file:

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

You can set a default icon and color for notifications. These are used if the incoming notifications do not explicitly set the icon and colour. Add the following to your AndroidManifest.xml file:

<meta-data
    android:name="com.google.firebase.messaging.default_notification_icon"
    android:resource=“@drawable/ic_your_notification_icon" />
<meta-data
    android:name="com.google.firebase.messaging.default_notification_color"
    android:resource="@color/yourAccentColor" />

3. Retrieve and save the registration token

The first time your Android app is run, the FCM SDK generates a new registration token. This token may change when:

  • The app deletes Instance ID
  • The app is restored on a new device
  • The user uninstalls/reinstall the app
  • The user clears app data.

To handle creation and update of registration tokens, add the following to the service that extends FirebaseMessagingService. After retrieving the token, you’ll want to save it to your app server so you and target notifications to the device. Your code would look like this:

@Override
public void onTokenRefresh() {
    // Get updated InstanceID token.
    String refreshedToken = FirebaseInstanceId.getInstance().getToken();
    Log.d(TAG, "Refreshed token: " + refreshedToken);
    // Send the token to your app server.
    sendRegistrationToServer(refreshedToken);
}

4. Check if Google Play Services are available

Finally, since FCM relies on the Play Services SDK, you should check to see if a compatible version of Google Play services are installed on the device. Google recommends checking for this in the onCreate() and onResume() methods of your Android app’s main activity.

If the device doesn’t have a compatible version of Google Play services, you can call GoogleApiAvailability.makeGooglePlayServicesAvailable(), which prompts users to download Google Play services from the Play Store.

Download our sample Android app starter project for a complete working example of how to support Firebase Cloud Messaging for push notifications in an Android app.

Download

You can download this for free, but if you feel like joining our mailing list for relevant articles, take a moment to fill in your information.


Leave a Reply

Your email address will not be published.

top