Advertisement
Guest User

Untitled

a guest
Aug 17th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.81 KB | None | 0 0
  1. import UIKit
  2. import Firebase
  3. import FirebaseMessaging
  4. import UserNotifications
  5.  
  6. @UIApplicationMain
  7. class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate, FIRMessagingDelegate {
  8.  
  9. var window: UIWindow?
  10.  
  11.  
  12. func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
  13.  
  14. FIRApp.configure()
  15.  
  16. if #available(iOS 10.0, *) {
  17. let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
  18. UNUserNotificationCenter.current().requestAuthorization(
  19. options: authOptions,
  20. completionHandler: {_, _ in })
  21.  
  22. // For iOS 10 display notification (sent via APNS)
  23. UNUserNotificationCenter.current().delegate = self
  24. // For iOS 10 data message (sent via FCM)
  25. FIRMessaging.messaging().remoteMessageDelegate = self
  26.  
  27. } else {
  28. let settings: UIUserNotificationSettings =
  29. UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
  30. application.registerUserNotificationSettings(settings)
  31. }
  32.  
  33. application.registerForRemoteNotifications()
  34.  
  35. return true
  36. }
  37.  
  38.  
  39.  
  40. func applicationReceivedRemoteMessage(_ remoteMessage: FIRMessagingRemoteMessage) {
  41. print("applicationReceivedRemoteMessage")
  42. }
  43.  
  44.  
  45.  
  46. func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
  47. if let refreshedToken = FIRInstanceID.instanceID().token() {
  48. print("InstanceID token: (refreshedToken)")
  49. FIRMessaging.messaging().subscribe(toTopic: "/topics/global")
  50. }
  51. }
  52.  
  53.  
  54.  
  55. @available(iOS 10.0, *)
  56. func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
  57. completionHandler(UNNotificationPresentationOptions.alert)
  58. }
  59.  
  60.  
  61.  
  62. func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
  63. // If you are receiving a notification message while your app is in the background,
  64. // this callback will not be fired till the user taps on the notification launching the application.
  65. }
  66.  
  67.  
  68.  
  69. func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
  70. // If you are receiving a notification message while your app is in the background,
  71. // this callback will not be fired till the user taps on the notification launching the application.
  72. }
  73.  
  74. if Messaging.messaging().fcmToken != nil {
  75. Messaging.messaging().subscribe(toTopic: “foo”)
  76. }
  77.  
  78. func messaging(_ messaging: Messaging, didRefreshRegistrationToken fcmToken: String) {
  79. Messaging.messaging().subscribe(toTopic: “foo”)
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement