推送通知委托方法未在Xcode 8.2.1中调用

时间:2017-01-12 10:17:37

标签: swift3 ios9 xcode8.2

我在Xcode 7.3中实现了苹果推送通知演示应用程序。它工作正常。最近我下载Xcode 8.现在我的演示应用程序无法在Xcode 7和Xcode 8中工作。委托方法没有被调用。我不知道,出了什么问题。 Xcode建议创建权利,我做了。请任何人帮助我。

谢谢, Vikash

1 个答案:

答案 0 :(得分:1)

从功能推送通知和添加UserNotification框架在BuildPhase中工作 在AppDelegate中添加委托方法UNUserNotificationCenterDelegate

 import UserNotifications

在didFinishingLaunching中......

  if #available(iOS 10.0, *) {
        let center  = UNUserNotificationCenter.current()
        center.delegate = self
        center.requestAuthorization(options: [.sound, .alert, .badge]) { (granted, error) in
            if error == nil{
                UIApplication.shared.registerForRemoteNotifications()
            }
        }
    } else {
        // Fallback on earlier versions
    }

添加这些方法

   func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {

    let deviceTokenString = deviceToken.reduce("", {$0 + String(format: "%02X", $1)})
    print(deviceTokenString)


}

func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {

    print("i am not available in simulator \(error)")

}



@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {

}

@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {

}