在app delegate

时间:2016-06-16 19:46:22

标签: ios swift

我的应用程序需要入门,我想等到用户到达AskForVOIPNotificationsViewController之后才能请求获得推送/ voip通知的权限。下面的代码有点工作,问题是AppDelegate中的pushRegistry没有意识到它有权限并且AppDelegate中的didUpdatePushCredentials没有被调用。代码永远不会运行,服务器永远不会获取设备令牌。但是,如果我关闭应用程序并重新启动,则会调用didUpdatePushCredentials,服务器将获取令牌,并且用户可以接收通知。

如何确保从AskForVOIPNotificationsViewController调用didUpdatePushCredentials / PKPushRegistry,以便用户能够立即接收voip通知而无需重新启动应用程序?

我根据类似的question实现了我的代码,但我无法使用PushKit。

非常感谢任何帮助 - 谢谢!

  

在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)
    }
}
  

在App代理

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)

    }

2 个答案:

答案 0 :(得分:0)

这对我有用,我希望这也适合你。

仍然不是一个好主意,这个东西必须在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

答案 1 :(得分:0)

根据您发布的代码,您似乎唯一可以拨打registerForVoIPPushes()的地方位于application(_:didFinishLaunchingWithOptions:)

由于registerForVoIPPushes()设置了voip注册表委托,因此您应该在用户授予通知权限时调用它。

根据this tutorialregisterUserNotificationSettings(userNotificationSettings)有一个代表回调application(_:didRegisterWithNotificationSettings:),您可以将其用于此目的:

func application(application: UIApplication, didRegisterUserNotifications notificationSettings: UIUserNotificationSettings)
{
    // Check whether notifications are permitted
    if notificationSettings != .None
    {
        print("Permission for notifications granted.")
        registerForVoIPPushes()
    }
}