Add Notification Support to your iOS App
This task builds on Apple notification documentation.
Before you start
Familiarize yourself with Apple notification documentation before undertaking any development. See https://developer.apple.com/documentation/pushkit/supporting_pushkit_notifications_in_your_app.final public class Controller : NSObject {
/**Register this device to our server with Apple APNS server when we get the deviceToken, then we can receive notification from server.*/
public func registerDeviceForPushNotifiction(applicationID: String, deviceID: String, deviceToken: String, userID: String)
/** Report the incoming call to OracleLive widget.*/
@available(iOS 10.0, *)
public func reportIncomingCall(payload: PKPushPayload)
}
Then, the app needs to identify itself by providing details about
the device and the configured user name, which Live Experience uses to do a lookup.
Also, expand the Controller to make it catch incoming calls, as shown in the
following example:
class AppDelegate: UIResponder, UIApplicationDelegate, PKPushRegistryDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
self.voipRegistration()
...
return true
}
// Register for VoIP notifications
private func voipRegistration() {
let mainQueue = DispatchQueue.main
// Create a push registry object
let voipRegistry: PKPushRegistry = PKPushRegistry(queue: mainQueue)
// Set the registry's delegate to self
voipRegistry.delegate = self
// Set the push type to VoIP
voipRegistry.desiredPushTypes = [PKPushType.voIP]
}
// implement this from PKPushRegistryDelegate, called when we got the device token
func pushRegistry(_ registry: PKPushRegistry, didUpdate pushCredentials: PKPushCredentials, for type: PKPushType) {
let pushtoken = pushCredentials.token
let token = pushtoken.map { String(format: "%02.2hhx", $0) }.joined()
AppState.deviceToken = token
Log.debug("voip get pushCredentials: \(token)")
let userID = UserDefaults.userEmailAddress
let clientID = UserDefaults.applicationId
let deviceID = UIDevice.current.identifierForVendor!.uuidString
if !AppState.authToken.isEmpty {
Controller.shared.registerDeviceForPushNotifiction(applicationID: clientID, deviceID: deviceID, deviceToken: token, userID: userID)
}
}
// implement this from PKPushRegistryDelegate, called when we receive the incoming call notification
func pushRegistry(_ registry: PKPushRegistry, didReceiveIncomingPushWith payload: PKPushPayload, for type: PKPushType, completion: @escaping () -> Void) {
Log.debug("voip notice got.....")
if #available(iOS 10.0, *) {
if AppState.authToken.isEmpty {
let settings = AuthenticationUtils.retrieveAuthSettings()
let authCompletion: (String?, Error?) -> Void = { (token, error) in
guard token != nil else {
return
}
AppState.authToken = token!
Controller.shared.service.authToken = AppState.authToken
Controller.shared.reportIncomingCall(payload: payload)
}
AuthenticationUtils.authenticate(withType: AuthType.guest, withSettings: settings, withPassword: nil, completionHandler: authCompletion)
}else{
Controller.shared.reportIncomingCall(payload: payload)
}
} else {
// Fallback on earlier versions
}
}
What to do next
Next, Additional Configuration Options for your iOS App.