Page updated Nov 14, 2023

Set up Amplify Push Notifications

The Push Notifications category allows you to integrate push notifications in your app with Amazon Pinpoint targeting, campaign, and journey management support. You can segment your users, trigger push notifications to your app, and record metrics in Pinpoint when users receive or open notifications. Amazon Pinpoint helps you to create messaging campaigns and journeys targeted to specific user segments or demographics and collect interaction metrics with push notifications.

Prerequisites

  • An Android application targeting at least Android SDK API level 24 with Amplify libraries integrated

Push Notifications are delivered via Firebase Cloud Messaging (FCM). In order to use FCM, you need to register your app on the Firebase console. See Setting up push notification services for more information.

Request Permissions

Android 13 (API level 33) introduces a runtime permission for sending non-exempt notifications, including Foreground Services (FGS), from your app. To enable this permission, follow these instructions.

For Android versions v12 or lower, permission modals will appear during channel creation and app startup by default. However, starting from Android v13 (API Level 33) and higher, you can control the timing of the permissions modal by using the Notifications.Push.getPermissionStatus() method. The appearance of the permissions modal will be determined by your app's target SDK version.

Set up backend resources

To use Push Notifications with Amplify, you have the option to either have the Amplify CLI setup resources for you, or you can use an existing Amazon Pinpoint resource in your AWS account.

Prerequisite: Install and configure the Amplify CLI

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

To start provisioning push notification resources in the backend, go to your project directory and execute the command:

1amplify add notifications

Choose FCM when promoted:

1? Choose the push notification channel to enable.
2 APNS |  Apple Push Notifications
3❯ FCM | » Firebase Push Notifications
4 In-App Messaging
5 Email
6 SMS
7
8? Provide your pinpoint resource name:
9 `yourPinpointResourceName`
10
11? Apps need authorization to send analytics events. Do you want to allow guests and unauthenticated users to send analytics events? (we recommend you allow this when getting started) (Y/n)
12 'Y'

The CLI will prompt for your Server Key, paste the Token you copied while setting up push notification services.

Upon completion, amplifyconfiguration.json will be updated to reference the newly provisioned backend push notifications resources. Note that this file should already be generated for you by the Amplify CLI as a part of your project if you followed the project setup walkthrough.

Amplify reads configuration information at runtime from a file called amplifyconfiguration.json. If you used the Amplify CLI to create backend resources, this file was created for you. If it does not already exist, create the file and add it to your Android project.

Existing Amazon Pinpoint resources can be used with the Amplify Libraries by referencing your Application ID and Region in your amplifyconfiguration.json file.

1"notifications": {
2 "plugins": {
3 "awsPinpointPushNotificationsPlugin": {
4 "appId": "[APP ID]",
5 "region": "[REGION]"
6 }
7 }
8}
  • appId: Amazon Pinpoint application ID
  • region: AWS Region where the resources are provisioned (e.g. us-east-1)

Install Amplify Libraries

Open build.gradle (Module :app) and add these libraries into the dependencies block.

1dependencies {
2 implementation 'com.amplifyframework:aws-auth-cognito:ANDROID_VERSION'
3 implementation 'com.amplifyframework:aws-push-notifications-pinpoint:ANDROID_VERSION'
4}

Click Sync Now when prompted.

Add the following service to your app's AndroidManifest.xml file:

1<application ...>
2 ...
3 <service android:name="com.amplifyframework.pushnotifications.pinpoint.FCMPushNotificationService"
4 android:exported="false">
5 <intent-filter>
6 <action android:name="com.google.firebase.MESSAGING_EVENT" />
7 </intent-filter>
8 </service>
9 ...
10</application>

Initialize Amplify Push Notifications

