我正在开发聊天应用程序,用于消息通知,我正在使用Firebase推送通知,还用于呼叫警报。但是我搜索并了解了关于Push Kit voip通知的信息,这些通知在应用程序处于后台和kill状态时会发出呼叫警报。我启用了voip服务,并启用了Xcode功能,并为Push Kit及其委托添加了代码。现在,当我运行该应用程序时,我从该委托那里收到了一个推送令牌,
func pushRegistry(_ registry: PKPushRegistry, didUpdate pushCredentials: PKPushCredentials, for type: PKPushType) {
let token = pushCredentials.token.map { String(format: "%02.2hhx", $0) }.joined()
NSLog("voip token: \(token)")
UserDefaults.standard.set(token, forKey: Key_Push_token)
UserDefaults.standard.synchronize()
}
我将此令牌传递到服务器端并进行存储,就像服务器向我发送Firebase推送通知之前一样,我的服务器端在此推送令牌上发送了Firebase通知,但是当应用程序处于后台或终止状态时,我没有收到通知。我再次收到Firebase通知,而不是Voip通知。这是我的代码,
let mainQueue = DispatchQueue.main
let voipRegistry: PKPushRegistry = PKPushRegistry(queue: mainQueue)
voipRegistry.delegate = self
voipRegistry.desiredPushTypes = [PKPushType.voIP]
extension AppDelegate: PKPushRegistryDelegate {
func pushRegistry(_ registry: PKPushRegistry, didUpdate pushCredentials: PKPushCredentials, for type: PKPushType) {
let token = pushCredentials.token.map { String(format: "%02.2hhx", $0) }.joined()
NSLog("voip token: \(token)")
UserDefaults.standard.set(token, forKey: Key_Push_token)
UserDefaults.standard.synchronize()
}
func pushRegistry(registry: PKPushRegistry!, didUpdatePushCredentials credentials: PKPushCredentials!, forType type: String!) {
//print out the VoIP token. We will use this to test the notification.
let token = credentials.token.map { String(format: "%02.2hhx", $0) }.joined()
NSLog("voip token: \(token)")
}
private func pushRegistry(registry: PKPushRegistry!, didReceiveIncomingPushWithPayload payload: PKPushPayload!, forType type: String!) {
userInfo = payload.dictionaryPayload["aps"] as? [AnyHashable:Any]
print(userInfo!)
let payloadDict = payload.dictionaryPayload["aps"] as? Dictionary<String, String>
let message = payloadDict?["alert"]
print(message!)
//present a local notifcation to visually see when we are recieving a VoIP Notification
if UIApplication.shared.applicationState == UIApplicationState.background {
print("Background")
}
else {
}
NSLog("incoming voip notfication: \(payload.dictionaryPayload)")
}
private func pushRegistry(registry: PKPushRegistry!, didInvalidatePushTokenForType type: String!) {
NSLog("token invalidated")
}
}
当应用程序处于后台状态时,如何获取voip通知。 firebase通知和voip通知有什么区别?