Guest User

Untitled

a guest
May 25th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.69 KB | None | 0 0
  1. //
  2. // AppDelegate.swift
  3. // Test
  4. //
  5. // Created by 陳韋全 on 2018/5/21.
  6. // Copyright © 2018年 陳韋全. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10. import Firebase
  11. import UserNotifications
  12.  
  13. @UIApplicationMain
  14. class AppDelegate: UIResponder, UIApplicationDelegate {
  15.  
  16. var window: UIWindow?
  17.  
  18. func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
  19. // Override point for customization after application launch.
  20.  
  21. // 載入 firebase SDK
  22. FirebaseApp.configure()
  23.  
  24. if #available(iOS 10.0, *) {
  25. UNUserNotificationCenter.current().delegate = self
  26. Messaging.messaging().delegate = self // For iOS 10 data message (sent via FCM)
  27.  
  28. // 在程式一啟動即詢問使用者是否接受圖文(alert)、聲音(sound)、數字(badge)三種類型的通知
  29. UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge], completionHandler: { granted, error in
  30. if granted {
  31. print("允許...")
  32. } else {
  33. print("不允許...")
  34. }
  35. })
  36. } else {
  37. let settings: UIUserNotificationSettings =
  38. UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
  39. application.registerUserNotificationSettings(settings)
  40. }
  41.  
  42. application.registerForRemoteNotifications()
  43.  
  44. return true
  45. }
  46.  
  47. func applicationWillResignActive(_ application: UIApplication) {
  48. // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
  49. // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
  50. }
  51.  
  52. func applicationDidEnterBackground(_ application: UIApplication) {
  53. // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
  54. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
  55. }
  56.  
  57. func applicationWillEnterForeground(_ application: UIApplication) {
  58. // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
  59. }
  60.  
  61. func applicationDidBecomeActive(_ application: UIApplication) {
  62. // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
  63. }
  64.  
  65. func applicationWillTerminate(_ application: UIApplication) {
  66. // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
  67. }
  68.  
  69. /// iOS10 以下的版本接收推播訊息的 delegate
  70. ///
  71. /// - Parameters:
  72. /// - application: _
  73. /// - userInfo: _
  74. func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
  75.  
  76. // 印出後台送出的推播訊息(JOSN 格式)
  77. print("userInfo: \(userInfo)")
  78. }
  79.  
  80. /// iOS10 以下的版本接收推播訊息的 delegate
  81. ///
  82. /// - Parameters:
  83. /// - application: _
  84. /// - userInfo: _
  85. /// - completionHandler: _
  86. func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
  87.  
  88. // 印出後台送出的推播訊息(JOSN 格式)
  89. print("userInfo: \(userInfo)")
  90.  
  91. completionHandler(UIBackgroundFetchResult.newData)
  92. }
  93.  
  94. /// 推播失敗的訊息
  95. ///
  96. /// - Parameters:
  97. /// - application: _
  98. /// - error: _
  99. func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
  100. print("Unable to register for remote notifications: \(error.localizedDescription)")
  101. }
  102.  
  103. /// 取得 DeviceToken,通常 for 後台人員推播用
  104. ///
  105. /// - Parameters:
  106. /// - application: _
  107. /// - deviceToken: _
  108. func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
  109.  
  110. // 將 Data 轉成 String
  111. let deviceTokenString = deviceToken.reduce("", {$0 + String(format: "%02X", $1)})
  112. print("deviceTokenString: \(deviceTokenString)")
  113.  
  114. // 將 Device Token 送到 Server 端...
  115.  
  116. }
  117. }
  118.  
  119. @available(iOS 10, *)
  120. extension AppDelegate: UNUserNotificationCenterDelegate {
  121.  
  122. /// 推播送出時即會觸發的 delegate
  123. ///
  124. /// - Parameters:
  125. /// - center: _
  126. /// - notification: _
  127. /// - completionHandler: _
  128. func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
  129.  
  130. // 印出後台送出的推播訊息(JOSN 格式)
  131. let userInfo = notification.request.content.userInfo
  132. print("userInfo: \(userInfo)")
  133.  
  134. // 可設定要收到什麼樣式的推播訊息,至少要打開 alert,不然會收不到推播訊息
  135. completionHandler([.badge, .sound, .alert])
  136. }
  137.  
  138. /// 點擊推播訊息時所會觸發的 delegate
  139. ///
  140. /// - Parameters:
  141. /// - center: _
  142. /// - response: _
  143. /// - completionHandler: _
  144. func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
  145.  
  146. // 印出後台送出的推播訊息(JOSN 格式)
  147. let userInfo = response.notification.request.content.userInfo
  148. print("userInfo: \(userInfo)")
  149.  
  150. completionHandler()
  151. }
  152. }
  153.  
  154. extension AppDelegate: MessagingDelegate {
  155.  
  156. /// iOS10 含以上的版本用來接收 firebase token 的 delegate
  157. ///
  158. /// - Parameters:
  159. /// - messaging: _
  160. /// - fcmToken: _
  161. func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
  162.  
  163. // 用來從 firebase 後台推送單一裝置所必須的 firebase token
  164. print("Firebase registration token: \(fcmToken)")
  165. }
  166. }
Add Comment
Please, Sign In to add comment