Push Notifications in Native Android Applications

improve this page | report issue

Overview

This tutorial explains how to configure a MobileFirst Native Android application to support push notifications.
Also mentioned are the addresses and ports that are required for notifications to arrive to the supported Android Push Notification Service vendor (GCM).

Prerequisite: Make sure that you read the Configuring a native Android application with the MobileFirst Platform SDK tutorial first.

Setting up the project

missing_alt

  1. Create a MobileFirst project and add a MobileFirst Android Native API.

    In this tutorial and the accompanying sample, the application is called "androidnativepush". Be sure to replace this value with your own application name.

    The native API includes the following push-related file:

    • The push.png file is an icon file that is displayed when a push notification arrives. Copy this file from your native API folder and put it in your native project's res/drawable folders.
  2. Edit the application-descriptor.xml file.

    These settings are also editable with the Application Descriptor Editor in Design mode.

    • Add the pushSender child-element inside the nativeAndroidApp element . Replace the key and senderId values with your API key and project number respectively. In case you do not have these, you can get them from the Google Developer Console.

      <nativeAndroidApp id="AppName" platformVersion="7.1.0.00.20150812-0731"
      	version="1.0" xmlns="http://www.worklight.com/native-android-descriptor">
      	<displayName>AppName</displayName>
      	<description>AppName</description>
      	<publicSigningKey></publicSigningKey>
      	<packageName></packageName>
      	<accessTokenExpiration>3600</accessTokenExpiration>
      	<userIdentityRealms></userIdentityRealms>
      	<pushSender key="" senderId=""/>
      </nativeAndroidApp>
  3. Edit the wlclient.properties file.

    Edit the wlclient.properties file in your native Android project and enter appropriate values for the following fields:

    • wlServerHost – The hostname or IP address of MobileFirst Server.
    • wlServerPort – The port on which MobileFirst Server is listening.
    • GcmSenderId – The project number that you obtained through the Google API console.

    wlServerProtocol = http
    wlServerHost =
    wlServerPort = 10080
    wlServerContext = /MobileFirst-Project-Name/
    wlAppId = MobileFirst-App-Name
    wlAppVersion = 1.0
    wlEnvironment = Androidnative
    wlUid = wY/mbnwKTDDYQUvuQCdSgg==
    wlPlatformVersion = 7.1.0.0
    #languagePreferences = Add locales in order of preference (e.g. en, fr, fr-CA)
    #For Push Notifications,uncomment below line and assign value to it
    GcmSenderId =
    Note: Google recently announced a move from GCM to FCM. The below instructions have been updated accordingly. Also note that existing in-the-field GCM configurations will continue to function however new GCM configurations will not, and FCM must be used instead.
    Android devices use the Firebase Cloud Messaging (FCM) service for push notifications. To setup FCM:
    1. Visit the Firebase Console.
    2. Create a new project and provide a project name.
    3. Click on the Settings "cog wheel" icon and select Project settings.
    4. Click the Cloud Messaging tab to generate a Server API Key and a Sender ID and click Save.
  4. Add Google Play Services (optional)

    For instructions about how to setup Google Play Services review the Setting Up Google Play Services topic at the Android Developer website.

  5. Modify the native Android project.

    Verify that the following permissions exist in the AndroidManifest.xml file of your Android project.

    <permission android:name="com.example.project.permission.C2D_MESSAGE" android:protectionLevel="signature" />
    <p> <uses-permission android:name="android.permission.INTERNET" />
     <uses-permission android:name="android.permission.GET_TASKS"/>
     <uses-permission android:name="com.example.project.permission.C2D_MESSAGE" />
     <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
     <uses-permission android:name="android.permission.WAKE_LOCK" />
     <uses-permission android:name="android.permission.GET_ACCOUNTS" />
     <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"/>

    Replace all com.example.project with your Android project package name.

    Add the launchMode attribute to the application element. Set its value to singleTask.

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme"
        android:launchMode="singleTask">

    Add an intent-filter to the activity element for notifications.

    <activity
    	android:name=".ActivityName"<
    	android:label="@string/app_name">
    	<intent-filter>
     		<action android:name="com.example.project.app_name.NOTIFICATION" />
     		<category android:name="android.intent.category.DEFAULT" />
    	</intent-filter>

    Replace the com.example.project.app_name with your Android project package name and the app_name string as it appears in the res/values/string.xml file. For example: com.sample.notifications.NativeAndroidNotifications.

    Add the GCMIntentService and add an intent-filter for RECEIVE and REGISTRATION of notifications.

    <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="com.example.project" />
       </intent-filter>
       <intent-filter>
           <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
           <category android:name="com.example.project" />
       </intent-filter>
    </receiver>

    Replace all com.example.project with your Android project package name.

Android Push Notifications Service

The following ports must be open: 443, 5228, 5229, and 5230. GCM typically uses only 5228, but it sometimes uses 5229 and 5230.
GCM does not provide specific IP addresses, so you must allow your firewall to accept outgoing connections to all IP addresses that are contained in the IP blocks listed in Google ASN of 15169.

Notification Types

Last modified on December 30, 2016