We're here to keep you up-to-date with all the latest happenings in and around mobile and web application development..
20 March, 2018
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let notificationSettings = UIUserNotificationSettings(types: [.badge, .sound,.alert], categories: nil)
//register the notification settings
application.registerUserNotificationSettings(notificationSettings)
//output what state the app is in. This will be used to see when the app is started in the background
NSLog("app launched with state \(application.applicationState)")
return true
}
Since we are using the registerUserNotificationSettings method we need to implement it’s delegate callback application(application: UIApplication, didRegisterUserNotificationSettings
notificationSettings:UIUserNotificationSettings):
func application(_ application: UIApplication, didRegister notificationSettings: UIUserNotificationSettings) {
//register for voip notifications
let voipRegistry = PKPushRegistry(queue: DispatchQueue.main)
voipRegistry.desiredPushTypes = Set([PKPushType.voIP])
voipRegistry.delegate = self
}
– Add the required PKPushRegistryDelegate:
func pushRegistry(_ registry: PKPushRegistry, didUpdate pushCredentials: PKPushCredentials, for type: PKPushType) {
//print out the VoIP token. We will use this to test the notification.
let deviceToken:
String = pushCredentials.token.reduce(“”, {$0 + String(format: “%02X”, $1) })
NSLog(“voip token: \(deviceToken)”)
}
func pushRegistry(_ registry: PKPushRegistry, didReceiveIncomingPushWith payload: PKPushPayload, for type: PKPushType){
let payloadDict = payload.dictionaryPayload[“aps”] as? Dictionary<String, String>
NSLog(“incoming voip notfication: \(payload.dictionaryPayload)”)
}
Now the app can receive Push notification.
So What's Next ?
Why wait when now is the right time to build your app and chase your dream?