0

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.

  • 1
    Possible duplicate of [Registering for Push Notifications in Xcode 8/Swift 3.0?](https://stackoverflow.com/questions/37956482/registering-for-push-notifications-in-xcode-8-swift-3-0) – mfaani Jun 27 '18 at 23:18
  • 1
    See the 2nd answer to the duplicate link... – mfaani Jun 27 '18 at 23:19
  • try this https://github.com/firebase/quickstart-ios/tree/master/messaging/MessagingExampleSwift – naga Jun 28 '18 at 06:33

1 Answers1

1

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

}
Christian Abella
  • 5,747
  • 2
  • 30
  • 42