我正在使用Xcode 9.2,Swift 4.0,最小iOS版本编写VoIP应用程序,为10.3。我想接到即使是iPhone处于睡眠模式的接收来电(就像WhatsApp或Viber一样(。我已经知道我必须使用callkit。但是我不知道如何正确接收推送通知。我的意思是,我有两种方法如何接收它们。首先是AppDelegate.swift,请查看(此方法正在使用(:
import UIKit
import UserNotifications
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
//Getting permissions for Notifications
//222
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { granted, error in
}
application.registerForRemoteNotifications()
//222
return true
}
//For PUSH Notifications settings
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
print("NOTIFICATION ERROR: (error)")
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
let pushToken = deviceToken.reduce("", {$0 + String(format: "%02X", $1)}).lowercased()
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
// Receiving call?
}
}
但是,无论如何,我在做什么,如果iPhone睡觉,它不会触发来电。最近,我看到还有另一种接收推送通知的方法。请看:
import UIKit
import CallKit
import PushKit
class ViewController: UIViewController, CXProviderDelegate, PKPushRegistryDelegate {
override func viewDidLoad() {
let registry = PKPushRegistry(queue: nil)
registry.delegate = self
registry.desiredPushTypes = [PKPushType.voIP]
}
func providerDidReset(_ provider: CXProvider) {
}
func provider(_ provider: CXProvider, perform action: CXAnswerCallAction) {
action.fulfill()
}
func provider(_ provider: CXProvider, perform action: CXEndCallAction) {
action.fulfill()
}
func pushRegistry(_ registry: PKPushRegistry, didUpdate pushCredentials: PKPushCredentials, for type: PKPushType) {
print(pushCredentials.token.map { String(format: "%02.2hhx", $0) }.joined())
}
func pushRegistry(_ registry: PKPushRegistry, didReceiveIncomingPushWith payload: PKPushPayload, for type: PKPushType, completion: @escaping () -> Void) {
// Receive call?
}
}
问题是,在第一种方法中,我有此推动令牌:9BCF73AC3D6A68BE07C26D8B6A972F5B716CD5308F1A1A1A1A1A1A1A1A1A1A148B62D19B9B9B9B9B8D4BD1
第二种方法返回我这个推动令牌:1E40FAFB2963161B1BCF1BDDE00D625A879C6934E19E2FB32FB32F62B1C9272E956F
它们不等!怎么会这样?因此,主要问题是:如何接收推送通知,AppDelegate和Pushkit方法之间有什么区别,哪个令牌是正确的?
感谢您的任何帮助!
有两种类型的通知(实际上更多,但在这种情况下,只有两个(:推送通知和VOIP通知。它们具有不同的令牌和不同的接收系统。VoIP服务仅用于呼叫(Apple CallKit(,与消息和其他通知存在相反的推送服务。因此,实际上,这两种方法都是必要的,并且不能取代另一种方法。这两种方法都需要在Apple开发人员帐户中使用不同证书的方法:Apple Push Notification Service SSL用于推送通知和VoIP的VoIP服务证书。谢谢大家的帮助!如果您看到错误,请自由编辑。
首先删除此行应用程序
现在在下面的puskit中使用正确的令牌
func pushRegistry(_ registry: PKPushRegistry, didUpdate credentials: PKPushCredentials, forType type: PKPushType) {
print("(#function) voip token: (credentials.token)")
let deviceToken = credentials.token.reduce("", {$0 + String(format: "%02X", $1) })
print("(#function) token is: (deviceToken)")}
和按钮框架没有UI,例如远程或本地通知,您需要每次触发本地通知,而下面的方法称为
func pushRegistry(_ registry: PKPushRegistry, didReceiveIncomingPushWith payload: PKPushPayload, for type: PKPushType, completion: @escaping () -> Void) {
// Receive call?
}