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.1cd YOUR_PROJECT_FOLDER2amplify add notifications -
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 FCMServer 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
:1dependencies {2 // Overrides an auth dependency to ensure correct behavior3 implementation 'com.google.android.gms:play-services-auth:19.2.0'45 // Import the BoM for the Firebase platform6 implementation platform('com.google.firebase:firebase-bom:28.2.1')7 implementation 'com.google.firebase:firebase-messaging'89 implementation 'com.amazonaws:aws-android-sdk-pinpoint:ANDROID_SDK_VERSION'10 implementation 'com.amazonaws:aws-android-sdk-mobile-client:ANDROID_SDK_VERSION'11}1213apply plugin: 'com.google.gms.google-services' -
Add the following to your project level
build.gradle
. Make sure that you specify thegoogle
repository:1buildscript {2 dependencies {3 classpath 'com.google.gms:google-services:4.0.1'4 }5}67allprojects {8 repositories {9 google()10 }11} -
AndroidManifest.xml
must contain the definition of the following service forPushListenerService
in the application tag:1<service2 android:name=".PushListenerService">3 <intent-filter>4 <action android:name="com.google.firebase.MESSAGING_EVENT"/>5 </intent-filter>6</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.1import android.app.Application;2import android.content.Context;3import android.util.Log;4import android.support.annotation.NonNull;56import 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;1516public class MyPinpointApp extends Application {17 public static final String TAG = MyPinpointApp.class.getSimpleName();1819 private static PinpointManager pinpointManager;2021 @Override22 public void onCreate() {23 super.onCreate();2425 // Initialize PinpointManager26 getPinpointManager(getApplicationContext());27 }2829 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 @Override34 public void onResult(UserStateDetails userStateDetails) {35 Log.i("INIT", "User State: " + userStateDetails.getUserState());36 }3738 @Override39 public void onError(Exception e) {40 Log.e("INIT", "Initialization error.", e);41 }42 });4344 PinpointConfiguration pinpointConfig = new PinpointConfiguration(45 applicationContext,46 AWSMobileClient.getInstance(),47 awsConfig);4849 pinpointManager = new PinpointManager(pinpointConfig);5051 FirebaseMessaging.getInstance().getToken()52 .addOnCompleteListener(new OnCompleteListener<String>() {53 @Override54 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} -
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:1<?xml version="1.0" encoding="utf-8"?>2<manifest xmlns:android="http://schemas.android.com/apk/res/android"3 package="com.example.MyPinpointApp">45 <!-- Add the android:name attribute to the application node -->6 <application7 android:name=".MyPinpointApplication"8 ...9 </application>10</manifest>