7

我在这里挣扎了一段时间,这可能是我的错误,但我在任何地方都找不到,这是一个答案。

我已经以某些方式实现了 PushKit,但没有一个是有效的。

我添加了正确的背景模式,以正确的方式实现了回调,didUpdatePushCredentials正在正常调用......

但是,credentials: PKPushCredentials!变量给了我一个错误指针……它不是空的……不是零……什么都不是……它根本没有值……它被分配了垃圾……出于这个原因..我收到 EXC_BREAKPOINT..

我已经尝试了 3 种不同的设备...相同的行为...

我已经为 VoIP 推送创建了证书...

我以不同的方式做到了:

  • 通过在 didRegisterForRemotePushNotification 之后创建 void Push Registry 对象
  • 通过创建推送注册表而不注册远程推送通知...
  • 通过使用主队列、全局队列和自定义队列创建注册表..

总是一样...

这是代码:

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")
}

}

编辑:

请注意,上面的代码是 AppDelegate 的扩展,只是为了分隔代码,所以它变得更具可读性。

我还添加var registry : PKPushRegistry?了 AppDelegate 的。为了让它工作,你必须registerForVoipPush()在代码中的某个地方调用。就我而言,我是通过一个按钮完成的。

请帮我!

4

2 回答 2

3

我遇到了同样的问题,我通过在启动 VoIP 注册之前添加了一个经典的 Push 实现来解决这个问题......我猜苹果希望以某种方式确保您首先要求用户进行基本推送并在授权您处理之前获得用户的确认进一步无声的 voip 东西......

为您的应用启用推送通知... 在此处输入图像描述

然后,这是整个生成的 AppDelegate 文件,与您的非常相似:

import UIKit
import PushKit


@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.

        //Enable all notification type.
        let notificationSettings = UIUserNotificationSettings(forTypes: [UIUserNotificationType.Alert, UIUserNotificationType.Badge, UIUserNotificationType.Sound] , categories: nil)

        //register the notification settings
        application.registerUserNotificationSettings(notificationSettings)


        NSLog("app launched with state \(application.applicationState.stringValue)")

        return true
    }



    func applicationWillResignActive(application: UIApplication) {

    }

    func applicationDidEnterBackground(application: UIApplication) {

    }

    func applicationWillEnterForeground(application: UIApplication) {

    }

    func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.

        NSLog("app terminated")
    }


}


extension AppDelegate {

    func application(application: UIApplication, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings) {

        //register for voip notifications
        let voipRegistry = PKPushRegistry(queue: dispatch_get_main_queue())
        voipRegistry.desiredPushTypes = Set([PKPushTypeVoIP])
        voipRegistry.delegate = self;

        NSLog("didRegisterUserNotificationSettings")
    }
}

extension AppDelegate: PKPushRegistryDelegate {


    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)")
    }

    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

                let alert = UIAlertView(title: "VoIP Notification", message: message, delegate: nil, cancelButtonTitle: "Ok");
                alert.show()
            })
        }

        NSLog("incoming voip notfication: \(payload.dictionaryPayload)")
    }

    func pushRegistry(registry: PKPushRegistry!, didInvalidatePushTokenForType type: String!) {

        NSLog("token invalidated")
    }
}

extension UIApplicationState {

    //help to output a string instead of an enum number
    var stringValue : String {
        get {
            switch(self) {
            case .Active:
                return "Active"
            case .Inactive:
                return "Inactive"
            case .Background:
                return "Background"
            }
        }
    }
}

有关更多信息,这是受这篇文章的启发

于 2016-05-17T23:39:21.713 回答
0

作为@JBA,我忘记激活推送通知。看起来即使您只需要推送 VOIP,您也必须激活推送(传统)

于 2016-09-15T12:23:25.757 回答