2017-02-20 94 views
0

我有这个代码注册的应用程序加载推送通知,但没有发生。如何在iOS中正确注册推送通知?

import UIKit 

@UIApplicationMain 
class AppDelegate: UIResponder, UIApplicationDelegate { 

    var window: UIWindow? 


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 
     // Override point for customization after application launch. 
     singleton.deviceToken = nil; 
     application.registerForRemoteNotifications() 
     return true 
    } 

    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { 
     var token = deviceToken.description.trimmingCharacters(in: CharacterSet.init(charactersIn:"<>")); 
     token = token.replacingOccurrences(of: " ", with: ""); 
     singleton.deviceToken = token; 
    } 

    func application(_ application: UIApplication, didRegister notificationSettings: UIUserNotificationSettings) { 
     if notificationSettings.types == .none { application.registerForRemoteNotifications() } 
    } 

    func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) { 
     print("Cannot register for push notification. Repeating.") 
     application.registerForRemoteNotifications(); 
    } 
  • 我已经设置以上所有方法的断点,但被调用的唯一方法只有application(application:didFinishLaunchingWithOptions:)。所有其他人都不会被叫到。
  • 我测试设备上的应用程序从不显示任何请求来显示推送通知。
  • 我已经要求能够在目标上显示推送通知。它说:[✓]将推送通知添加到您的应用程序ID,然后[✓]将推送通知权利添加到您的权利文件。

  • 没有印刷在控制台上。

出了什么问题?以及如何解决这个问题?至少,如果我确实未能注册推送通知,我期望在控制台上反复打印Cannot register for push notification. Repeating.。但是控制台上没有任何东西。 PS:我刚刚更新了我的Xcode到8.2.1。但是,再次,我从未尝试推送通知,直到我升级到8.2.1,所以我不知道如果我使用旧的Xcode,推送通知是否有效。

+0

你有没有在项目设置中启用推送通知功能选项卡上AppDelegate.swift 请求用户权限? – Vakas

+0

@Vakas是的,我有。 –

+0

另请参阅开发人员门户上推送通知所需的配置? – Vakas

回答

0
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 
    self.registerForRemoteNotification(application:application) // call method to register push notification 
    return true 
} 

//MARK : Push Notification 
// add this code in AppDelegate.swift outside of classs 
import UserNotifications 
extension AppDelegate:UNUserNotificationCenterDelegate { 
    func registerForRemoteNotification(application: UIApplication) { 
     if #available(iOS 10.0, *) { 
      let center = UNUserNotificationCenter.current() 
      center.delegate = self 
      center.requestAuthorization(options: [.alert,.sound,.badge], completionHandler: { (complete, error) in 
       if let err = error { 
        print(err.localizedDescription) 
       } else { 
        UIApplication.shared.registerForRemoteNotifications() 
       } 
      }) 
     } 
     else { 
      let types:UIUserNotificationType = ([.alert, .sound, .badge]) 
      let settings:UIUserNotificationSettings = UIUserNotificationSettings(types:types, categories:nil) 
      application.registerUserNotificationSettings(settings) 
      application.registerForRemoteNotifications() 
     } 
    } 
    @available(iOS 10.0, *) 
    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping() -> Void) { 
     print("User Info = ",response.notification.request.content.userInfo) 
     completionHandler() 

    } 
    @available(iOS 10.0, *) 
    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { 
     print("User Info = ",notification.request.content.userInfo) 
     completionHandler([.alert,.sound,.badge]) 
    } 

    func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) { 
     print(deviceToken) 
    } 

    func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) { 
     print("Fail to register for notification : \(error.localizedDescription)") 
    } 

    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) { 

     if UIApplication.shared.applicationState == .active { 
      if let dictAps = userInfo["aps"] as? NSDictionary { 
       if let message = dictAps["alert"] as? String { 
        print(message) // show 

       } 
      } 
     } 

    } 
} 
+0

好的,等等,一旦我尝试这个,我会尽快回复你。谢谢! –

0

进口UserNotifications

框架,并添加UNUserNotificationCenterDelegate在

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { 
    registerForRemoteNotification() 
    return true 
} 

func registerForRemoteNotification() { 
    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 { 
      UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [.sound, .alert, .badge], categories: nil)) 
     UIApplication.shared.registerForRemoteNotifications() 
    } 
} 

//Getting device token 
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { 

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


} 
//In case of error 
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) { 

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

} 
//In case if you need to know the permissions granted 
UNUserNotificationCenter.current().getNotificationSettings(){ (setttings) in 

     switch setttings.soundSetting{ 
     case .enabled: 

      print("enabled sound setting") 

     case .disabled: 

      print("setting has been disabled") 

     case .notSupported: 
      print("something vital went wrong here") 
     } 
    } 
+0

好的,等等,一旦我尝试这个,我会尽快回复你。谢谢! –