Getting started
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
-
Complete the Get Started steps before you proceed.
-
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.
cd YOUR_PROJECT_FOLDERamplify add notifications
-
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 FCMservice 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.
-
Add the following dependencies and plugin to your
app/build.gradle
:dependencies {// Overrides an auth dependency to ensure correct behaviorimplementation 'com.google.android.gms:play-services-auth:19.2.0'// Import the BoM for the Firebase platformimplementation 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' -
Add the following to your project level
build.gradle
. Make sure that you specify thegoogle
repository:buildscript {dependencies {classpath 'com.google.gms:google-services:4.0.1'}}allprojects {repositories {google()}} -
AndroidManifest.xml
must contain the definition of the following service forPushListenerService
in the application tag:<serviceandroid:name=".PushListenerService"><intent-filter><action android:name="com.google.firebase.MESSAGING_EVENT"/></intent-filter></service> -
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;@Overridepublic void onCreate() {super.onCreate();// Initialize PinpointManagergetPinpointManager(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>() {@Overridepublic void onResult(UserStateDetails userStateDetails) {Log.i("INIT", "User State: " + userStateDetails.getUserState());}@Overridepublic 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>() {@Overridepublic 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;}} -
Next, configure your application to use your custom
Application class
. Open theAndroidManifest.xml
file located in your project directory atapp/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 theAndroidManifest.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 --><applicationandroid:name=".MyPinpointApplication"...</application></manifest>