Record notification events
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 @Override3 public void onMessageReceived(RemoteMessage remoteMessage) {4 // Convert the RemoteMessage into a NotificationPayload5 NotificationPayload notificationPayload = NotificationPayload.builder(6 new NotificationContentProvider.FCM(remoteMessage.getData())7 ).build();8
9 // Amplify should handle notification if it is sent from Pinpoint10 boolean isAmplifyMessage = Amplify.Notifications.Push.shouldHandleNotification(notificationPayload);11 if (isAmplifyMessage) {12 // let Amplify handle foreground and background message received13 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.
1class MyAppService extends FirebaseMessagingService() {2 @Override3 public void onMessageReceived(RemoteMessage remoteMessage) {4 // Convert the RemoteMessage into a NotificationPayload5 NotificationPayload notificationPayload = NotificationPayload.builder(6 new NotificationContentProvider.FCM(remoteMessage.getData())7 ).build();8
9 // Amplify should handle notification if it is sent from Pinpoint10 boolean isAmplifyMessage = Amplify.Notifications.Push.shouldHandleNotification(notificationPayload);11 if (isAmplifyMessage) {12 // Record notification received with Amplify13 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.
1class LaunchActivity extends AppCompatActivity() {2 @Override3 public void onCreate(Bundle savedInstanceState) {4 super.onCreate(savedInstanceState);5
6 // Get the payload from the intent7 NotificationPayload payload = NotificationPayload.fromIntent(getIntent());8
9 if (payload != null) {10 // Record notification opened when activity launches11 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}