Page updated Nov 8, 2023

Record notification events

You do not need to add these APIs to your app if you want to use Amplify with Amazon Pinpoint. You should use these APIs if you are interested in supporting multiple push providers.

Handle Notification Received

You can display the notification in the system tray if the app is in a background or killed state by calling Amplify.Notifications.Push.handleNotificationReceived() from the Messaging Service class. It also calls recordNotificationReceived irrespective of the app state when the device receives a notifications from Pinpoint.

1class MyAppService extends FirebaseMessagingService() {
2 @Override
3 public void onMessageReceived(RemoteMessage remoteMessage) {
4 // Convert the RemoteMessage into a NotificationPayload
5 NotificationPayload notificationPayload = NotificationPayload.builder(
6 new NotificationContentProvider.FCM(remoteMessage.getData())
7 ).build();
8
9 // Amplify should handle notification if it is sent from Pinpoint
10 boolean isAmplifyMessage = Amplify.Notifications.Push.shouldHandleNotification(notificationPayload);
11 if (isAmplifyMessage) {
12 // let Amplify handle foreground and background message received
13 Amplify.Notifications.Push.handleNotificationReceived(notificationPayload,
14 result -> Log.i("MyAmplifyApp", "Successfully handled a notification"),
15 error -> Log.e("MyAmplifyApp", "Error handling notification", error)
16 );
17 }
18 }
19}
1class MyAppService : FirebaseMessagingService() {
2 override fun onMessageReceived(remoteMessage: RemoteMessage) {
3 // Convert the RemoteMessage into a NotificationPayload
4 val notificationPayload = NotificationPayload(NotificationContentProvider.FCM(remoteMessage.data))
5
6 // Amplify should handle notification if it is sent from Pinpoint
7 val isAmplifyMessage = Amplify.Notifications.Push.shouldHandleNotification(notificationPayload)
8 if (isAmplifyMessage) {
9 // let Amplify handle foreground and background message received
10 Amplify.Notifications.Push.handleNotificationReceived(notificationPayload,
11 { Log.i("MyAmplifyApp", "Successfully handled a notification") },
12 { Log.e("MyAmplifyApp", "Error handling notification", error) }
13 )
14 }
15 }
16}
1class MyAppService : FirebaseMessagingService() {
2 override fun onMessageReceived(remoteMessage: RemoteMessage) {
3 // Convert the RemoteMessage into a NotificationPayload
4 val notificationPayload = NotificationPayload(NotificationContentProvider.FCM(remoteMessage.data))
5
6 // Amplify should handle notification if it is sent from Pinpoint
7 val isAmplifyMessage = Amplify.Notifications.Push.shouldHandleNotification(notificationPayload)
8 if (isAmplifyMessage) {
9 try {
10 // let Amplify handle foreground and background message received
11 Amplify.Notifications.Push.handleNotificationReceived(notificationPayload)
12 Log.i("MyAmplifyApp", "Successfully handled a notification")
13 } catch (error: PushNotificationsException) {
14 Log.e("MyAmplifyApp", "Error handling notification", error)
15 }
16 }
17 }
18}
1class MyAppService extends FirebaseMessagingService() {
2 @Override
3 public void onMessageReceived(RemoteMessage remoteMessage) {
4 // Convert the RemoteMessage into a NotificationPayload
5 NotificationPayload notificationPayload = NotificationPayload.builder(
6 new NotificationContentProvider.FCM(remoteMessage.getData())
7 ).build();
8
9 // Amplify should handle notification if it is sent from Pinpoint
10 boolean isAmplifyMessage = Amplify.Notifications.Push.shouldHandleNotification(notificationPayload);
11 if (isAmplifyMessage) {
12 // let Amplify handle foreground and background message received
13 RxAmplify.Notifications.Push.handleNotificationReceived(notificationPayload).subscribe(
14 result -> Log.i("MyAmplifyApp", "Successfully handled a notification"),
15 error -> Log.e("MyAmplifyApp", "Error handling notification", error)
16 );
17 }
18 }
19}

Amazon Pinpoint enables you to record when users receive or open push notifications in order to gather metrics on the effectiveness of customer engagement campaigns. The Amplify Push Notifications plugin provides a simple API to record and send these events to Amazon Pinpoint.

Record Notification Received

You can record receipt of a push notification with Amazon Pinpoint by calling Amplify.Notifications.Push.recordNotificationReceived() from the Messaging Service class.

You should call either handleNotificationReceived or recordNotificationReceived, but not both since handleNotificationReceived internally calls recordNotificationReceived.

