Messaging campaigns

You are currently viewing the AWS SDK for Mobile documentation which is a collection of low-level libraries. Use the Amplify libraries for all new app development. Learn more

You can view the Mobile SDK API reference here.

The Amazon Pinpoint console enables you to target your app users with push messaging. You can send individual messages or configure campaigns that target a group of users that match a profile that you define. For instance, you could email users that have not used the app in 30 days, or send an SMS to those that frequently use a given feature of your app.

The following steps show how to receive push notifications targeted for your app.

  1. To receive Amazon Pinpoint push notification to your app, you'll use pinpoint.notificationManager to intercept the registration of the app for notifications in the application(_:didRegisterForRemoteNotificationsWithDeviceToken:) application call back in AppDelegate.

    Then add and call a function like registerForPushNotifications() to prompt permission from the user for the app to use notifications. The following example uses the UNUserNotificationCenter framework, which is available in iOS 10.0+. Choose the right location in your app to prompt the user for permissions. In the following example the call is implemented in the application(_:didFinishLaunchingWithOptions:) event in AppDelegate. This causes the prompt to appear when the app launches.

    1import UserNotifications
    2
    3// Other imports...
    4
    5class AppDelegate: UIResponder, UIApplicationDelegate {
    6
    7 // Other app delegate methods...
    8 func application(
    9 _ application: UIApplication,
    10 didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
    11 ) -> Bool {
    12 // Other didFinishLaunching code, including Pinpoint initialization...
    13
    14 registerForPushNotifications()
    15
    16 // Other didFinishLaunching code...
    17 }
    18
    19 func application(
    20 _ application: UIApplication,
    21 didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    22
    23 pinpoint!.notificationManager.interceptDidRegisterForRemoteNotifications(
    24 withDeviceToken: deviceToken)
    25 }
    26
    27 func application(
    28 _ application: UIApplication,
    29 didReceiveRemoteNotification userInfo: [AnyHashable: Any],
    30 fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void
    31 ) {
    32
    33 pinpoint!.notificationManager.interceptDidReceiveRemoteNotification(
    34 userInfo,
    35 fetchCompletionHandler: completionHandler
    36 )
    37
    38 if (application.applicationState == .active) {
    39 let alert = UIAlertController(
    40 title: "Notification Received",
    41 message: userInfo.description,
    42 preferredStyle: .alert
    43 )
    44 alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: nil))
    45
    46 UIApplication.shared.keyWindow?
    47 .rootViewController?.present(alert, animated: true, completion:nil)
    48 }
    49 }
    50
    51 // Request user to grant permissions for the app to use notifications
    52 func registerForPushNotifications() {
    53 UNUserNotificationCenter.current().delegate = self
    54 UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) {
    55 (granted, error) in
    56 print("Permission granted: \(granted)")
    57 // 1. Check if permission granted
    58 guard granted else { return }
    59 // 2. Attempt registration for remote notifications on the main thread
    60 DispatchQueue.main.async {
    61 UIApplication.shared.registerForRemoteNotifications()
    62 }
    63 }
    64 }
    65
    66 // Other app delegate methods...
    67}

    Note: If you already have push notification delegate methods, you can just add the interceptDidRegisterForRemoteNotificationsWithDeviceToken: and interceptDidReceiveRemoteNotification:fetchCompletionHandler: callbacks to Pinpoint client.

  2. In Xcode Project Navigator, choose your app name at the top, choose your app name under Targets, choose the Capabilities tab, and then turn on Push Notifications.

  3. Configure the app to run in the Release profile instead of the default Debug profile. Perform the following steps to get a notification to the device:

    1. For your app target, go to the General tab of project configuration and make sure Automatically Manage Signing check box is not selected.

    2. In the Signing (Release) section, choose the production provisioning profile you created on Apple developer console. For testing push notifications on a device, you will need an Ad Hoc Provisioning Profile configured with a Production AppStore and Ad Hoc certificate, and with the device(s) to be used for testing.

    3. In the top left corner of Xcode (where your app name is displayed next to the current build target device), choose on your app name and then select Edit Scheme, and then set Build configuration to Release

      Note: Run your app on an iPhone device to test. Push notifications are not supported on simulators.

    4. Xcode will give an error that it could not run the app, this is due to production profile apps not being allowed to debug. Click Ok and launch the app directly from the device.

    5. When prompted, chose to allow notifications for the device.

    6. To create a new campaign to send notifications to your app from the Amazon Pinpoint console run the following command from your app project folder.

      1cd YOUR_APP_PROJECT_FOLDER
      2amplify notifications console
    7. Provide a campaign name, choose Next, choose Filter by standard attributes, and then choose iOS as the platform.

    8. You should see 1 device as a targeted endpoint, which is the app you are running on the iPhone device. Choose the option and then choose Next Step.

    9. Provide text for a sample title and body for push notification, and then choose Next Step.

    10. Choose Immediate, and then choose Next Step.

    11. Review the details on the screen, and then choose Launch Campaign.

    12. A notification should appear on your iPhone. You may want to try testing push notifications when your app is in the foreground as well as when it is closed.