首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法使用VoIP接收PushKit通知

无法使用VoIP接收PushKit通知
EN

Stack Overflow用户
提问于 2016-04-18 18:36:21
回答 1查看 4.2K关注 0票数 4

我正在跟踪这篇文章M·佩内斯的回答构建VoIP通知演示。我能够注册通知使用didRegisterUserNotificationSettings和可以从didUpdatePushCredentials获得网络电话令牌。

但是当我使用休斯顿模拟发送通知到我的设备时,我可以从控制台获得1 push notification sent successfully消息,但是设备端没有任何操作或日志。didReceiveIncomingPushWithPayload似乎从未调用过。

我使用Xcode 7.3、iPhone 6(iOS 9.3)和iPad Mini(iOS 9.3)进行开发。用于安装的分布式特设ipa。

密码贴在这里。

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

你知道如何收到这样的通知吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-04-20 15:24:26

最后我解决了这个问题。有可以快速源代码,问题是我错误地发送推请求到苹果的开发服务器gateway.sandbox.push.apple.com,实际上我们应该发送请求到苹果的生产服务器gateway.push.apple.com

如果您正在跟踪M·佩内斯程序,请注意您需要修改简朴脚本,只需在第20行用ssl://gateway.push.apple.com:2195替换ssl://gateway.sandbox.push.apple.com:2195,然后再试一次。

希望它对你有用。

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

https://stackoverflow.com/questions/36701787

复制
相关文章

相似问题

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