1class MyAppService extends FirebaseMessagingService() {
2 @Override
3 public void onMessageReceived(RemoteMessage remoteMessage) {
4 // Convert the RemoteMessage into a NotificationPayload
5 NotificationPayload notificationPayload = NotificationPayload.builder(
6 new NotificationContentProvider.FCM(remoteMessage.getData())
7 ).build();
8
9 // Amplify should handle notification if it is sent from Pinpoint
10 boolean isAmplifyMessage = Amplify.Notifications.Push.shouldHandleNotification(notificationPayload);
11 if (isAmplifyMessage) {
12 // Record notification received with Amplify
13 Amplify.Notifications.Push.recordNotificationReceived(notificationPayload,
14 () -> Log.i("MyAmplifyApp", "Successfully recorded a notification received"),
15 error -> Log.e("MyAmplifyApp", "Error recording notification received", error)
16 );
17 }
18 }
19}
1class MyAppService : FirebaseMessagingService() {
2 override fun onMessageReceived(remoteMessage: RemoteMessage) {
3 // Convert the RemoteMessage into a NotificationPayload
4 val notificationPayload = NotificationPayload(NotificationContentProvider.FCM(remoteMessage.data))
5
6 // Amplify should handle notification if it is sent from Pinpoint
7 val isAmplifyMessage = Amplify.Notifications.Push.shouldHandleNotification(notificationPayload)
8 if (isAmplifyMessage) {
9 // Record notification received with Amplify
10 Amplify.Notifications.Push.recordNotificationReceived(notificationPayload,
11 { Log.i("MyAmplifyApp", "Successfully recorded a notification received") },
12 { Log.e("MyAmplifyApp", "Error recording notification received", error) }
13 )
14 }
15 }
16}
1class MyAppService : FirebaseMessagingService() {
2 override fun onMessageReceived(remoteMessage: RemoteMessage) {
3 // Convert the RemoteMessage into a NotificationPayload
4 val notificationPayload = NotificationPayload(NotificationContentProvider.FCM(remoteMessage.data))
5
6 // Amplify should handle notification if it is sent from Pinpoint
7 val isAmplifyMessage = Amplify.Notifications.Push.shouldHandleNotification(notificationPayload)
8 if (isAmplifyMessage) {
9 try {
10 // let Amplify handle foreground and background message received
11 Amplify.Notifications.Push.recordNotificationReceived(notificationPayload)
12 Log.i("MyAmplifyApp", "Successfully recorded a notification received")
13 } catch (error: PushNotificationsException) {
14 Log.e("MyAmplifyApp", "Error recording notification received", error)
15 }
16 }
17 }
18}
1class MyAppService extends FirebaseMessagingService() {
2 @Override
3 public void onMessageReceived(RemoteMessage remoteMessage) {
4 // Convert the RemoteMessage into a NotificationPayload
5 NotificationPayload notificationPayload = NotificationPayload.builder(
6 new NotificationContentProvider.FCM(remoteMessage.getData())
7 ).build();
8
9 // Amplify should handle notification if it is sent from Pinpoint
10 boolean isAmplifyMessage = Amplify.Notifications.Push.shouldHandleNotification(notificationPayload);
11 if (isAmplifyMessage) {
12 // let Amplify handle foreground and background message received
13 RxAmplify.Notifications.Push.recordNotificationReceived(notificationPayload).subscribe(
14 () -> Log.i("MyAmplifyApp", "Successfully recorded a notification received"),
15 error -> Log.e("MyAmplifyApp", "Error recording notification received", error)
16 );
17 }
18 }
19}

Record Notification Opened

You can record that a user opened a push notification with Amazon Pinpoint by calling Amplify.Notifications.Push.recordNotificationOpened() from the launch activity.

1class LaunchActivity extends AppCompatActivity() {
2 @Override
3 public void onCreate(Bundle savedInstanceState) {
4 super.onCreate(savedInstanceState);
5
6 // Get the payload from the intent
7 NotificationPayload payload = NotificationPayload.fromIntent(getIntent());
8
9 if (payload != null) {
10 // Record notification opened when activity launches
11 Amplify.Notifications.Push.recordNotificationOpened(payload,
12 () -> Log.i("MyAmplifyApp", "Successfully recorded notification opened"),
13 error -> Log.e("MyAmplifyApp", "Error recording notification opened", error)
14 );
15 }
16 }
17}
1class LaunchActivity : AppCompatActivity() {
2 override fun onCreate(savedInstanceState: Bundle?) {
3 super.onCreate(savedInstanceState)
4
5 // Get the payload from the intent
6 NotificationPayload.fromIntent(intent)?.let {
7 // Record notification opened when activity launches
8 Amplify.Notifications.Push.recordNotificationOpened(it,
9 { Log.i("MyAmplifyApp", "Successfully recorded notification opened") },
10 { Log.e("MyAmplifyApp", "Error recording notification opened", error) }
11 )
12 }
13 }
14}
1class LaunchActivity : AppCompatActivity() {
2 override fun onCreate(savedInstanceState: Bundle?) {
3 super.onCreate(savedInstanceState)
4
5 try {
6 // Get the payload from the intent
7 NotificationPayload.fromIntent(intent)?.let {
8 // Record notification opened when activity launches
9 Amplify.Notifications.Push.recordNotificationOpened(it)
10 Log.i("MyAmplifyApp", "Successfully recorded notification opened")
11 }
12 } catch (error: PushNotificationsException) {
13 Log.e("MyAmplifyApp", "Error recording notification opened", error)
14 }
15 }
16}
1class LaunchActivity extends AppCompatActivity() {
2 @Override
3 public void onCreate(Bundle savedInstanceState) {
4 super.onCreate(savedInstanceState);
5
6 NotificationPayload payload = NotificationPayload.fromIntent(getIntent());
7
8 if (payload != null) {
9 // Record notification opened when activity launches
10 RxAmplify.Notifications.Push.recordNotificationOpened(payload).subscribe(
11 () -> Log.i("MyAmplifyApp", "Successfully recorded notification opened"),
12 error -> Log.e("MyAmplifyApp", "Error recording notification opened", error)
13 );
14 }
15 }
16}