amplify-swift
amplify-swift copied to clipboard
Push Notification support for Amplify iOS (and Android)
I would like to use Pinpoint for targeting users with my native apps and send them a push notification when they meet certain campaign criteria similar to what is available for react native amplify libraries
HI @MattJColes, currently Amplify for iOS does not support Pinpoint Campaigns out of the box. We are investigating ways to provide this functionality from an Amplify Plugin. You can also use the Pinpoint SDK to achieve the use case. The following are some steps to use Amplify's AWSCognitoAuthPlugin plugin, get the credentials provider, and pass it programatically to the Pinpoint SDK.
Provision the Pinpoint resource as normal, in the CLI
-
ampliify add analytics
and allow unauth access. -
amplify push
to create the Cognito and Pinpoint resources.
Install the following pods
pod 'Amplify'
pod 'AmplifyPlugins/AWSCognitoAuthPlugin'
pod 'AWSPinpoint'
Open the workspace and drop in only the amplifyconfiguration.json
file. This file will be used by Amplify when you call Amplify.configure()
Add the following imports
import Amplify
import AmplifyPlugins
import AWSPluginsCore
import AWSPinpoint
Configure Amplify inside application(didFinishLaunchingWithOptions)
#if DEBUG
AWSDDLog.sharedInstance.logLevel = .verbose
AWSDDLog.add(AWSDDTTYLogger.sharedInstance)
Amplify.Logging.logLevel = .verbose
#endif
do {
try Amplify.add(plugin: AWSCognitoAuthPlugin())
try Amplify.configure()
print("Amplify configured with auth plugin")
} catch {
print("Failed to initialize Amplify with \(error)")
}
Get the Credentials Provider and configure AWSPinpoint. In the pinpoint configuration, if you want to use pinpoint campaigns, you will most likely want to set pinpointConfiguration.debug = true
to use APNS Sandbox endpoint for debug builds.
let credentialsProvider = AmplifyAWSCredentialsProvider()
let pinpointConfiguration = AWSPinpointConfiguration(appId: [PINPOINT_APP_ID], launchOptions: nil)
let serviceConfiguration = AWSServiceConfiguration(region: .US_WEST_2,
credentialsProvider: credentialsProvider)
let targetingServiceConfiguration = AWSServiceConfiguration(region: .US_WEST_2,
credentialsProvider: credentialsProvider)
pinpointConfiguration.serviceConfiguration = serviceConfiguration
pinpointConfiguration.targetingServiceConfiguration = targetingServiceConfiguration
#if DEBUG
pinpointConfiguration.debug = true
Amplify.Logging.verbose("Setting pinpointConfiguration.debug to true")
#endif
pinpoint = AWSPinpoint(configuration: pinpointConfiguration)
return true
}
the pinpoint instance is declared in AppDelegate as
class AppDelegate: UIResponder, UIApplicationDelegate {
var pinpoint: AWSPinpoint?
The hardcoded values for pinpoint AppId and regions come from amplifyconfiiguratiion.json
, however you can also retrieve from programmatically by reading the Bundle at bundle.url(forResource: "amplifyconfiguration", withExtension: "json")
and extracting out the values. Now that pinpoint is instantiated, you can follow the rest of the Pinpoint SDK docs to set up push notifications here.
Feel free to let us know if you have any questions, as there are some updates to the documentation in flight that you may find useful here: Enabling for iOS 10 and above: https://github.com/aws-amplify/docs/pull/2114/files
@lawmicha Let's say I do that and my app uses Cognito too.
Does this mean I can send a message to a specific user id (user id taken from cognito) from a lambda function?
Or do I have to update the endpoint in the client with the user id (attribute)?
It seems that here: https://html.developreference.com/article/13722006/Send+a+notification+by+Lambda+function+with+AWS+Pinpoint
he is sending the user id to the endpoint, so, when using a lambda function later, we can search the endpoint we want by user id, and then send a push notification. Is the the proper way? Shouldn't we have something like an automatic integration with cogito?
Thanks for clarification.
Push Notifications support has been added in Amplify Library for Swift 2.5.0.
The documentation is available here: Push Notifications