Name:
interface
Value:
Amplify has re-imagined the way frontend developers build fullstack applications. Develop and deploy without the hassle.

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.

Push Notifications requires version 12.12.3+ of the Amplify CLI. You can check your current version with amplify -version and upgrade to the latest version with amplify upgrade.

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.

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

    • Choose Firebase Cloud Messaging (FCM).

      > FCM
    • Provide your service account key (json file) path (relative or absolute). For information on getting an FCM service account 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:

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

    buildscript {
    dependencies {
    classpath 'com.google.gms:google-services:4.0.1'
    }
    }
    allprojects {
    repositories {
    google()
    }
    }
  3. AndroidManifest.xml must contain the definition of the following service for PushListenerService in the application tag:

    <service
    android:name=".PushListenerService">
    <intent-filter>
    <action android:name="com.google.firebase.MESSAGING_EVENT"/>
    </intent-filter>
    </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.

    import android.app.Application;
    import android.content.Context;
    import android.util.Log;
    import android.support.annotation.NonNull;
    import com.amazonaws.mobile.client.AWSMobileClient;
    import com.amazonaws.mobile.client.Callback;
    import com.amazonaws.mobile.client.UserStateDetails;
    import com.amazonaws.mobile.config.AWSConfiguration;
    import com.amazonaws.mobileconnectors.pinpoint.PinpointConfiguration;
    import com.amazonaws.mobileconnectors.pinpoint.PinpointManager;
    import com.google.android.gms.tasks.OnCompleteListener;
    import com.google.android.gms.tasks.Task;
    import com.google.firebase.messaging.FirebaseMessaging;
    public class MyPinpointApp extends Application {
    public static final String TAG = MyPinpointApp.class.getSimpleName();
    private static PinpointManager pinpointManager;
    @Override
    public void onCreate() {
    super.onCreate();
    // Initialize PinpointManager
    getPinpointManager(getApplicationContext());
    }
    public static PinpointManager getPinpointManager(final Context applicationContext) {
    if (pinpointManager == null) {
    final AWSConfiguration awsConfig = new AWSConfiguration(applicationContext);
    AWSMobileClient.getInstance().initialize(applicationContext, awsConfig, new Callback<UserStateDetails>() {
    @Override
    public void onResult(UserStateDetails userStateDetails) {
    Log.i("INIT", "User State: " + userStateDetails.getUserState());
    }
    @Override
    public void onError(Exception e) {
    Log.e("INIT", "Initialization error.", e);
    }
    });
    PinpointConfiguration pinpointConfig = new PinpointConfiguration(
    applicationContext,
    AWSMobileClient.getInstance(),
    awsConfig);
    pinpointManager = new PinpointManager(pinpointConfig);
    FirebaseMessaging.getInstance().getToken()
    .addOnCompleteListener(new OnCompleteListener<String>() {
    @Override
    public void onComplete(@NonNull Task<String> task) {
    if (!task.isSuccessful()) {
    Log.w(TAG, "Fetching FCM registration token failed", task.getException());
    return;
    }
    final String token = task.getResult();
    Log.d(TAG, "Registering push notifications token: " + token);
    pinpointManager.getNotificationClient().registerDeviceToken(token);
    }
    });
    }
    return pinpointManager;
    }
    }
  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:

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.MyPinpointApp">
    <!-- Add the android:name attribute to the application node -->
    <application
    android:name=".MyPinpointApplication"
    ...
    </application>
    </manifest>