Overview

Skill Level: Intermediate

As of April 10, 2018, Google officially deprecated the GCM push service and APIs and announced that GCM support could disappear as early as April 11, 2019. For this reason, IBM recommends that you implement FCM for new projects and consider migrating existing GCM projects to FCM.

WARNING: When IBM Mobile App Messaging stores data that you send, the data is not encrypted. IBM Mobile App Messaging recommends that you do not transmit sensitive information in inbox, inapp, and mobile app messages. We do not encrypt the data in our databases or log files, or when stored on the device. If you must transmit sensitive information, encrypt the data before sending it and decrypt the data in memory on the device.

Prerequisites

Notes:

Are you implementing a new project?
IBM recommends that you use FCM because Google has depricated GCM and will soon disable it entirely.

Do you have an existing project that uses GCM?

IBM recommends that you migrate from GCM to FCM. For information, see Migrating from Google Cloud Messaging (GCM) to Firebase Cloud Messaging (FCM) to get your code running under FCM.

To get started, you need:

  • An account with Watson Campaign Automation. ¬†

TIP: If you do not already have an Watson Campaign Automation account, contact your Client Services representative or sales.

Step-by-step

  1. Create a Google API project with Google Cloud Messaging (GCM) and then verify your GCM API Key.

    To use Google Cloud Messaging (GCM) in your application, first you need to create a new project in the Google APIs console.

    NOTE: Starting in September 2016, Google changed the way that GCM server keys are generated. New server keys must be created in the Firebase Console. See https://developers.google.com/cloud-messaging/http-server-ref#error-codes for more details about the change.

    1. Open the Google APIs Console page at  https://console.developers.google.com.
    2. If you did not create an API project yet, the page prompts you to do so.
      Note: If you already have existing projects, the first page you see is the Dashboard page. From there, you can create a new project by opening the project drop-down menu that is located in the upper left corner and choosing Create Project.
    3. Click Enable API.
    4. In the search window, enter GCM and choose Google Cloud Messaging.
    5. Click Enable. The following message is displayed about creating credentials; however, IBM recommends that you do not create credentials at this time.

      “This API is enabled, but you can’t use it in your project until you create credentials. Click “Go to Credentials” to do this now (strongly recommended).”

    6. Go to https://console.firebase.google.com.
    7. Click Import Google Project.
    8. Choose the project that you created in Google console and your country/region.
    9. Click Add Firebase.
    10. Click the Settings icon and choose Project Settings.
    11. Click Cloud Messaging tab.
    12. Copy the Legacy server key and Sender ID values. You need these values to enable mobile app messages in your application.

    It is recommended that you create separate projects for your development and production builds so that you can align testing best practices.

    Next, verify that your GCM API Key is valid

    There are cases where a newly created GCM API key does not work for push notifications. Before using your newly created GCM API Key, verify that the API Key is valid. Otherwise, if you use an invalid API Key, an “Invalid Google API key” message is displayed when you add a new application in the Watson Campaign Automation console.

    Before using your newly created GCM API Key, verify that the API Key is valid. Otherwise, if you use an invalid API Key, the application does not receive push notifications.

    To verify your Google API Key is valid, follow these steps:

    1. Send the following request using curl or postman:
      curl --header "Authorization: key=REPLACE_WITH_GCM_API_KEY"
      --header Content-Type:"application/json"
      https://android.googleapis.com/gcm/send -d "{\"registration_ids\":[\"ABC\"]}"

    2. If you receive the following response, the API Key is valid.
      {"multicast_id":5464097959083008962,"success":0,"failure":1,"canonical_ids":0,"results":
      [{"error":"InvalidRegistration"}]}
    3. However, if you receive this response, the API key is invalid and you must go to step 4:
      <HTML>
      <HEAD>
      <TITLE>Unauthorized</TITLE>
      </HEAD>
      <BODY BGCOLOR="#FFFFFF" TEXT="#000000">
      <H1>Unauthorized</H1>
      <H2>Error 401</H2>
      </BODY>
      </HTML>
    4. For an invalid API Key as indicated in step 3 above, follow these steps:
      1. Go to FCM console at https://console.firebase.google.com and convert your project from GCM to a FCM project. If you already imported your project into FCM, you might be using the incorrect API key. Continue to step 2.
      2. Open the converted project and go to Project Settings (gear icon).
      3. Copy Legacy server key. You will use this value in step 4, substep 12.
      4. Copy Sender ID. You will use this value in step 4, substep 12.

     

  2. Register your Android application in Watson Campaign Automation.

    You must register your Android app with Watson Campaign Automation.

    For more information about adding your app in Watson Campaign Automation, see Add an app in Watson Campaign Automation.

    Restriction: Watson Campaign Automation documentation contains information for marketers, app developers, and organization administrators and requires a user name and password. If you need a user name and password, contact your Client Services representative.

  3. Implement the Android SDK.

    After you register your Android app, you must implement it so that you can start testing and creating notifications. 

    TIP: It is not recommended to allow your app to be installed on external storage cards for several reasons that Google states on the developer site at https://developer.android.com/guide/topics/data/install-location#ShouldNot. Your app should instead be installed on internal memory.

    1. Download the Android SDK as described in the Prerequisites.
    2. Add the Android SDK to your Gradle project by copying the Android SDK JAR file to your libs folder. If you do not have a libs folder, create one next to your src folder. Next, in your app’s build.gradle¬†file, make sure that you have the following code in the dependencies section, where <play-services version> is the latest version of the play-services package. For example, you could use¬†version 11.8.0.
      dependencies {
      implementation fileTree(dir: 'libs', include: ['*.jar'])
      implementation 'com.android.support:appcompat-v7:21.+'
      implementation 'com.google.android.gms:play-services-base:<play-services version>'
      implementation 'com.google.android.gms:play-services-gcm:<play-services version>'

      If you use location:

      implementation 'com.google.android.gms:play-services-location:<play-services version>'

    3. Optional: If you do not have a Gradle project, use the following steps.
      a. Create a folder in your project that is called libs, and move the SDK JAR file to it. You can find the SDK JAR file in the root directory after you extract the SDK package.
      b. Select your project.
      c. Right-click properties.
      d. Choose java build path.
      e. Select libraries tab.
      f. Choose Add JARs, and browse to Android SDK JAR file in the libs folder.
    4. Modify the AndroidManifest.xml file. Follow these steps.
      1. Set the minimum Android SDK version to 19 (Android 4.4) or higher in your application’s build.gradle or the AndroidManifest.xml. For simple push, you can set Android SDK version to 16 or higher. The 3.x Android SDK is not supported with previous versions of Android. The target version should be the latest release version of Android.

        Note: For Android SDK versions 16, 17, and 18, ensure that location services are disabled.

      2. The following permissions are needed for the SDK to work properly. These are positioned as the child of the <manifest> node. Replace the two occurrences of YOUR_APP_PACKAGE_NAME with your application package name.
        <permission android:name="YOUR_APP_PACKAGE_NAME.permission.C2D_MESSAGE" android:protectionLevel="signature" />
        <uses-permission android:name="YOUR_APP_PACKAGE_NAME.permission.C2D_MESSAGE" />
        <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
        <uses-permission android:name="android.permission.INTERNET" />
        <uses-permission android:name="android.permission.WAKE_LOCK" />
        <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
        
      3. If you want to use the dial action in the notification details page, add the following information:

        <uses-permission android:name="android.permission.CALL_PHONE" /> 

        If you plan to publish your app in the Google Play store and want to allow non-phone devices to download your app, also include:

        <uses-feature android:name="android.hardware.telephony" android:required="false" /> 
      4. Set your application class to a class that extends com.ibm.mce.sdk.api.MceApplication. Replace YOUR_APP_PACKAGE_NAME with your package name.

        You can use the following as a template for your application class:

        package YOUR_APP_PACKAGE_NAME;
        import android.annotation.TargetApi;
        import android.app.NotificationChannel;
        import android.app.NotificationManager;
        import android.content.Context;
        import android.os.Build;
        import com.ibm.mce.sdk.api.MceApplication;
        import com.ibm.mce.sdk.api.MceSdk;
        import com.ibm.mce.sdk.api.notification.NotificationsPreference;
        public class MyApplication extends MceApplication {
        public void onCreate() {
        super.onCreate();
        if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.O) {
        createNotificationChannel(getApplicationContext());
        }
        }@
        TargetApi(26) private static void createNotificationChannel(Context context) {
        String MY_SAMPLE_NOTIFICATION_CHANNEL_ID = context.getString(R.string.notif_channel_id);
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        NotificationChannel channel = notificationManager.getNotificationChannel(MY_SAMPLE_NOTIFICATION_CHANNEL_ID);
        if (channel == null) {
        CharSequence name = context.getString(R.string.notif_channel_name);
        String description = context.getString(R.string.notif_channel_description);
        int importance = NotificationManager.IMPORTANCE_HIGH;
        channel = new NotificationChannel(MY_SAMPLE_NOTIFICATION_CHANNEL_ID, name, importance);
        channel.setDescription(description);
        NotificationsPreference notificationsPreference = MceSdk.getNotificationsClient().getNotificationsPreference();
        notificationsPreference.setNotificationChannelId(context, MY_SAMPLE_NOTIFICATION_CHANNEL_ID);
        notificationManager.createNotificationChannel(channel);
        }
        }
        }

        The class above refers to the following that you will need to add to strings.xml:

        <string name="notif_channel_id">my-notification-channel</string> <string name="notif_channel_name">My sample notification channel</string> <string name="notif_channel_description">My sample notification channel description</string>

        If you are targeting Android O or higher, you will have to set a default notification channel as well. Add this to your <application> node in AndroidManifest.xml:

        <meta-data android:name="com.google.firebase.messaging.default_notification_channel_id" android:value="@string/notif_channel_id" /
      5. Add the following XML to the <application> node. Replace the three occurrences of YOUR_APP_PACKAGE_NAME with your application package name.

        Note: If you are migrating the Android SDK from an earlier release to 3.7.1.0.2 (or later), you must make a few changes to the AndroidManifest.xml. For instructions, see Migrating the Android SDK to release 3.7.1.0.2 (or later).

        Add this XML: 

        <provider android:name="com.ibm.mce.sdk.db.Provider" android:authorities="YOUR_APP_PACKAGE_NAME.MCE_PROVIDER" android:exported="false" /><receiver android:name="com.google.android.gms.gcm.GcmReceiver" > <intent-filter android:permission="com.google.android.c2dm.permission.SEND" > <action android:name="com.google.android.c2dm.intent.RECEIVE" /> <category android:name="YOUR_APP_PACKAGE_NAME" /> </intent-filter> <intent-filter android:permission="com.google.android.c2dm.permission.SEND" > <action android:name="com.google.android.c2dm.intent.REGISTRATION" /> <category android:name="YOUR_APP_PACKAGE_NAME" /> </intent-filter></receiver><receiver android:name="com.ibm.mce.sdk.notification.NotifActionReceiver" /><receiver android:name="com.ibm.mce.sdk.wi.AlarmReceiver" > <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> </intent-filter> <intent-filter> <action android:name="android.intent.action.TIMEZONE_CHANGED" /> </intent-filter> <intent-filter> <action android:name="android.intent.action.LOCALE_CHANGED" /> </intent-filter> <intent-filter> <action android:name="android.intent.action.PACKAGE_REPLACED" /> <data android:scheme="package" /> </intent-filter></receiver><service android:name="com.ibm.mce.sdk.events.EventsAlarmListener" /><service android:name="com.ibm.mce.sdk.attributes.AttributesQueueConsumer" /><service android:name="com.ibm.mce.sdk.registration.RegistrationIntentService" /><service android:name="com.ibm.mce.sdk.registration.PhoneHomeIntentService" /><service android:name="com.ibm.mce.sdk.job.MceJobService" android:permission="android.permission.BIND_JOB_SERVICE"/><service android:name="com.ibm.mce.sdk.gcm.MceGcmListenerService" android:exported="false" > <intent-filter> <action android:name="com.google.android.c2dm.intent.RECEIVE" /> </intent-filter></service>
      6. Add the session tracking service if you want sessions to be timeout automatically, even if the application is in the background. This service is scheduled to check whether the current session timed out <session duration> + 1 minute after the application was sent to the background.
        <service android:name="com.ibm.mce.sdk.session.SessionTrackingIntentService"/>
      7. Set your application class to a class that extends com.ibm.mce.sdk.spi.MceApplication. Replace YOUR_APP_PACKAGE_NAME with your package name.

        <application android:name="YOUR_APP_PACKAGE_NAME.MyApplication" android:allowBackup="false" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme">

        You can use the following as a template for your application class:

        package YOUR_APP_PACKAGE_NAME;import android.annotation.TargetApi;import android.app.NotificationChannel;import android.app.NotificationManager;import android.content.Context;import android.os.Build;import com.ibm.mce.sdk.api.MceApplication;import com.ibm.mce.sdk.api.MceSdk;import com.ibm.mce.sdk.api.notification.NotificationsPreference;public class MyApplication extends MceApplication { public void onCreate() { super.onCreate(); if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { createNotificationChannel(getApplicationContext()); } } @TargetApi(26) private static void createNotificationChannel(Context context) { String MY_SAMPLE_NOTIFICATION_CHANNEL_ID = context.getString(R.string.notif_channel_id); NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); NotificationChannel channel = notificationManager.getNotificationChannel(MY_SAMPLE_NOTIFICATION_CHANNEL_ID); if(channel == null) { CharSequence name = context.getString(R.string.notif_channel_name); String description = context.getString(R.string.notif_channel_description); int importance = NotificationManager.IMPORTANCE_HIGH; channel = new NotificationChannel(MY_SAMPLE_NOTIFICATION_CHANNEL_ID, name, importance); channel.setDescription(description); NotificationsPreference notificationsPreference = MceSdk.getNotificationsClient().getNotificationsPreference(); notificationsPreference.setNotificationChannelId(context, MY_SAMPLE_NOTIFICATION_CHANNEL_ID); notificationManager.createNotificationChannel(channel); } }}

        The class above refers to the following that you will need to add to strings.xml:

         <string name="notif_channel_id">my-notification-channel</string> <string name="notif_channel_name">My sample notification channel</string> <string name="notif_channel_description">My sample notification channel description</string>
        
      8.  Configure your Android app not to back up inappropriate SDK files. See Automated Restores on Android.

    5. You have two options for setting properties – by using the MceConfig.json properties file or the MceSdkConfiguration object.The most common way is by copying MceConfig.json from one of the sample apps in the SDK into your app/src/main/assets directory. We also provide a way to configure via API.
      • MceConfig.json properties file
        To set MCE properties with the MceConfig.json, copy the MceConfig.json file from a sample app to the project’s assets folder. Create the assets folder under app/main/src if it doesn’t exist.

        The baseUrl, appKey, senderId and messagingService are required. All other values are optional.

        baseURL – Replace the server URL with your production URL. Typically this will be something like “https://sdk1.ibm.xtify.com/3.0” if you are on pod 1. To confirm the baseURL for your pod, see Setting the baseURL.

        appKey – Replace “YOUR APP KEY” with the app key generated from Watson Campaign Automation. Do not change the app key after you publish your app.

        senderId – Replace “YOUR GCM PROJECT ID” with the project number Google created for you for when you created a project.

        messagingService – Do not change from “gcm”.¬†

        Note: For information about other properties in the MceConfig.json, see these topics:

         

        MceConfig.json example

        "Note: If you are in Europe, you may need to change the value in 'baseUrl' below to": "https://sdk6.ibm.xtify.com",
         "baseUrl": "https://sdk.ibm.xtify.com/3.0",
        
         "appKey": {
         "prod":"YOUR APP KEY"
         },
         "Set this to the google project number if you are using GCM or to an empty string if you are using FCM": "",
         "senderId": "YOUR GCM PROJECT ID",
        
         "Use 'gcm' for GCM of 'fcm' for FCM": "",
         "messagingService": "gcm",
        
         "Will invalidate the existing user/channel ID if they exist when set to true": "This is for testing only. Do not ship with this set to true unless you understand what you are doing",
         "invalidateExistingUser": false,
        
         "Setting sessionEnabled to true will trigger session start and session end events": "",
         "sessionsEnabled": true,
        
         "sessionTimeout is the number of minutes the SDK wait while the application is in the background before sending session end event": "",
         "sessionTimeout": 20,
        
         "metricTimeInterval defines the interval in minutes between SDK events sync with the server. This is only for events that are sent to the event queue": "",
         "metricTimeInterval": 3,
        
         "Set groupNotificationsByAttribution to true to bundle together notifications with the same attribution": "This will work on Android N and above",
         "groupNotificationsByAttribution": false,
        
         "Choose one of the following values for loglevel: ": ["none", "error", "warn", "info", "debug", "verbose"],
         "loglevel": "error",
        
         "Please note, the log iteration files will be stored under": "<application cache folder on sdcard>/ibm-mce-android-sdk/logs/<installation time folder, format: yyyy-MM-dd-HH-mm-ss>",
         "logfile": false,
        
         "The number of log iteration files": "After reaching this number, the log files will be overwritten in order",
         "logIterations": 1,
        
         "The number of hours each log iteration will hold": "0 is for infinite time",
         "logIterationDurationInHours": 0,
        
         "The number of log lines that will be stored in the ram before writing to the log file": "The higher this number is, more log lines will be lost in case of a crash.",
         "logBufferSize": 10,
        
         "Set useInMemoryImageCache to true to use RAM cache for inbox images": "",
         "useInMemoryImageCache": true,
        
         "Set useFileImageCache to true to use file based cache for inbox images": "Uses the sdcard application cache folder",
         "useFileImageCache": true,
        
         "The size of the inbox images RAM cache in MB": "",
         "inMemoryImageCacheCapacityInMB": 20,
        
         "The size of the inbox images file based cache in MB": "",
         "fileImageCacheCapacityInMB": 100,
        
         "location is only required if location support is needed. Please note that unlike iOS, location support is not enabled automatically. Enabling location support is done in the application code (see documentation).": "",
         "location": {
         "The sync key is only used to customize the iBeacon and Geofence syncing service, it is not required for those features": "",
         "sync": {
         "Specify how long to wait before syncing again on significant location change in seconds, default 5 minutes":"",
         "syncInterval": 300,
        
         "Location Sync radius is in meters, default 100km":"",
         "syncRadius": 100000,
        
         "Specify how long to wait before retrieving a new location from the device, default 5 minutes":"",
         "locationResponsiveness": 300,
        
         "Specify the minimum results when looking for locations nearby, default is 1, minimum value is 1":"",
         "minLocationsForSearch": 1,
        
         "Specify the maximum results when looking for locations nearby, default is 1, minimum value is 1":"",
         "maxLocationsForSearch": 20,
        
         "Specify the location providers that will be used to retrieve the device location. 'gps' - gps location. 'network' - wifi + cellular, default is gps + network":"",
         "providerPreferences": ["gps", "network"] },
        
         "Please note, unlike iOS, the existence of the ibeacon key does not enable iBeacon support, iBeacon support is enabled only if iBeacons are nearby":"",
         "ibeacon": {
        
         "Please note: if this is not set, iBeacon events will not be sent": "This value should never be empty. Leave the default value if you don't want to use iBeacons",
         "uuid": "YOUR-IBEACONS-UUID",
        
         "Specify how much time the sdk will scan for iBeacons in every scan session while the application is in the foreground, default is 5 seconds": "",
         "beaconForegroundScanDuration": 5,
        
         "Specify how much time the sdk will wait between iBeacons scan sessions while the application is in the foreground, default is 30 seconds": "",
         "beaconForegroundScanInterval": 30,
        
         "Specify how much time the sdk will scan for iBeacons in every scan session while the application is in the background, default is 30 seconds": "",
         "beaconBackgroundScanDuration": 30,
        
         "Specify how much time the sdk will wait between iBeacons scan sessions while the application is in the background, default is 5 minutes": "",
         "beaconBackgroundScanInterval": 300
         }
         }
        }
        
      • MceSdkConfiguration object
        To set the SDK configuration in the application code by using the MceSdkConfiguration object, ensure that the application class is not an MceApplication and that you call the MceApplication init method with an MceSdkConfiguration object. If you do not set a config property, the default value is used. 
        For example,
        import com.ibm.mce.sdk.api.MceApplication;
        import com.ibm.mce.sdk.api.MceSdkConfiguration;
        import com.ibm.mce.sdk.api.SdkInitLifecycleCallbacks;
        
        import org.json.JSONArray;
        import org.json.JSONException;
        import org.json.JSONObject;
        
        public class SampleApplication extends Application {
        
         @Override
         public void onCreate() {
         super.onCreate();
         String appKey = "<YOUR APP KEY>";
         String senderId = "<YOUR SENDER ID>";
         MceSdkConfiguration mceSdkConfiguration = new MceSdkConfiguration(appKey, senderId);
        
         mceSdkConfiguration.setBaseUrl("<YOUR BASE URL>");
        
         mceSdkConfiguration.setMessagingService(MceSdkConfiguration.MessagingService.gcm);
         mceSdkConfiguration.setGroupNotificationsByAttribution(true);
        
         mceSdkConfiguration.setLogBufferSize(10);
         mceSdkConfiguration.setLogFile(true);
         mceSdkConfiguration.setLogIterationDurationInHours(1);
         mceSdkConfiguration.setLogLevel(Logger.LogLevel.error);
        
         mceSdkConfiguration.setMetricTimeInterval(3);
        
         mceSdkConfiguration.setSessionsEnabled(true);
         mceSdkConfiguration.setSessionTimeout(20);
         mceSdkConfiguration.setUseFileImageCache(true);
         mceSdkConfiguration.setUseInMemoryImageCache(true);
         mceSdkConfiguration.setFileImageCacheCapacityInMB(200);
         mceSdkConfiguration.setInMemoryImageCacheCapacityInMB(20);
         
         
         MceSdkConfiguration.LocationConfiguration.SyncConfiguration syncConfiguration = mceSdkConfiguration.getLocationConfiguration().getSyncConfiguration();
         try {
         syncConfiguration.setProviderPreferences(new JSONArray("[\"gps\", \"network\"]"));
         } catch (JSONException e) {
         }
         syncConfiguration.setLocationResponsiveness(300);
         syncConfiguration.setSyncInterval(300);
         syncConfiguration.setSyncRadius(100000);
         syncConfiguration.setMinLocationsForSearch(1);
         syncConfiguration.setMaxLocationsForSearch(20);
        
         MceSdkConfiguration.LocationConfiguration.IBeaconConfiguration iBeaconConfiguration = mceSdkConfiguration.getLocationConfiguration().getiBeaconConfiguration();
         iBeaconConfiguration.setUuid("<YOUR UUID>");
         iBeaconConfiguration.setBeaconForegroundScanDuration(5);
         iBeaconConfiguration.setBeaconForegroundScanInterval(30);
         iBeaconConfiguration.setBeaconBackgroundScanDuration(30);
         iBeaconConfiguration.setBeaconBackgroundScanInterval(300);
        
        
         MceApplication.init(this, mceSdkConfiguration, new SdkInitLifecycleCallbacks() {
         @Override
         public void handleMetadata(Bundle bundle) {
        
         }
        
         @Override
         public void onPluginActionLoad(JSONObject jsonObject) {
        
         }
        
         @Override
         public void onStart(MceSdkConfiguration mceSdkConfiguration) {
        
         }
         });
         }
        } 
        
    6. On new Android platforms, you will need to request some permissions explicitly. See Requesting location permissions on Android for details.
    7. Implement MceBroadcastReceiver if you want to monitor events in the Android SDK, such as SDK registration, GCM registration, GCM error, and notification received. Create a new MceBroadcastReceiver implementation class. For example:
      package YOUR_APP_PACKAGE;
      
      import android.content.Context;
      import java.util.Date;
      import android.content.Intent;
      import android.location.Location;
      import android.os.Bundle;
      import android.util.Log;
      
      import com.ibm.mce.sdk.api.MceBroadcastReceiver;
      import com.ibm.mce.sdk.api.attribute.AttributesOperation;
      import com.ibm.mce.sdk.api.event.Event;
      import com.ibm.mce.sdk.api.notification.NotificationDetails;
      import com.ibm.mce.sdk.location.MceLocation;
      import com.ibm.mce.sdk.api.broadcast.EventBroadcastUtil;
      
      import java.util.List;
      
      public class MyMceBroadcastReceiver extends MceBroadcastReceiver {
      
          @Override
          public void onSdkRegistered(Context context) {
              // Handle the SDK registration event
              // context - The application context
      
          }
      
          @Override
          public void onDeliveryChannelRegistered(Context context) {
              // Handle the FCM registration event
              // context - The application context
          }
      
          @Override
          public void onSdkRegistrationChanged(Context context) {
              // context - The application context
          }
      
          @Override
          public void onMessage(Context context, NotificationDetails notificationDetails, Bundle extraPayload) {
              // Handle the notification received event
              // context - The application context
              // notificationDetails - The received notification
              // extraPayload- Additional payload that arrived with the notification
          }
      
          @Override
          public void onSessionStart(Context context, Date sessionStartDate) {
              // context - The application context
              // sessionStartDate- The new session start time
          }
      
          @Override
          public void onSessionEnd(Context context, Date sessionEndDate, long sessionDurationInMinutes) {
              // context - The application context
              // sessionEndDate- The session end time
              // sessionDurationInMinutes - The session duration in minutes
          }
      
          @Override
          public void onNotificationAction(Context context, Date actionTime, String pushType, String actionType, String actionValue) {
              // context - The application context
              // actionTime- The time the action was clicked on
              // pushType - always "simple"
              // actionType - The type of the action
              // actionValue - the value of the "value" key in the payload.
          }
      
          @Override
          public void onAttributesOperation(Context context, AttributesOperation attributesOperation) {
              // context - The application context
              // attributesOperation - The operation that was executed
          }
      
      
          @Override
          public void onEventsSend(Context context, List<Event> list) {
              // context - The application context
              // events- The events that were sent
          }
      
          @Override
          public void onIllegalNotification(Context context, Intent intent) {
              // context - The application context
              // intent- The intent that contains the illegal notification
          }
      
          @Override
          public void onNonMceBroadcast(Context context, Intent intent) {
              // context - The application context
              // intent- The intent that contains the non MCE broadcast
          }
      
          /**
           * This method is called when a location event occurs
           * @param location The related location
           * @param locationType The related location type
           * @param locationEventType The related location event type
           */
          public void onLocationEvent(Context context, MceLocation location, LocationType locationType, LocationEventType locationEventType) {
              // do something interesting with the location event
          };
      
          /**
           * This method is called when the device location is updated
           * @param context The application's context
           * @param location The device location
           */
          public void onLocationUpdate(Context context, Location location) {
              // do something interesting with the location update
          }
      
          @Override
          public void onReceive(Context context, Intent intent) {
              Log.i(getClass().getSimpleName(), "Received intent: " + intent.toString());
              if(intent == null || intent.getAction() == null) {
                  return;
              }
              try {
                  EventBroadcastUtil.handleBroadcast(context, intent, this);
              } catch(Throwable t) {
                  Log.e(getClass().getSimpleName(), "Unexpected error on receive: ", t);
              }
          }
      
          @Override
          public void onC2dmError(Context context, String errorId) {
              Log.i(getClass().getSimpleName(), "C2DM errorId: " + errorId);
          }
      }
      

       

  4. Set up your emulator for testing.

    You can use the Android sample app in Android Studio to view and test the structure and features of the Android SDK. You use the sample app included in the SDK to teach you how to build a mobile app message and to show you the capabilities that are available. The sample app shows you the following information:

    • Credential and ID screens that display the following values: user ID, channel ID, development app key, and production app key.
    • A push action screen that displays the default and custom push actions that could be used when you send a mobile app message to your app users.
    • A custom action screen that displays details about the example categories you could use with the sample app.
    • Test events so you can send events to the server.
    • Test user attributes so you can send a user attribute to the server.

    You can test the Android SDK by using either Eclipse ADT or Gradle.

    1. Go to ibm-mobile-push-android-sdk-<version number>/samples/Demo. Make sure Demo is selected.
    2. Select the 1:Project tab.
    3. Go to app/src/main/assets/MceConfig.json and change appkey to your app key and senderid to your sender ID.
    4. If you’re not using the US server, go to app/assets/MceConfig.json and update your ‘baseUrl’ to your server’s URL. For example, eu is for European Union and a URL is for a different MCE server.
    5. When you create your Android Virtual Device (AVD), you must build a virtual device that is based on the Google Messaging API. A normal AVD results in silent failure.
      1. In Android Studio, select Tools > Android > SDK Manager, and make sure the “Google APIs” item is installed under the versions you are supporting.
      2. In Android Studio, select Tools > Android > AVD Manager.
        1. Select New, and enter a name.
        2. Under Target, make sure to select an option that uses Google APIs, such as Google APIs (Google Inc.) – API Level 16.
        3. After the device is created and started, you must also add an active Google account to the device through Settings > Accounts.
    6. Plug in an Android device into your machine, and run the app.
    7. Your emulator must be connected to a Google account for the GCM push to work. You can verify that the connection is initially working by sending an email to an account that was set up on your emulator.
     

Expected outcome

This tutorial takes you through the basic steps to configure mobile app messages in Android apps that use Google Cloud Messaging (GCM). For more information, see the Documentation.

Go Back to Mobile Application Messaging home page.

1 comment on"Getting Started with Mobile App Messaging in Android Apps"

  1. JoanGriffin April 05, 2017

    Please forward this comment to content owner.

Join The Discussion

Your email address will not be published. Required fields are marked *