Guest User

Untitled

a guest
Feb 22nd, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.25 KB | None | 0 0
  1. var window: UIWindow?
  2. let gcmMessageIDKey = "gcm.message_id"
  3. static var shared: AppDelegate { return UIApplication.shared.delegate as! AppDelegate }
  4. func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
  5.  
  6. FirebaseApp.configure()
  7. Messaging.messaging().delegate = self
  8. if #available(iOS 10.0, *) {
  9.  
  10. UNUserNotificationCenter.current().delegate = self
  11.  
  12. let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
  13. UNUserNotificationCenter.current().requestAuthorization(
  14. options: authOptions,
  15. completionHandler: {_, _ in })
  16. } else {
  17. let settings: UIUserNotificationSettings =
  18. UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
  19. application.registerUserNotificationSettings(settings)
  20. }
  21. application.registerForRemoteNotifications()
  22. let token = Messaging.messaging().fcmToken
  23. print("FCM token: (token ?? "")")
  24. return true
  25. }
  26.  
  27.  
  28. func application(received remoteMessage: MessagingRemoteMessage) {
  29.  
  30. print(remoteMessage.appData)
  31.  
  32. }
  33.  
  34. func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
  35. if let messageID = userInfo[gcmMessageIDKey] {
  36. print("Message ID: (messageID)")
  37.  
  38. }
  39. // Print full message.
  40. print(userInfo)
  41. }
  42.  
  43. func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
  44. fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
  45.  
  46. if let messageID = userInfo[gcmMessageIDKey] {
  47. print("Message ID: (messageID)")
  48.  
  49. }
  50.  
  51. print(userInfo)
  52. completionHandler(UIBackgroundFetchResult.newData)
  53. }
  54.  
  55. func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
  56. print("Unable to register for remote notifications: (error.localizedDescription)")
  57.  
  58. }
  59.  
  60.  
  61. func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
  62. Messaging.messaging().apnsToken = deviceToken
  63.  
  64. }
  65.  
  66. // Receive displayed notifications for iOS 10 devices.
  67. func userNotificationCenter(_ center: UNUserNotificationCenter,
  68. willPresent notification: UNNotification,
  69. withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
  70. let userInfo = notification.request.content.userInfo
  71.  
  72. if let messageID = userInfo[gcmMessageIDKey] {
  73. print("Message ID: (messageID)")
  74.  
  75. }
  76. print(userInfo)
  77. completionHandler([])
  78. }
  79.  
  80. func userNotificationCenter(_ center: UNUserNotificationCenter,
  81. didReceive response: UNNotificationResponse,
  82. withCompletionHandler completionHandler: @escaping () -> Void) {
  83. let userInfo = response.notification.request.content.userInfo
  84. // Print message ID.
  85. if let messageID = userInfo[gcmMessageIDKey] {
  86. print("Message ID: (messageID)")
  87.  
  88. }
  89. print(userInfo)
  90. completionHandler()
  91. }
  92.  
  93. func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {
  94. print("Received data message: (remoteMessage.appData)")
  95. }
Add Comment
Please, Sign In to add comment