To initialize the Amplify Push Notifications, you will use the Amplify.addPlugin() method to add the AWSCognitoAuthPlugin and the AWSPinpointPushNotificationsPlugin and then call Amplify.configure() to finish configuring Amplify. Displaying push notifications from FCM requires registering mobile devices with the FCM service. The AWSPinpointPushNotificationsPlugin takes care of registering the device with FCM during configuration, and automatically records analytics in Amazon Pinpoint.

Add the following code to your onCreate() method in your application class:

1import android.util.Log;
2import com.amplifyframework.AmplifyException;
3import com.amplifyframework.auth.cognito.AWSCognitoAuthPlugin;
4import com.amplifyframework.core.Amplify;
5import com.amplifyframework.pushnotifications.pinpoint.AWSPinpointPushNotificationsPlugin;
1Amplify.addPlugin(new AWSCognitoAuthPlugin());
2Amplify.addPlugin(new AWSPinpointPushNotificationsPlugin());

Your class will look like this:

1public class MyAmplifyApp extends Application {
2 @Override
3 public void onCreate() {
4 super.onCreate();
5
6 try {
7 // Add these lines to add the AWSCognitoAuthPlugin and AWSPinpointPushNotificationsPlugin plugins
8 Amplify.addPlugin(new AWSCognitoAuthPlugin());
9 Amplify.addPlugin(new AWSPinpointPushNotificationsPlugin());
10 Amplify.configure(getApplicationContext());
11
12 Log.i("MyAmplifyApp", "Initialized Amplify");
13 } catch (AmplifyException error) {
14 Log.e("MyAmplifyApp", "Could not initialize Amplify", error);
15 }
16 }
17}
1import android.util.Log
2import com.amplifyframework.AmplifyException
3import com.amplifyframework.auth.cognito.AWSCognitoAuthPlugin
4import com.amplifyframework.core.Amplify
5import com.amplifyframework.pushnotifications.pinpoint.AWSPinpointPushNotificationsPlugin
1Amplify.addPlugin(AWSCognitoAuthPlugin())
2Amplify.addPlugin(AWSPinpointPushNotificationsPlugin())

Your class will look like this:

1class MyAmplifyApp : Application() {
2 override fun onCreate() {
3 super.onCreate()
4
5 try {
6 // Add these lines to add the AWSCognitoAuthPlugin and AWSPinpointPushNotificationsPlugin plugins
7 Amplify.addPlugin(AWSCognitoAuthPlugin())
8 Amplify.addPlugin(AWSPinpointPushNotificationsPlugin())
9 Amplify.configure(applicationContext)
10
11 Log.i("MyAmplifyApp", "Initialized Amplify")
12 } catch (error: AmplifyException) {
13 Log.e("MyAmplifyApp", "Could not initialize Amplify", error)
14 }
15 }
16}
1import android.util.Log;
2import com.amplifyframework.AmplifyException;
3import com.amplifyframework.auth.cognito.AWSCognitoAuthPlugin;
4import com.amplifyframework.pushnotifications.pinpoint.AWSPinpointPushNotificationsPlugin;
5import com.amplifyframework.rx.RxAmplify;
1RxAmplify.addPlugin(AWSCognitoAuthPlugin());
2RxAmplify.addPlugin(AWSPinpointPushNotificationsPlugin());

Your class will look like this:

1public class MyAmplifyApp extends Application {
2 @Override
3 public void onCreate() {
4 super.onCreate();
5
6 try {
7 // Add these lines to add the AWSCognitoAuthPlugin and AWSPinpointPushNotificationsPlugin plugins
8 RxAmplify.addPlugin(new AWSCognitoAuthPlugin());
9 RxAmplify.addPlugin(new AWSPinpointPushNotificationsPlugin());
10 RxAmplify.configure(getApplicationContext());
11
12 Log.i("MyAmplifyApp", "Initialized Amplify");
13 } catch (AmplifyException error) {
14 Log.e("MyAmplifyApp", "Could not initialize Amplify", error);
15 }
16 }
17}

Upon building and running this application you should see the following in your console window:

1Initialized Amplify