Author: Corinne Krych (ckrych), Christos Vasilakis (cvasilak)
Level: Beginner
Technologies: Swift 4.0, iOS
Summary: A basic example of Push : Registration and receiving messages.
Target Product: Mobile
Product Versions: MP 1.0
Source: https://github.com/aerogear/aerogear-push-helloworld/ios-swift
This project is a very simple helloworld, to show how to get started with the UnifiedPush Server on iOS. The demo is implemented in Swift 4.0 and uses the push-sdk [Swift|https://github.com/aerogear/aerogear-ios-push/tree/master] for registering to the UnifiedPush Server.
- iOS 9, iOS 10, iOS 11
- Xcode version 9.0+
- Have created an variant in UnifiedPush admin console
- Have a valid provisioning profile as you will need to test on device (push notification not available on simulator)
- Replace the bundleId with your bundleId (the one associated with your certificate). Go to HelloWorldSwift target -> Info -> change Bundle Identifier field.
The project uses CocoaPods for handling its dependencies. As a pre-requisite, install CocoaPods and then install the pod. On the root directory of the project run:
pod installand then double click on the generated HelloWorldSwift.xcworkspace to open in Xcode.
In HelloWorldSwift/Supporting Files/pushconfig.plist fill the values for serverURL, variantID and variantSecret.
When the application is launched, AppDelegate's application:didFinishLaunchingWithOptions: registers the app to receive remote notifications.
Note that registerForRemoteNotificationTypes: has been removed in iOS8 in favor of registerUserNotificationSettings: and registerForRemoteNotifications
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
let settings = UIUserNotificationSettings(types: .alert | .badge | .sound, categories: nil)
UIApplication.shared.registerUserNotificationSettings(settings)
UIApplication.shared.registerForRemoteNotifications()
...
return true
}Therefore, AppDelegate's application:didRegisterForRemoteNotificationsWithDeviceToken: will be called.
When AppDelegate's application:didRegisterForRemoteNotificationsWithDeviceToken: is called, the device is registered to UnifiedPush Server instance. This is where configuration changes are required (see code snippet below).
Now you can send a message to your device by clicking Compose Message... from the application page. Write a message in the text field and hit 'Send Push Message'.
After a while you will see the message end up on the device.
When the application is running in foreground, you can catch messages in AppDelegate's application:didReceiveRemoteNotification:. The event is forwarded using NSNotificationCenter for decoupling appDelegate and ViewController. It will be the responsability of ViewController's messageReceived: method to render the message on UITableView.
When the app is running in background, user can bring the app in the foreground by selecting the Push notification. Therefore AppDelegate's application:didReceiveRemoteNotification: will be triggered and the message displayed on the list. If a background processing was needed we could have used application:didReceiveRemoteNotification:fetchCompletionHandler:. Refer to Apple documentation for more details
For application not running, we're using AppDelegate's application:didFinishLaunchingWithOptions:, we locally save the latest message and forward the event to ViewController's messageReceived:.
NOTE: The local save is required here because of the asynchronous nature of viewDidLoad vs application:didFinishLaunchingWithOptions:
- Which iOS version is supported by AeroGear iOS libraries?
AeroGear supports iOS 9.X+
Set a break point in Xcode.

