1.配置好Voip证书(和推送通知证书一样配置)
2.项目中注意加上如下两项配置 Push Notifications 以及 Background Modes (记得勾选 Voice over IP / Remote notifications)
3.在 Appdelete 中 先注册推送通知
func registerPushNotifications() {
let options:UNAuthorizationOptions = [.alert,.sound,.badge]
UNUserNotificationCenter.current().delegate = self
UNUserNotificationCenter.current().requestAuthorization(options: options) { (success, error) in
debugPrint(success)
if let info = error?.localizedDescription {
print(info)
}
}
UIApplication.shared.registerForRemoteNotifications()
}
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
self.registerPushNotifications()
return true
}
extension AppDelegate : UNUserNotificationCenterDelegate {
/// app处于前台收到通知
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
debugPrint("app处于前台触发")
let userInfo = notification.request.content.userInfo as Dictionary
debugPrint(userInfo)
if let aps = userInfo["aps"] as? Dictionary<String,Any> {
debugPrint(aps["alert"] ?? "")
}
let options:UNNotificationPresentationOptions = [.alert,.sound,.badge]
completionHandler(options)
}
/// 点击通知进入app触发
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
debugPrint("点击通知进入app触发")
let userInfo = response.notification.request.content.userInfo
debugPrint(userInfo)
if let aps = userInfo["aps"] as? Dictionary<String,Any> {
debugPrint(aps["alert"] ?? "")
}
completionHandler()
}
}
- 推送通知
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
let deviceTokenString = deviceToken.reduce("",{$0+String(format:"%02x",$1)})
debugPrint("deviceToken: \(deviceTokenString)")
}
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
debugPrint("注册推送失败: \(error.localizedDescription)")
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
debugPrint("在前台收到")
debugPrint(userInfo)
}
然后用 SmarkPush 测试测试远程推送通知
4. 在Appdelegate 注册 Voip推送
import PushKit
func registerVoipPush() {
let voipRegistry = PKPushRegistry(queue: .main)
voipRegistry.delegate = self
voipRegistry.desiredPushTypes = [.voIP]
}
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
self.registerVoipPush()
return true
}
extension AppDelegate : PKPushRegistryDelegate {
func pushRegistry(_ registry: PKPushRegistry, didUpdate pushCredentials: PKPushCredentials, for type: PKPushType) {
let voipToken = pushCredentials.token.reduce("",{$0+String(format:"%02x",$1)})
debugPrint("voipToken: \(voipToken)")
}
func pushRegistry(_ registry: PKPushRegistry, didReceiveIncomingPushWith payload: PKPushPayload, for type: PKPushType, completion: @escaping () -> Void) {
debugPrint("didReceiveIncomingPushWith")
guard type == .voIP else {
debugPrint("Callkit& pushRegistry didReceiveIncomingPush But Not VoIP")
return
}
debugPrint("收到VoIP")
let uuid = UUID()
let handle = "影"
}
}
- 倒入依赖库 Build Phases
- 用SmartPush 测试一下Voip能否收到打印(⚠️Voip证书和类型别选错了,和推送通知不一样)