首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PushKit未构思凭据

PushKit未构思凭据
EN

Stack Overflow用户
提问于 2016-01-05 18:52:33
回答 2查看 2K关注 0票数 7

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

我在某些方面实现了PushKit,但没有一种是有效的。

我添加了正确的后台模式,以正确的方式实现了回调,didUpdatePushCredentials通常被调用.

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

我试过三种不同的装置..。同样的行为..。

我已经为VoIP Push创建了证书.

我用不同的方式做过:

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

一切都一样..。

这是密码:

代码语言:javascript
复制
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的扩展,它只用于分隔代码,因此它更容易读懂。

我还在AppDelegate上添加了var registry : PKPushRegistry?,为了使其工作,您必须在代码中的某个地方调用registerForVoipPush()。在我的例子中,我是从一个按钮上做的。

请帮帮我!

EN

回答 2

Stack Overflow用户

发布于 2016-05-17 23:39:21

我遇到了同样的问题,我通过在启动VoIP注册之前添加一个经典的Push实现来解决这个问题.我想苹果想要确保你先向用户询问基本的推送,并在授权你处理更多的无声voip信息之前得到用户的支持.

启用应用程序的推送通知.

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

代码语言:javascript
复制
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"
            }
        }
    }
}

有关更多信息,这是受这个职位启发的。

票数 3
EN

Stack Overflow用户

发布于 2016-09-15 12:23:25

作为@JBA,我忘记激活推送通知。看起来就像你需要的只是推送VOIP,你必须激活推送(传统)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34619187

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档