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

Page updated Feb 21, 2024

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)
);
}
}
}
class MyAppService : FirebaseMessagingService() {
override fun onMessageReceived(remoteMessage: RemoteMessage) {
// Convert the RemoteMessage into a NotificationPayload
val notificationPayload = NotificationPayload(NotificationContentProvider.FCM(remoteMessage.data))
// Amplify should handle notification if it is sent from Pinpoint
val isAmplifyMessage = Amplify.Notifications.Push.shouldHandleNotification(notificationPayload)
if (isAmplifyMessage) {
// let Amplify handle foreground and background message received
Amplify.Notifications.Push.handleNotificationReceived(notificationPayload,
{ Log.i("MyAmplifyApp", "Successfully handled a notification") },
{ Log.e("MyAmplifyApp", "Error handling notification", error) }
)
}
}
}
class MyAppService : FirebaseMessagingService() {
override fun onMessageReceived(remoteMessage: RemoteMessage) {
// Convert the RemoteMessage into a NotificationPayload
val notificationPayload = NotificationPayload(NotificationContentProvider.FCM(remoteMessage.data))
// Amplify should handle notification if it is sent from Pinpoint
val isAmplifyMessage = Amplify.Notifications.Push.shouldHandleNotification(notificationPayload)
if (isAmplifyMessage) {
try {
// let Amplify handle foreground and background message received
Amplify.Notifications.Push.handleNotificationReceived(notificationPayload)
Log.i("MyAmplifyApp", "Successfully handled a notification")
} catch (error: PushNotificationsException) {
Log.e("MyAmplifyApp", "Error handling notification", error)
}
}
}
}
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
RxAmplify.Notifications.Push.handleNotificationReceived(notificationPayload).subscribe(
result -> Log.i("MyAmplifyApp", "Successfully handled a notification"),
error -> Log.e("MyAmplifyApp", "Error handling notification", error)
);
}
}
}

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)
);
}
}
}
class MyAppService : FirebaseMessagingService() {
override fun onMessageReceived(remoteMessage: RemoteMessage) {
// Convert the RemoteMessage into a NotificationPayload
val notificationPayload = NotificationPayload(NotificationContentProvider.FCM(remoteMessage.data))
// Amplify should handle notification if it is sent from Pinpoint
val 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") },
{ Log.e("MyAmplifyApp", "Error recording notification received", error) }
)
}
}
}
class MyAppService : FirebaseMessagingService() {
override fun onMessageReceived(remoteMessage: RemoteMessage) {
// Convert the RemoteMessage into a NotificationPayload
val notificationPayload = NotificationPayload(NotificationContentProvider.FCM(remoteMessage.data))
// Amplify should handle notification if it is sent from Pinpoint
val isAmplifyMessage = Amplify.Notifications.Push.shouldHandleNotification(notificationPayload)
if (isAmplifyMessage) {
try {
// let Amplify handle foreground and background message received
Amplify.Notifications.Push.recordNotificationReceived(notificationPayload)
Log.i("MyAmplifyApp", "Successfully recorded a notification received")
} catch (error: PushNotificationsException) {
Log.e("MyAmplifyApp", "Error recording notification received", error)
}
}
}
}
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
RxAmplify.Notifications.Push.recordNotificationReceived(notificationPayload).subscribe(
() -> Log.i("MyAmplifyApp", "Successfully recorded a notification received"),
error -> Log.e("MyAmplifyApp", "Error recording notification received", error)
);
}
}
}

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