I have been scared for some time here, and this is probably my mistake, but I cannot find it anywhere else, the answer to this question.
I implemented PushKit in some way, but none of them were effective.
I added the correct background modes, correctly executed the callbacks, didUpdatePushCredentials normally called ...
However, the variable credentials: PKPushCredentials! gives me a pointer to an error ... It's not null ... not zero ... not something ... It just doesn't matter ... it's "garbage allocated ... and for that reason .. I get EXC_BREAKPOINT ..
I tried on three different devices ... Same behavior ...
I have already created Certificates for VoIP Push ...
I did it differently:
- By creating a void Push Registry object after didRegisterForRemotePushNotification
- By creating a push registry without registering for a remote push notification ...
- By creating a registry with basic, global, and custom queues.
All the same...
Here is the code:
extension AppDelegate : PKPushRegistryDelegate { func registerForVoipPush() { self.registry = PKPushRegistry(queue:nil) if self.registry != nil { self.registry!.delegate = self self.registry!.desiredPushTypes = Set<String>(arrayLiteral: PKPushTypeVoIP) } //let notificationSettings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories:nil) //UIApplication.sharedApplication().registerUserNotificationSettings(notificationSettings) } func pushRegistry(registry: PKPushRegistry!, didUpdatePushCredentials credentials: PKPushCredentials!, forType type: String!) { //print out the VoIP token. We will use this to test the nofications. NSLog("voip token: \(credentials.token)") if credentials != nil { let username = NSUserDefaults.standardUserDefaults().objectForKey("username") as? String if username != nil { ServerDefinitions.subscribeForPush(username!, token: NSString(data: credentials.token, encoding: NSUTF8StringEncoding) as! String, callback: { (retMsg) -> Void in print(retMsg) }) } } } func pushRegistry(registry: PKPushRegistry!, didReceiveIncomingPushWithPayload payload: PKPushPayload!, forType type: String!) { let payloadDict = payload.dictionaryPayload["aps"] as? Dictionary<String, String> let message = payloadDict?["alert"] //present a local notifcation to visually see when we are recieving a VoIP Notification if UIApplication.sharedApplication().applicationState == UIApplicationState.Background { let localNotification = UILocalNotification(); localNotification.alertBody = message localNotification.applicationIconBadgeNumber = 1; localNotification.soundName = UILocalNotificationDefaultSoundName; UIApplication.sharedApplication().presentLocalNotificationNow(localNotification); } else { dispatch_async(dispatch_get_main_queue(), { () -> Void in print("Incoming Call") }) } NSLog("incoming voip notfication: \(payload.dictionaryPayload)") } func pushRegistry(registry: PKPushRegistry!, didInvalidatePushTokenForType type: String!) { NSLog("token invalidated") }
}
EDIT:
Please note that the above code is an extension of AppDelegate, made only to separate the code, so it becomes more readable.
I also added var registry : PKPushRegistry? in AppDelegate. For this to work, you must call registerForVoipPush() somewhere in the code. In my case, I did this with a button.
PLEASE HELP ME!