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

Page updated Feb 21, 2024

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 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.

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

amplify add notifications

Choose FCM when promoted:

? Choose the push notification channel to enable.
APNS |  Apple Push Notifications
❯ FCM | » Firebase Push Notifications
In-App Messaging
Email
SMS
? Provide your pinpoint resource name:
`yourPinpointResourceName`
? 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)
'Y'

The CLI will prompt for your service account key (json file) path created from steps in setting up push notification services. Enter the absolute path, or a path relative to the location where you ran the command.

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.

"notifications": {
"plugins": {
"awsPinpointPushNotificationsPlugin": {
"appId": "[APP ID]",
"region": "[REGION]"
}
}
}
  • 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.

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

Click Sync Now when prompted.

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

<application ...>
...
<service android:name="com.amplifyframework.pushnotifications.pinpoint.FCMPushNotificationService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
...
</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:

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

Your class will look like this:

public class MyAmplifyApp extends Application {
@Override
public void onCreate() {
super.onCreate();
try {
// Add these lines to add the AWSCognitoAuthPlugin and AWSPinpointPushNotificationsPlugin plugins
Amplify.addPlugin(new AWSCognitoAuthPlugin());
Amplify.addPlugin(new AWSPinpointPushNotificationsPlugin());
Amplify.configure(getApplicationContext());
Log.i("MyAmplifyApp", "Initialized Amplify");
} catch (AmplifyException error) {
Log.e("MyAmplifyApp", "Could not initialize Amplify", error);
}
}
}
import android.util.Log
import com.amplifyframework.AmplifyException
import com.amplifyframework.auth.cognito.AWSCognitoAuthPlugin
import com.amplifyframework.core.Amplify
import com.amplifyframework.pushnotifications.pinpoint.AWSPinpointPushNotificationsPlugin
Amplify.addPlugin(AWSCognitoAuthPlugin())
Amplify.addPlugin(AWSPinpointPushNotificationsPlugin())

Your class will look like this:

class MyAmplifyApp : Application() {
override fun onCreate() {
super.onCreate()
try {
// Add these lines to add the AWSCognitoAuthPlugin and AWSPinpointPushNotificationsPlugin plugins
Amplify.addPlugin(AWSCognitoAuthPlugin())
Amplify.addPlugin(AWSPinpointPushNotificationsPlugin())
Amplify.configure(applicationContext)
Log.i("MyAmplifyApp", "Initialized Amplify")
} catch (error: AmplifyException) {
Log.e("MyAmplifyApp", "Could not initialize Amplify", error)
}
}
}
import android.util.Log;
import com.amplifyframework.AmplifyException;
import com.amplifyframework.auth.cognito.AWSCognitoAuthPlugin;
import com.amplifyframework.pushnotifications.pinpoint.AWSPinpointPushNotificationsPlugin;
import com.amplifyframework.rx.RxAmplify;
RxAmplify.addPlugin(AWSCognitoAuthPlugin());
RxAmplify.addPlugin(AWSPinpointPushNotificationsPlugin());

Your class will look like this:

public class MyAmplifyApp extends Application {
@Override
public void onCreate() {
super.onCreate();
try {
// Add these lines to add the AWSCognitoAuthPlugin and AWSPinpointPushNotificationsPlugin plugins
RxAmplify.addPlugin(new AWSCognitoAuthPlugin());
RxAmplify.addPlugin(new AWSPinpointPushNotificationsPlugin());
RxAmplify.configure(getApplicationContext());
Log.i("MyAmplifyApp", "Initialized Amplify");
} catch (AmplifyException error) {
Log.e("MyAmplifyApp", "Could not initialize Amplify", error);
}
}
}

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

Initialized Amplify