Page updated Nov 8, 2023

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:

1try Amplify.add(
2 plugin: AWSPinpointPushNotificationsPlugin(options: [.badge, .alert, .sound])
3)

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.

The following is only required when you do not pass UNAuthorizationOptions when adding the plugin as described above.

1// Request user permission for notifications.
2let options: UNAuthorizationOptions = [.badge, .alert, .sound]
3let notificationsAllowed = try await UNUserNotificationCenter.current().requestAuthorization(
4 options: options
5)

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.

1// Note: In order for this to work on the simulator, you must be running
2// on Apple Silicon, with macOS 13+, Xcode 14+, and iOS simulator 16+.
3//
4// If your development environment does not meet all of these requirements,
5// then you must run on a real device to get an APNs token.
6//
7func application(
8 _ application: UIApplication,
9 didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data
10) {
11 Task {
12 do {
13 try await Amplify.Notifications.Push.registerDevice(apnsToken: deviceToken)
14 print("Registered with Pinpoint.")
15 } catch {
16 print("Error registering with Pinpoint: \(error)")
17 }
18 }
19}
1func application(
2 _ application: NSApplication,
3 didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data
4) {
5 Task {
6 do {
7 try await Amplify.Notifications.Push.registerDevice(apnsToken: deviceToken)
8 print("Registered with Pinpoint.")
9 } catch {
10 print("Error registering with Pinpoint: \(error)")
11 }
12 }
13}
1func didRegisterForRemoteNotifications(withDeviceToken deviceToken: Data) {
2 Task {
3 do {
4 try await Amplify.Notifications.Push.registerDevice(apnsToken: deviceToken)
5 print("Registered with Pinpoint.")
6 } catch {
7 print("Error registering with Pinpoint: \(error)")
8 }
9 }
10}