Set up Amplify Push Notifications
The Push Notifications category allows you to integrate push notifications in your app with Amazon Pinpoint targeting, campaign, and journey management support. You can segment your users, trigger push notifications to your app, and record metrics in Pinpoint when users receive or open notifications. Amazon Pinpoint helps you to create messaging campaigns and journeys targeted to specific user segments or demographics and collect interaction metrics with push notifications.
Prerequisites
An application with Amplify libraries integrated and a minimum target of any of the following:
- iOS 13.0, using Xcode 14.1 or later.
- macOS 10.15, using Xcode 14.1 or later.
- tvOS 13.0, using Xcode 14.3 or later.
- watchOS 9.0, using Xcode 14.3 or later.
- visionOS 1.0, using Xcode 15 or later. (Preview support - see below for more details.)
For a full example, please follow the project setup walkthrough.
Push Notifications are delivered via the Apple Push Notification service (APNs). In order to use APNs, you need to setup credentials (keys or certificates) and export your credentials to a p12 file. See Setting up push notification services for more information.
Set Entitlements
Using Amazon Pinpoint with APNs requires the following capabilities
- Push Notifications
- Background Processing -> Remote Notifications (iOS/tvOS/watchOS)
- App Sandbox -> Outgoing Connections (macOS only)
To add these capabilities:
- Open your Xcode project, go to project settings, select your main application target, select the Signing and Capabilities tab, and click + Capability in the upper left corner of the editor pane to bring up the Capabilities dialog.
- Type push in the filter box and double-click Push Notifications to add the capability.
- Repeat step 1 to open the Capabilities dialog and then type back in the filter box and double-click Background Modes to add the capability.
-
Select Change All when prompted.
-
Under Background Modes, select Remote Notifications
- Select Change All again when prompted.
- Open your Xcode project, go to project settings, select your main application target, select the Signing and Capabilities tab, and click + Capability in the upper left corner of the editor pane to bring up the Capabilities dialog.
- Type push in the filter box and double-click Push Notifications to add the capability.
- Repeat step 1 to open the Capabilities dialog and then type sand in the filter box and double-click App Sandbox to add the capability.
- Under App Sandbox, select Outgoing Connections (Client)
Application Delegate
APNs requires an application delegate to handle push notifications functionality via delegate methods. If your app does not already have an application delegate, you will need to create one:
- Create a class that subclasses
NSObject
and implements theUIApplicationDelegate
(iOS/tvOS),WKApplicationDelegate
(watchOS), orNSApplicationDelegate
(macOS) protocol.
class AppDelegate: NSObject, UIApplicationDelegate {
}
class AppDelegate: NSObject, NSApplicationDelegate {
}
class AppDelegate: NSObject, WKApplicationDelegate {
}
- Add a property to your main app struct that identifies your application delegate.
@mainstruct <YOUR_APP_NAME>App: App { @UIApplicationDelegateAdaptor private var appDelegate: AppDelegate // ...}
@mainstruct <YOUR_APP_NAME>App: App { @NSApplicationDelegateAdaptor private var appDelegate: AppDelegate // ...}
@mainstruct <YOUR_APP_NAME>App: App { @WKApplicationDelegateAdaptor private var appDelegate: AppDelegate // ...}
Set up backend resources
To use Push Notifications with Amplify, you have the option to either have the Amplify CLI setup resources for you, or you can use an existing Amazon Pinpoint resource in your AWS account.
Prerequisite: Install and configure the Amplify CLI
To start provisioning push notification resources in the backend, go to your project directory and execute the command:
amplify add notifications
Choose APNS when promoted:
? Choose the push notification channel to enable.❯ APNS | Apple Push Notifications FCM | » Firebase Push Notifications In-App Messaging Email SMS
Follow the setup prompts:
? Provide your pinpoint resource name: › [pinpoint_resource_name]? Apps need authorization to send analytics events. Do you want to allow guests and unauthenticated users to send analytics events? (we recommend you allow this when getting started) (Y/n)
Choose Certificate when promoted:
? Choose authentication method used for APNs> CertificateKey
The CLI will prompt for your p12 certificate path, enter a path relative to the location where you ran the command.
Upon completion, amplifyconfiguration.json
will be updated to reference the newly provisioned backend push notifications resources. Note that this file should already be generated for you by the Amplify CLI as a part of your project if you followed the project setup walkthrough.
Amplify reads configuration information at runtime from a file called amplifyconfiguration.json
. If you used the Amplify CLI to create backend resources, this file was created for you. If it does not already exist, create the file and add it to your Xcode project.
Existing Amazon Pinpoint resources can be used with the Amplify Libraries by referencing your Application ID and Region in your amplifyconfiguration.json
file.
"notifications": { "plugins": { "awsPinpointPushNotificationsPlugin": { "appId": "[APP ID]", "region": "[REGION]" } }}
- appId: Amazon Pinpoint application ID
- region: AWS Region where the resources are provisioned (e.g.
us-east-1
)
Install Amplify Libraries
Swift Package Manager
-
To install Amplify Libraries in your application, open your project in Xcode and select File > Add Packages....
-
Enter the Amplify Library for Swift GitHub repo URL (
https://github.com/aws-amplify/amplify-swift
) into the search bar and click Add Package.
- Lastly, choose AWSPinpointPushNotificationsPlugin, AWSCognitoAuthPlugin, and Amplify. Then click Add Package.
Initialize Amplify Push Notifications
To initialize the Amplify Push Notifications, you will use the Amplify.addPlugin()
method to add the AWSPinpointPushNotificationsPlugin and then call Amplify.configure()
to finish configuring Amplify.
Open the main file of the application - AppDelegate.swift
or <YOUR_APP_NAME>App.swift
depending on the app's life cycle - and add the following import statements at the top of the file:
import Amplifyimport AWSCognitoAuthPluginimport AWSPinpointPushNotificationsPlugin
In the same file, create a function to configure Amplify:
func configureAmplify() { do { try Amplify.add(plugin: AWSCognitoAuthPlugin()) try Amplify.add(plugin: AWSPinpointPushNotificationsPlugin()) try Amplify.configure() print("Initialized Amplify"); } catch { print("Could not initialize Amplify: \(error)") }}
Now call the configureAmplify()
function in the starting point of your application.
@mainstruct <YOUR_APP_NAME>App: App { // add a default initializer and configure Amplify public init() { configureAmplify() } // ...}
@UIApplicationMainclass AppDelegate: UIResponder, UIApplicationDelegate { func application(_: UIApplication, didFinishLaunchingWithOptions _: [UIApplication.LaunchOptionsKey: Any]? ) -> Bool { configureAmplify() return true } // ...}
Upon building and running this application you should see the following in your console window:
Initialized Amplify