nmnir nmnir - 1 year ago 258
Swift Question

iOS VoIP Push - Failed to Register

The function

func pushRegistry(registry: PKPushRegistry!, didUpdatePushCredentials credentials: PKPushCredentials!, forType type: String!)


Is not being called.
Here is my code:

import UIKit
import PushKit
import Foundation

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, PKPushRegistryDelegate {

var window: UIWindow?

//Tried also to make it local
var voipRegistry:PKPushRegistry?

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
self.registerVoipNotifications()
return true
}
func registerVoipNotifications() {
let mainQueue = dispatch_get_main_queue()
// Create a push registry object
voipRegistry = PKPushRegistry(queue: mainQueue)
// Set the registry's delegate to self
voipRegistry!.delegate = self
// Set the push type to VoIP
voipRegistry!.desiredPushTypes = [PKPushTypeVoIP]
}
func pushRegistry(registry: PKPushRegistry!, didUpdatePushCredentials credentials: PKPushCredentials!, forType type: String!) {
//1. Create the alert controller.
let alert = UIAlertController(title: "VoIPPushTest", message: "Token", preferredStyle: .Alert)

//2. Add the text field. You can configure it however you need.
alert.addTextFieldWithConfigurationHandler({ (textField) -> Void in
textField.text = "got cred"
})

//print out the VoIP token. We will use this to test the nofications.
NSLog("voip token: \(credentials.token)")
let tokenChars = UnsafePointer<CChar>(credentials.token.bytes)
var tokenString = ""

for i in 0..<credentials.token.length {
tokenString += String(format: "%02.2hhx", arguments: [tokenChars[i]])
}
print("Device Token:", tokenString)

}
//Other standard app delegate functions below without changes


I read that it may be a certificate problem so


  1. Created new app id, profile and certificate

  2. Added background capabilities to the app

  3. Signing the code with: Code Signing Identity - iPhone Distribution, Provisioning Profile VoIPPushTestProfile



I am archiving the code and exporting for ad hoc deployment.
What am I doing wrong???

Answer Source

According to your code you don't ask the user for permission to receive push notifications. You need to call UIApplication.registerUserNotificationSettings to do so.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download