微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ios – 无法使用PushKit接收VoIP通知

我正在关注 this articleM Penades’ answer构建VoIP通知演示.我可以使用didRegisterUserNotificationSettings注册通知,并可以从didUpdatePushCredentials获取voip令牌.

但是当我使用休斯顿模拟向我的设备发送通知时,我可以从控制台获得1推送通知成功消息,但设备端没有动作或日志.似乎didReceiveIncomingPushWithPayload永远不会被调用.

附:我正在使用Xcode 7.3,iPhone 6(iOS 9.3)和iPad Mini(iOS 9.3)进行开发.分布式Ad Hoc ipa用于安装.

这里的代码发布了.

import UIKit
import PushKit


@UIApplicationMain
class AppDelegate: UIResponder,UIApplicationDelegate {

var window: UIWindow?
let voipRegistry = PKPushRegistry(queue: dispatch_get_main_queue())


func application(application: UIApplication,didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.
    //Enable all notification type. VoIP Notifications don't present a UI but we will use this to show local nofications later
    let notificationSettings = UIUserNotificationSettings(forTypes: [UIUserNotificationType.Alert,UIUserNotificationType.Badge,UIUserNotificationType.sound],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.stringValue)")
    return true
}

    func applicationWillTerminate(application: UIApplication) {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    //output to see when we terminate the app
    NSLog("app terminated")
}

}

extension AppDelegate {

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

    //register for voip notifications
    NSLog("didRegisterUserNotificationSettings called")
    voipRegistry.desiredPushTypes = Set([PKPushTypeVoIP])
    voipRegistry.delegate = self;
}
}

extension AppDelegate: PKPushRegistryDelegate {


func pushRegistry(registry: PKPushRegistry!,didUpdatePushCredentials credentials: PKPushCredentials!,forType type: String!) {
    NSLog("didUpdatePushCredentials called")

    //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!) {

    NSLog("didReceiveIncomingPushWithPayload called")


    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 {
        NSLog("incoming notificaiton from background")

        let localnotification = UIlocalnotification();
        localnotification.alertBody = message
        localnotification.applicationIconBadgeNumber = 1;
        localnotification.soundName = UIlocalnotificationDefaultSoundName;

        UIApplication.sharedApplication().presentlocalnotificationNow(localnotification);
    }

    else {
        NSLog("incoming notificaiton from frontend")


        dispatch_async(dispatch_get_main_queue(),{ () -> Void in


            let alertController = UIAlertController(title: "Title",message: "This is UIAlertController default",preferredStyle: UIAlertControllerStyle.Alert)
            let cancelAction = UIAlertAction(title: "Cancel",style: UIAlertActionStyle.Cancel,handler: nil)
            let okAction = UIAlertAction(title: "OK",style: UIAlertActionStyle.Default,handler: nil)
            alertController.addAction(cancelAction)
            alertController.addAction(okAction)

            UIApplication.sharedApplication().keyWindow?.rootViewController?.presentViewController(alertController,animated: true,completion: nil)

        })
    }

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

func pushRegistry(registry: PKPushRegistry!,didInvalidatePushTokenForType type: String!) {
    NSLog("didInvalidatePushTokenForType called")
    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"
        }
    }
}
}

有关如何收到此类通知的任何想法?

解决方法

最后我解决了这个问题.
有OK的迅速源代码,这个问题是我错发送推送请求苹果的开发服务器gateway.sandBox.push.apple.com,其实我们应该请求发送到苹果的生产服务器gateway.push.apple.com.

如果你正在跟踪M Penades’ procedure,请注意,您需要修改脚本Simplepush,只需更换SSL:SSL由2195://gateway.push.apple.com://gateway.sandBox.push.apple.com 2195在第20行和再试一次.

希望对你有效.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