Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

The UrbanAirship SDK supports the new rich notifications provided by iOS 10+, as well as providing provides support for previous versions. Depending on the target framework, the relevant instructions can be followed on the site linked above.

...

Once the notifications are correctly setup and the UrbanAirship services are started, we can retrieve the DeviceToken and the ChannelID which (either DeviceToken or ChannelID) which would eventually be used to target push notifications for individual devices. Once this DeviceTokenDeviceIToken is retrieved successfully, it needs to be registered with the Rubiq API RubiqAPI so push notifications can be sent out from the Rubiq Platform via the UrbanAirship SDK.

The exact location for registering the DeviceToken depends on the flow of the app. This could for instance be soon after a user has successfully logged in to the app using Rubiq API endpoint 2001: Authenticate. On successful authentication, dialogportal's master key would be returned as a part of the response. This Master key along with the DeviceID could then be used to register the device using Rubiq API endpoint 4001: Register mobile device.

Note

Please note that that in order to push notifications using the Rubiq Platform, Rubiq needs to know the UrbanAirship MasterKey and AppKey, which can be accessed from the UrbanAirship App dashboard.

...


A sample AppDelegate file is as follows.

Code Block
languagec#
titleAppDelegate.swift
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate, UARegistrationDelegate, UAPushNotificationDelegate {

    var window: UIWindow?
    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {
        
        let config = UAConfig.default()
        UAirship.takeOff(config)
        
        UAirship.push().notificationOptions = [.alert, .badge, .sound]
        UAirship.push().userPushNotificationsEnabled = true
        
        UAirship.push().registrationDelegate = self
        UAirship.push().pushNotificationDelegate = self
        
        return true
    }
    
    func registrationSucceeded(forChannelID channelID: String, deviceToken: String) {
        print("Registration succeeded. ChannelID: \(channelID), DeviceToken: \(deviceToken)")
         // TODO: Register DeviceID with RubiqAPI
    }

    func receivedForegroundNotification(_ notificationContent: UANotificationContent, completionHandler: @escaping () -> Void) {
        print("Received foreground notification. DeviceToken: \(UAirship.push().deviceToken)")
        
    }
    
    @available(iOS 10.0, *)
    func presentationOptions(for notification: UNNotification) -> UNNotificationPresentationOptions {
        print("Presentation options")
        return [.alert, .sound]
    }
    
    func receivedBackgroundNotification(_ notificationContent: UANotificationContent, completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
        print("Received background notification. DeviceToken: \(UAirship.push().deviceToken)")
    }
}

A sample code for generating the encrypted token using Swift is provided below. The prerequisite is that the raw token has already been generated as defined in Authenticating requests. The library used for generating the encrypted token is CryptoSwift https://github.com/krzyzanowskim/CryptoSwift.

Code Block
languagec#
titleGenerating token
func createEncryptedToken(_ rawToken: String, _ apiSecret: String) -> String {
	let appSecretBytes = apiSecret.utf8.map({$0})
    let tokenBytes = rawToken.utf8.map({$0})
    let encryptedBytes = try! HMAC(key: appSecretBytes, variant: .sha256).authenticate(tokenBytes)
    return encryptedBytes.toBase64()!
}