ios - 在应用程序委托(delegate)之外注册 voip 通知

标签 ios swift

我的应用程序需要登录,我想等到用户到达 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 中。

Please download code.

//
//  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
}

enter image description here

关于ios - 在应用程序委托(delegate)之外注册 voip 通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37868158/

相关文章:

ios - 如何反射(reflect)旋转的 ImageView

ios - CollectionView 自动滚动

swift - 简单 Int 和错误

swift - 如何将类型(例如字符串)作为函数参数传递?

ios - 如何弹出到 UITabBarItem 开关上的 Root View Controller ?

ios - 应用程序可以知道用户何时从 iOS 11 驾驶时驾驶请勿打扰功能吗?

ios - Swift 中 FLAnimatedImageView 的自动布局

ios - NSDictionary 根本没有数据

ios - 自定义 UIView : animate subLayers with delay

ios - 使用 Plivo API swift 从 iOS 应用程序发送短信