iOS Push with Amazon's AWS Simple Notifications Service (SNS) and Swift Made Easy

An example of a notification from PicShip
/// The SNS Platform application ARNlet SNSPlatformApplicationArn = “arn:aws:sns:us-east-1:203525439813:app/APNS_SANDBOX/SpreebieSNSExample”
import AWSSNS
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {/// Attach the device token to the user defaultsvar token = “”for i in 0..<deviceToken.count {token = token + String(format: “%02.2hhx”, arguments: [deviceToken[i]])}print(token)UserDefaults.standard.set(token, forKey: “deviceTokenForSNS”)/// Create a platform endpoint. In this case, the endpoint is a/// device endpoint ARNlet sns = AWSSNS.default()let request = AWSSNSCreatePlatformEndpointInput()request?.token = tokenrequest?.platformApplicationArn = SNSPlatformApplicationArnsns.createPlatformEndpoint(request!).continueWith(executor: AWSExecutor.mainThread(), block: { (task: AWSTask!) -> AnyObject! inif task.error != nil {print(“Error: \(String(describing: task.error))”)} else {let createEndpointResponse = task.result! as AWSSNSCreateEndpointResponseif let endpointArnForSNS = createEndpointResponse.endpointArn {print(“endpointArn: \(endpointArnForSNS)”)UserDefaults.standard.set(endpointArnForSNS, forKey: “endpointArnForSNS”)}}return nil})}func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {print(error.localizedDescription)}
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {// Override point for customization after application launch./// Setup AWS Cognito credentialslet credentialsProvider = AWSCognitoCredentialsProvider(regionType: AWSRegionType.USEast1, identityPoolId: “us-east-1:7d5b4064-d730–44ae-a1c3-bdc3d8bdf195”)let defaultServiceConfiguration = AWSServiceConfiguration(region: AWSRegionType.USEast1, credentialsProvider: credentialsProvider)AWSServiceManager.default().defaultServiceConfiguration = defaultServiceConfigurationreturn true}
registerForPushNotifications(application: application)
import UserNotifications
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {
func registerForPushNotifications(application: UIApplication) {/// The notifications settingsif #available(iOS 10.0, *) {UNUserNotificationCenter.current().delegate = selfUNUserNotificationCenter.current().requestAuthorization(options: [.badge, .sound, .alert], completionHandler: {(granted, error) inif (granted){UIApplication.shared.registerForRemoteNotifications()}else{//Do stuff if unsuccessful…}})} else {let settings = UIUserNotificationSettings(types: [UIUserNotificationType.alert, UIUserNotificationType.badge, UIUserNotificationType.sound], categories: nil)application.registerUserNotificationSettings(settings)application.registerForRemoteNotifications()}}
// Called when a notification is delivered to a foreground app.@available(iOS 10.0, *)func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {print(“User Info = “,notification.request.content.userInfo)completionHandler([.alert, .badge, .sound])}// Called to let your app know which action was selected by the user for a given notification.@available(iOS 10.0, *)func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {print(“User Info = “,response.notification.request.content.userInfo)completionHandler()}

If you found this tutorial helpful, please click the clap 👏 button below a few times to show your support! Thank you, :). ⬇⬇

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Thabo David Klass

Thabo David Klass

Coder @ Spreebie and Hooligan Development