Page updated Nov 8, 2023

Record notification events

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 application(_:didReceiveRemoteNotification:) delegate method of your Application Delegate.

1// Called when a notification is received
2//
3// Requires background processing->remote notifications entitlement
4// and "content-available" : 1 in the notification
5//
6func application(
7 _ application: UIApplication,
8 didReceiveRemoteNotification userInfo: [AnyHashable: Any]
9) async -> UIBackgroundFetchResult {
10
11 do {
12 try await Amplify.Notifications.Push.recordNotificationReceived(userInfo)
13 } catch {
14 print("Error recording receipt of notification: \(error)")
15 }
16
17 return .newData
18}
1// Called when a notification is received
2//
3// Requires background processing->remote notifications entitlement
4// and "content-available" : 1 in the notification
5//
6func application(
7 _ application: NSApplication,
8 didReceiveRemoteNotification userInfo: [String : Any]
9) {
10 Task {
11 do {
12 try await Amplify.Notifications.Push.recordNotificationReceived(userInfo)
13 } catch {
14 print("Error recording receipt of notification: \(error)")
15 }
16 }
17}
1// Called when a notification is received
2//
3// Requires background processing->remote notifications entitlement
4// and "content-available" : 1 in the notification
5//
6func didReceiveRemoteNotification(_ userInfo: [AnyHashable : Any]) async -> WKBackgroundFetchResult {
7 do {
8 try await Amplify.Notifications.Push.recordNotificationReceived(userInfo)
9 } catch {
10 print("Error recording receipt of notification: \(error)")
11 }
12
13 return .newData
14}

Record Notification Opened

Note: This functionality is not available in tvOS.

You can record that a user opened a push notification with Amazon Pinpoint by calling Amplify.Notifications.Push.recordNotificationOpened(_) from the userNotificationCenter(_:didReceive:) delegate method of your UNUserNotificationCenter Delegate.

First, set the UNUserNotificationCenter delegate to your Application Delegate by adding or updating the following method.

1func application(
2 _ application: UIApplication,
3 didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil
4) -> Bool {
5 UNUserNotificationCenter.current().delegate = self
6 // ...
7 return true
8}
1func applicationDidFinishLaunching(_ notification: Notification) {
2 UNUserNotificationCenter.current().delegate = self
3 // ...
4}
1func applicationDidFinishLaunching() {
2 UNUserNotificationCenter.current().delegate = self
3}

Then, extend your Application Delegate to conform to UNUserNotificationCenterDelegate and add the following method.

1extension AppDelegate: UNUserNotificationCenterDelegate {
2
3 // Called when a user opens (taps or clicks) a notification.
4 func userNotificationCenter(
5 _ center: UNUserNotificationCenter,
6 didReceive response: UNNotificationResponse
7 ) async {
8 do {
9 try await Amplify.Notifications.Push.recordNotificationOpened(response)
10 } catch {
11 print("Error recording notification opened: \(error)")
12 }
13 }
14}