我的应用程序需要登录,我想等到用户到达 AskForVOIPNotificationsViewController 后再请求呈现推送/VoIP 通知的权限。下面的代码可以工作,问题是 AppDelegate 中的 pushRegistry 没有意识到它具有权限,并且 AppDelegate 中的 didUpdatePushCredentials 没有被调用。该代码永远不会运行,服务器也永远不会获取设备 token 。但是,如果我关闭应用程序并重新启动,则会调用 didUpdatePushCredentials,服务器会获取 token 并且用户能够接收通知。
如何确保从 AskForVOIPNotificationsViewController 调用 didUpdatePushCredentials/PKPushRegistry,以便用户能够立即接收 voip 通知而无需重新启动应用程序?
我根据类似的 question 实现了我的代码,但我无法让它与 PushKit 一起使用。
非常感谢任何帮助 - 谢谢!
In AskForVOIPNotificationsViewController
func registerForNotifications() {
let notificationSettings: UIUserNotificationSettings! = UIApplication.sharedApplication().currentUserNotificationSettings()
if !notificationSettings.types.contains([.Badge, .Sound, .Alert]) {
let notificationTypes: UIUserNotificationType = [.Badge, .Sound, .Alert]
let notificationSettings = UIUserNotificationSettings(forTypes: notificationTypes, categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(notificationSettings)
}
}
In App delegate
import UIKit
import PushKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, PKPushRegistryDelegate {
var voipRegistry:PKPushRegistry!
var window: UIWindow?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
registerForVoIPPushes()
return true
}
func registerForVoIPPushes() {
voipRegistry = PKPushRegistry(queue: dispatch_get_main_queue())
voipRegistry.delegate = self
voipRegistry.desiredPushTypes = Set([PKPushTypeVoIP])
print("VOIP Push registered")
}
func pushRegistry(registry: PKPushRegistry!, didUpdatePushCredentials credentials: PKPushCredentials!, forType type: String!) {
let voipToken: String! = credentials.token.description
print("\n\n##### didUpdatePushCredentials: \n\n", voipToken)
**// update server with device token HERE**
}
func pushRegistry(registry: PKPushRegistry!, didReceiveIncomingPushWithPayload payload: PKPushPayload!, forType type: String!) {
print("\n\n## DID RECEIVE NOTIFICATION ## \n\n")
let data = payload.dictionaryPayload
let aps = data["aps"] as! [String: AnyObject]
let alert = aps["alert"] as! [String: AnyObject]
let localNotification = UILocalNotification()
//setup the notification
localNotification.alertBody = alert["body"] as? String
localNotification.alertTitle = alert["title"] as? String
localNotification.soundName = "Simple_ring_tone_29s.aiff"
localNotification.alertAction = alert["action-loc-key"] as? String
UIApplication.sharedApplication().applicationIconBadgeNumber = 1
//show the notification
UIApplication.sharedApplication().presentLocalNotificationNow(localNotification)
}
最佳答案
这对我有用,我希望对你也有用。
但这仍然不是一个好主意,这些东西必须在 appdelegate 中。
//
// ViewController.swift
// PushDemo
//
// Created by Hasya.Panchasra on 01/07/16.
// Copyright © 2016 bv. All rights reserved.
//
import UIKit
import PushKit
class ViewController: UIViewController,PKPushRegistryDelegate {
override func viewDidLoad() {
super.viewDidLoad()
self.PushKitRegistration()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//MARK: - PushKitRegistration
func PushKitRegistration()
{
let mainQueue = dispatch_get_main_queue()
// Create a push registry object
if #available(iOS 8.0, *) {
let voipRegistry: PKPushRegistry = PKPushRegistry(queue: mainQueue)
// Set the registry's delegate to self
voipRegistry.delegate = self
// Set the push type to VoIP
voipRegistry.desiredPushTypes = [PKPushTypeVoIP]
} else {
// Fallback on earlier versions
}
}
@available(iOS 8.0, *)
func pushRegistry(registry: PKPushRegistry!, didUpdatePushCredentials credentials: PKPushCredentials!, forType type: String!) {
// Register VoIP push token (a property of PKPushCredentials) with server
let hexString : String = UnsafeBufferPointer<UInt8>(start: UnsafePointer(credentials.token.bytes),
count: credentials.token.length).map { String(format: "%02x", $0) }.joinWithSeparator("")
print(hexString)
}
@available(iOS 8.0, *)
func pushRegistry(registry: PKPushRegistry!, didReceiveIncomingPushWithPayload payload: PKPushPayload!, forType type: String!) {
// Process the received push
}
}
<小时/>
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let types: UIRemoteNotificationType = [.Alert, .Badge, .Sound]
application.registerForRemoteNotificationTypes(types)
return true
}
关于ios - 在应用程序委托(delegate)之外注册 voip 通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37868158/