Register a device
In order to send Push Notifications from Amazon Pinpoint to a user's device, the device must be registered with both Apple Push Notification service (APNs) and Amazon Pinpoint.
Request user permission for notifications
Displaying push notifications from APNs requires requesting permission from the user to receive notifications and registering with the APNs service.
AWSPinpointPushNotificationsPlugin takes care of registering with APNs during configuration. If you want the plugin to also request user permission during configuration, you can pass an array of UNAuthorizationOptions
when adding the plugin. For example, to request permission for badges, alerts, and sounds, you could add the plugin with the following:
try Amplify.add( plugin: AWSPinpointPushNotificationsPlugin(options: [.badge, .alert, .sound]))
If you don't pass UNAuthorizationOptions
when adding the plugin, you will be responsible for requesting notification permissions and registering with APNs as shown below.
// Request user permission for notifications.let options: UNAuthorizationOptions = [.badge, .alert, .sound]let notificationsAllowed = try await UNUserNotificationCenter.current().requestAuthorization( options: options)
Register with Amazon Pinpoint
Once the user has granted permission to receive notifications and the device has been registered with APNs, the application(_:didRegisterForRemoteNotificationsWithDeviceToken:)
delegate method of your Application Delegate will be called with the APNs token. You can use the Amplify.Notifications.Push.registerDevice(apnsToken:)
method to register the device with Pinpoint.
// Note: In order for this to work on the simulator, you must be running// on Apple Silicon, with macOS 13+, Xcode 14+, and iOS simulator 16+.//// If your development environment does not meet all of these requirements,// then you must run on a real device to get an APNs token.//func application( _ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { Task { do { try await Amplify.Notifications.Push.registerDevice(apnsToken: deviceToken) print("Registered with Pinpoint.") } catch { print("Error registering with Pinpoint: \(error)") } }}
func application( _ application: NSApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { Task { do { try await Amplify.Notifications.Push.registerDevice(apnsToken: deviceToken) print("Registered with Pinpoint.") } catch { print("Error registering with Pinpoint: \(error)") } }}
func didRegisterForRemoteNotifications(withDeviceToken deviceToken: Data) { Task { do { try await Amplify.Notifications.Push.registerDevice(apnsToken: deviceToken) print("Registered with Pinpoint.") } catch { print("Error registering with Pinpoint: \(error)") } }}