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
You should have completed the CLI and project setup steps.
An application targeting at least iOS 13.0, using Xcode 14.1 or later.
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 Amplify Push Notifications with APNs requires the following capabilities:
- Push Notifications
- Background Processing -> Remote Notifications
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.
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:
1amplify add notifications
Choose APNS when promoted:
1? Choose the push notification channel to enable.2❯ APNS | Apple Push Notifications3 FCM | » Firebase Push Notifications4 In-App Messaging5 Email6 SMS
Follow the setup prompts:
1? Provide your pinpoint resource name: › [pinpoint_resource_name]2? Apps need authorization to send analytics events. Do you want to allow guests3 and unauthenticated users to send analytics events? (we recommend you allow this 4 when getting started) (Y/n)
Choose Certificate when promoted:
1? Choose authentication method used for APNs2> Certificate3Key
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.
Install Amplify Libraries
In your project directory, you should first install the necessary dependencies for using Amplify Push Notifications.
1npm install aws-amplify @aws-amplify/react-native @aws-amplify/rtn-push-notification @react-native-async-storage/async-storage react-native-get-random-values
Integrate native module
Since push notifications require interaction with the native platform, there are some additional steps required to integrate your React Native app with the Amplify Push Notifications native module.
Link the module
You should have already installed the native module in an earlier step, now you need to link it. In your React Native app's root directory run the following command.
1npx pod-install
Update your Application Delegate
Locate and open your AppDelegate.m
or AppDelegate.mm
file in your text editor. You should find it in your React Native project under /ios/<your-project-name>
At the top of your AppDelegate
, import Amplify Push Notifications:
1#import "AppDelegate.h"2#import "AmplifyPushNotification.h"3
4...
In the body of your AppDelegate
, add the following two methods:
1- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {2 [AmplifyPushNotification didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];3}4
5- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler {6 [AmplifyPushNotification didReceiveRemoteNotification:userInfo withCompletionHandler:completionHandler];7}
Initialize Amplify Push Notifications
To initialize Amplify Push Notifications, you will first need to configure Amplify.
Import and load the configuration file in your app.
1// Example index.js2// ...3import { Amplify } from 'aws-amplify';4import amplifyconfig from './src/amplifyconfiguration.json';5
6Amplify.configure(amplifyconfig);7// ...
Next, initialize Amplify Push Notifications. It is recommended to do this in your app's root entry point after configuring Amplify if your app requires push notification handling even while in a terminated state.
1// Example index.js2// ...3import { initializePushNotifications } from 'aws-amplify/push-notifications';4
5initializePushNotifications();6// ...
Your resulting entry file should look something like the following:
1import { AppRegistry } from 'react-native';2import App from './App';3import { name as appName } from './app.json';4
5import { Amplify } from 'aws-amplify';6import { initializePushNotifications } from 'aws-amplify/push-notifications';7import amplifyconfig from './src/amplifyconfiguration.json';8
9Amplify.configure(amplifyconfig);10initializePushNotifications();11
12AppRegistry.registerComponent(appName, () => App);