Advertisement
Guest User

Untitled

a guest
Jan 16th, 2017
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.15 KB | None | 0 0
  1. //
  2. // AppDelegate.swift
  3. //
  4.  
  5.  
  6. import UIKit
  7. import UserNotifications
  8.  
  9. import Firebase
  10. import FirebaseInstanceID
  11. import FirebaseMessaging
  12.  
  13. @UIApplicationMain
  14. class AppDelegate: UIResponder, UIApplicationDelegate {
  15.  
  16. var window: UIWindow?
  17. let gcmMessageIDKey = "gcm.message_id"
  18.  
  19. func application(_ application: UIApplication,
  20. didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
  21.  
  22.  
  23. if UserDefaults.standard.bool(forKey: "launched_before") == false {
  24.  
  25. self.window = UIWindow(frame: UIScreen.main.bounds)
  26.  
  27. let storyboard = UIStoryboard(name: "Settings", bundle: nil)
  28.  
  29. let initialViewController = storyboard.instantiateViewController(withIdentifier: "welcomeViewController") as! WelcomeViewController
  30.  
  31. self.window?.rootViewController = initialViewController
  32. self.window?.makeKeyAndVisible()
  33.  
  34. } else {
  35.  
  36. // Register for remote notifications. This shows a permission dialog on first run, to
  37. // show the dialog at a more appropriate time move this registration accordingly.
  38. // [START register_for_notifications]
  39. if #available(iOS 10.0, *) {
  40.  
  41. // For iOS 10 display notification (sent via APNS)
  42. UNUserNotificationCenter.current().delegate = self
  43. // For iOS 10 data message (sent via FCM)
  44. FIRMessaging.messaging().remoteMessageDelegate = self
  45.  
  46. } else {
  47. let settings: UIUserNotificationSettings =
  48. UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
  49. application.registerUserNotificationSettings(settings)
  50. }
  51.  
  52. application.registerForRemoteNotifications()
  53.  
  54. // [END register_for_notifications]
  55. FIRApp.configure()
  56.  
  57.  
  58. // Add observer for InstanceID token refresh callback.
  59. NotificationCenter.default.addObserver(self,
  60. selector: #selector(self.tokenRefreshNotification),
  61. name: .firInstanceIDTokenRefresh,
  62. object: nil)
  63.  
  64. }
  65.  
  66. return true
  67. }
  68.  
  69. // [START receive_message]
  70. func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
  71. fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
  72. // If you are receiving a notification message while your app is in the background,
  73. // this callback will not be fired till the user taps on the notification launching the application.
  74. // TODO: Handle data of notification
  75. // Print message ID.
  76. if let messageID = userInfo[gcmMessageIDKey] {
  77. print("Message ID: (messageID)")
  78. }
  79.  
  80. // Print full message.
  81. print(userInfo)
  82.  
  83. print("METHOD 2")
  84.  
  85. completionHandler(.newData)
  86. }
  87.  
  88.  
  89. // [END receive_message]
  90.  
  91. // [START refresh_token]
  92. func tokenRefreshNotification(_ notification: Notification) {
  93. if let refreshedToken = FIRInstanceID.instanceID().token() {
  94. print("InstanceID token: (refreshedToken)")
  95. }
  96.  
  97. // Connect to FCM since connection may have failed when attempted before having a token.
  98. connectToFcm()
  99. }
  100. // [END refresh_token]
  101.  
  102. // [START connect_to_fcm]
  103. func connectToFcm() {
  104. // Won't connect since there is no token
  105. guard FIRInstanceID.instanceID().token() != nil else {
  106. return;
  107. }
  108.  
  109. // Disconnect previous FCM connection if it exists.
  110. FIRMessaging.messaging().disconnect()
  111.  
  112. FIRMessaging.messaging().connect { (error) in
  113. if error != nil {
  114. print("Unable to connect with FCM. (error)")
  115. } else {
  116. print("Connected to FCM.")
  117. }
  118. }
  119. }
  120. // [END connect_to_fcm]
  121.  
  122. func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
  123. print("Unable to register for remote notifications: (error.localizedDescription)")
  124. }
  125.  
  126. // This function is added here only for debugging purposes, and can be removed if swizzling is enabled.
  127. // If swizzling is disabled then this function must be implemented so that the APNs token can be paired to
  128. // the InstanceID token.
  129. func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
  130. print("APNs token retrieved: (deviceToken)")
  131.  
  132. // With swizzling disabled you must set the APNs token here.
  133. FIRInstanceID.instanceID().setAPNSToken(deviceToken, type: FIRInstanceIDAPNSTokenType.unknown)
  134.  
  135. FIRMessaging.messaging().subscribe(toTopic: "/topics/substitutions")
  136. FIRMessaging.messaging().subscribe(toTopic: "/topics/debug")
  137. }
  138.  
  139. // [START connect_on_active]
  140. func applicationDidBecomeActive(_ application: UIApplication) {
  141. connectToFcm()
  142. }
  143. // [END connect_on_active]
  144.  
  145. // [START disconnect_from_fcm]
  146. func applicationDidEnterBackground(_ application: UIApplication) {
  147. FIRMessaging.messaging().disconnect()
  148. print("Disconnected from FCM.")
  149. }
  150. // [END disconnect_from_fcm]
  151.  
  152. func evaluateRecievedData (remoteMessage: FIRMessagingRemoteMessage) {
  153.  
  154. // Evaluate the data...
  155.  
  156. }
  157.  
  158. }
  159.  
  160. // [START ios_10_message_handling]
  161. @available(iOS 10, *)
  162. extension AppDelegate : UNUserNotificationCenterDelegate {
  163.  
  164. // Receive displayed notifications for iOS 10 devices.
  165. func userNotificationCenter(_ center: UNUserNotificationCenter,
  166. willPresent notification: UNNotification,
  167. withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
  168.  
  169. let userInfo = notification.request.content.userInfo
  170. // Print message ID.
  171. if let messageID = userInfo[gcmMessageIDKey] {
  172. print("Message ID: (messageID)")
  173. }
  174.  
  175. // Change this to your preferred presentation option
  176. completionHandler(.alert)
  177. }
  178.  
  179.  
  180. func userNotificationCenter(_ center: UNUserNotificationCenter,
  181. didReceive response: UNNotificationResponse,
  182. withCompletionHandler completionHandler: @escaping () -> Void) {
  183. let userInfo = response.notification.request.content.userInfo
  184. // Print message ID.
  185. if let messageID = userInfo[gcmMessageIDKey] {
  186. print("Message ID: (messageID)")
  187. }
  188.  
  189. print("USER HAS PRESSED BANNER")
  190.  
  191. // Do some stuff...
  192.  
  193. completionHandler()
  194. }
  195.  
  196. }
  197. // [END ios_10_message_handling]
  198. // [START ios_10_data_message_handling]
  199. extension AppDelegate : FIRMessagingDelegate {
  200. // Receive data message on iOS 10 devices while app is in the foreground.
  201. func applicationReceivedRemoteMessage(_ remoteMessage: FIRMessagingRemoteMessage) {
  202.  
  203. print(remoteMessage.appData)
  204.  
  205. self.evaluateRecievedData(remoteMessage: remoteMessage)
  206.  
  207. print("RECIEVED MESSAGE (FOREGROUND)")
  208.  
  209. }
  210.  
  211. }
  212. // [END ios_10_data_message_handling]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement