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.

class MyAppService extends FirebaseMessagingService() { @Override public void onMessageReceived(RemoteMessage remoteMessage) { // Convert the RemoteMessage into a NotificationPayload NotificationPayload notificationPayload = NotificationPayload.builder( new NotificationContentProvider.FCM(remoteMessage.getData()) ).build(); // Amplify should handle notification if it is sent from Pinpoint boolean isAmplifyMessage = Amplify.Notifications.Push.shouldHandleNotification(notificationPayload); if (isAmplifyMessage) { // let Amplify handle foreground and background message received Amplify.Notifications.Push.handleNotificationReceived(notificationPayload, result -> Log.i("MyAmplifyApp", "Successfully handled a notification"), error -> Log.e("MyAmplifyApp", "Error handling notification", error) ); } } }
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}

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.

class MyAppService extends FirebaseMessagingService() { @Override public void onMessageReceived(RemoteMessage remoteMessage) { // Convert the RemoteMessage into a NotificationPayload NotificationPayload notificationPayload = NotificationPayload.builder( new NotificationContentProvider.FCM(remoteMessage.getData()) ).build(); // Amplify should handle notification if it is sent from Pinpoint boolean isAmplifyMessage = Amplify.Notifications.Push.shouldHandleNotification(notificationPayload); if (isAmplifyMessage) { // Record notification received with Amplify Amplify.Notifications.Push.recordNotificationReceived(notificationPayload, () -> Log.i("MyAmplifyApp", "Successfully recorded a notification received"), error -> Log.e("MyAmplifyApp", "Error recording notification received", error) ); } } }
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}

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.

class LaunchActivity extends AppCompatActivity() { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Get the payload from the intent NotificationPayload payload = NotificationPayload.fromIntent(getIntent()); if (payload != null) { // Record notification opened when activity launches Amplify.Notifications.Push.recordNotificationOpened(payload, () -> Log.i("MyAmplifyApp", "Successfully recorded notification opened"), error -> Log.e("MyAmplifyApp", "Error recording notification opened", error) ); } } }
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}