Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import UIKit
- import Firebase
- import FirebaseMessaging
- import UserNotifications
- @UIApplicationMain
- class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate, FIRMessagingDelegate {
- var window: UIWindow?
- func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
- FIRApp.configure()
- if #available(iOS 10.0, *) {
- let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
- UNUserNotificationCenter.current().requestAuthorization(
- options: authOptions,
- completionHandler: {_, _ in })
- // For iOS 10 display notification (sent via APNS)
- UNUserNotificationCenter.current().delegate = self
- // For iOS 10 data message (sent via FCM)
- FIRMessaging.messaging().remoteMessageDelegate = self
- } else {
- let settings: UIUserNotificationSettings =
- UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
- application.registerUserNotificationSettings(settings)
- }
- application.registerForRemoteNotifications()
- return true
- }
- func applicationReceivedRemoteMessage(_ remoteMessage: FIRMessagingRemoteMessage) {
- print("applicationReceivedRemoteMessage")
- }
- func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
- if let refreshedToken = FIRInstanceID.instanceID().token() {
- print("InstanceID token: (refreshedToken)")
- FIRMessaging.messaging().subscribe(toTopic: "/topics/global")
- }
- }
- @available(iOS 10.0, *)
- func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
- completionHandler(UNNotificationPresentationOptions.alert)
- }
- func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
- // If you are receiving a notification message while your app is in the background,
- // this callback will not be fired till the user taps on the notification launching the application.
- }
- func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
- // If you are receiving a notification message while your app is in the background,
- // this callback will not be fired till the user taps on the notification launching the application.
- }
- if Messaging.messaging().fcmToken != nil {
- Messaging.messaging().subscribe(toTopic: “foo”)
- }
- func messaging(_ messaging: Messaging, didRefreshRegistrationToken fcmToken: String) {
- Messaging.messaging().subscribe(toTopic: “foo”)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement