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.
// Called when a notification is received//// Requires background processing->remote notifications entitlement// and "content-available" : 1 in the notification//func application( _ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) async -> UIBackgroundFetchResult { do { try await Amplify.Notifications.Push.recordNotificationReceived(userInfo) } catch { print("Error recording receipt of notification: \(error)") } return .newData}
// Called when a notification is received//// Requires background processing->remote notifications entitlement// and "content-available" : 1 in the notification//func application( _ application: NSApplication, didReceiveRemoteNotification userInfo: [String : Any]) { Task { do { try await Amplify.Notifications.Push.recordNotificationReceived(userInfo) } catch { print("Error recording receipt of notification: \(error)") } }}
// Called when a notification is received//// Requires background processing->remote notifications entitlement// and "content-available" : 1 in the notification//func didReceiveRemoteNotification(_ userInfo: [AnyHashable : Any]) async -> WKBackgroundFetchResult { do { try await Amplify.Notifications.Push.recordNotificationReceived(userInfo) } catch { print("Error recording receipt of notification: \(error)") } return .newData}
Record Notification Opened
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.
func application( _ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool { UNUserNotificationCenter.current().delegate = self // ... return true}
func applicationDidFinishLaunching(_ notification: Notification) { UNUserNotificationCenter.current().delegate = self // ...}
func applicationDidFinishLaunching() { UNUserNotificationCenter.current().delegate = self}
Then, extend your Application Delegate to conform to UNUserNotificationCenterDelegate
and add the following method.
extension AppDelegate: UNUserNotificationCenterDelegate {
// Called when a user opens (taps or clicks) a notification. func userNotificationCenter( _ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse ) async { do { try await Amplify.Notifications.Push.recordNotificationOpened(response) } catch { print("Error recording notification opened: \(error)") } }}