Getting started

You are currently viewing the AWS SDK for Mobile documentation which is a collection of low-level libraries. Use the Amplify libraries for all new app development. Learn more

Enable your users to receive mobile push messages sent from the Apple (APNs) and Google (FCM/GCM) platforms. The CLI deploys your push notification backend using Amazon Pinpoint. You can also create Amazon Pinpoint campaigns that tie user behavior to push or other forms of messaging.

Set Up Your Backend

  1. Complete the Get Started steps before you proceed.

  2. Use the CLI to add storage to your cloud-enabled backend and app.

    In a terminal window, navigate to your project folder (the folder that typically contains your project level build.gradle), and add the SDK to your app.

    1cd YOUR_PROJECT_FOLDER
    2amplify add notifications
  3. Set up your backend to support receiving push notifications:

    • Choose Firebase Cloud Messaging (FCM).

      1> FCM
    • Provide your Server Key. For information on getting an FCM Server Key, see the section Setting Up FCM/GCM Guide. Use the steps in the next section to connect your app to your backend.

Connect to Your Backend

Use the following steps to connect your app to the push notification backend services.

  1. Add the following dependencies and plugin to your app/build.gradle:

    1dependencies {
    2 // Overrides an auth dependency to ensure correct behavior
    3 implementation 'com.google.android.gms:play-services-auth:19.2.0'
    4
    5 // Import the BoM for the Firebase platform
    6 implementation platform('com.google.firebase:firebase-bom:28.2.1')
    7 implementation 'com.google.firebase:firebase-messaging'
    8
    9 implementation 'com.amazonaws:aws-android-sdk-pinpoint:ANDROID_SDK_VERSION'
    10 implementation 'com.amazonaws:aws-android-sdk-mobile-client:ANDROID_SDK_VERSION'
    11}
    12
    13apply plugin: 'com.google.gms.google-services'
  2. Add the following to your project level build.gradle. Make sure that you specify the google repository:

    1buildscript {
    2 dependencies {
    3 classpath 'com.google.gms:google-services:4.0.1'
    4 }
    5}
    6
    7allprojects {
    8 repositories {
    9 google()
    10 }
    11}
  3. AndroidManifest.xml must contain the definition of the following service for PushListenerService in the application tag:

    1<service
    2 android:name=".PushListenerService">
    3 <intent-filter>
    4 <action android:name="com.google.firebase.MESSAGING_EVENT"/>
    5 </intent-filter>
    6</service>
  4. Create the Amazon Pinpoint client in your custom Application class. This is necessary to ensure Pinpoint is initialized before the push notification Intent is handled.

    1import android.app.Application;
    2import android.content.Context;
    3import android.util.Log;
    4import android.support.annotation.NonNull;
    5
    6import com.amazonaws.mobile.client.AWSMobileClient;
    7import com.amazonaws.mobile.client.Callback;
    8import com.amazonaws.mobile.client.UserStateDetails;
    9import com.amazonaws.mobile.config.AWSConfiguration;
    10import com.amazonaws.mobileconnectors.pinpoint.PinpointConfiguration;
    11import com.amazonaws.mobileconnectors.pinpoint.PinpointManager;
    12import com.google.android.gms.tasks.OnCompleteListener;
    13import com.google.android.gms.tasks.Task;
    14import com.google.firebase.messaging.FirebaseMessaging;
    15
    16public class MyPinpointApp extends Application {
    17 public static final String TAG = MyPinpointApp.class.getSimpleName();
    18
    19 private static PinpointManager pinpointManager;
    20
    21 @Override
    22 public void onCreate() {
    23 super.onCreate();
    24
    25 // Initialize PinpointManager
    26 getPinpointManager(getApplicationContext());
    27 }
    28
    29 public static PinpointManager getPinpointManager(final Context applicationContext) {
    30 if (pinpointManager == null) {
    31 final AWSConfiguration awsConfig = new AWSConfiguration(applicationContext);
    32 AWSMobileClient.getInstance().initialize(applicationContext, awsConfig, new Callback<UserStateDetails>() {
    33 @Override
    34 public void onResult(UserStateDetails userStateDetails) {
    35 Log.i("INIT", "User State: " + userStateDetails.getUserState());
    36 }
    37
    38 @Override
    39 public void onError(Exception e) {
    40 Log.e("INIT", "Initialization error.", e);
    41 }
    42 });
    43
    44 PinpointConfiguration pinpointConfig = new PinpointConfiguration(
    45 applicationContext,
    46 AWSMobileClient.getInstance(),
    47 awsConfig);
    48
    49 pinpointManager = new PinpointManager(pinpointConfig);
    50
    51 FirebaseMessaging.getInstance().getToken()
    52 .addOnCompleteListener(new OnCompleteListener<String>() {
    53 @Override
    54 public void onComplete(@NonNull Task<String> task) {
    55 if (!task.isSuccessful()) {
    56 Log.w(TAG, "Fetching FCM registration token failed", task.getException());
    57 return;
    58 }
    59 final String token = task.getResult();
    60 Log.d(TAG, "Registering push notifications token: " + token);
    61 pinpointManager.getNotificationClient().registerDeviceToken(token);
    62 }
    63 });
    64 }
    65 return pinpointManager;
    66 }
    67}
  5. Next, configure your application to use your custom Application class. Open the AndroidManifest.xml file located in your project directory at app/src/main/AndroidManifest.xml.

    Add the android:name attribute to the application node. For example, if the application name is MyPinpointApp and the new class is named MyPinpointApplication, the update to the AndroidManifest.xml file looks as follows:

    1<?xml version="1.0" encoding="utf-8"?>
    2<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    3 package="com.example.MyPinpointApp">
    4
    5 <!-- Add the android:name attribute to the application node -->
    6 <application
    7 android:name=".MyPinpointApplication"
    8 ...
    9 </application>
    10</manifest>