如何修复AppDelegate for Firebase Notifications中使用的已弃用代码?

时间:2018-06-27 22:34:36

标签: ios swift firebase

如何在Swift 4中使用以下已在iOS 10中弃用的代码?此应用程序用于使用Firebase发送推送通知。

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.

    let notificationTypes : UIUserNotificationType = [UIUserNotificationType.alert, UIUserNotificationType.badge, UIUserNotificationType.sound]
    let notificationSettings = UIUserNotificationSettings(forTypes: notificationTypes, categories: nil)

    application.registerForRemoteNotifications()
    application.registerUserNotificationSettings(notificationSettings)


    return true
}

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject],
                 fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
    // Print message ID.
    print("Message ID: \(userInfo["gcm.message_id"]!)")

    print(userInfo)
}

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

现在,我在使用Firebase通知的应用中执行此操作。

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    if #available(iOS 10.0, *) {
      // For iOS 10 display notification (sent via APNS)
      UNUserNotificationCenter.current().delegate = self

      let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
      UNUserNotificationCenter.current().requestAuthorization(
          options: authOptions,
          completionHandler: {_, _ in })

      // For iOS 10 data message (sent via FCM)
      Messaging.messaging().remoteMessageDelegate = self

    } else {
      let settings: UIUserNotificationSettings =
          UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
      application.registerUserNotificationSettings(settings)
    }

    application.registerForRemoteNotifications()

    FirebaseApp.configure()

}