user10002550
user10002550

Reputation:

How can I fix this deprecated code used in the AppDelegate for Firebase Notifications?

How can the following code that has been deprecated in iOS 10 be used in Swift 4? This application is used to send push notifications using 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
}

and

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

    print(userInfo)
}

Any help is appreciated, thanks.

Upvotes: 0

Views: 611

Answers (1)

Christian Abella
Christian Abella

Reputation: 5797

This is now I do it in my apps where I use Firebase notifications.

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()

}

Upvotes: 1

Related Questions