Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.58 KB | None | 0 0
  1. import UIKit
  2. import Firebase
  3. import UserNotifications
  4.  
  5. @UIApplicationMain
  6. class AppDelegate: UIResponder, UIApplicationDelegate {
  7.  
  8. var window: UIWindow?
  9. func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
  10.  
  11. // Register for push notification
  12. self.registerForNotification()
  13. FirebaseApp.configure()
  14. Messaging.messaging().delegate = self
  15. return true
  16. }
  17. }
  18.  
  19. extension AppDelegate: UNUserNotificationCenterDelegate {
  20.  
  21. //MARK: - Register For RemoteNotification
  22. func registerForNotification() {
  23. UNUserNotificationCenter.current().delegate = self
  24. UNUserNotificationCenter.current().requestAuthorization(options: [.badge, .alert, .sound]) { granted, error in }
  25. UIApplication.shared.registerForRemoteNotifications()
  26. }
  27.  
  28. func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
  29. #if DEVELOPMENT
  30. Messaging.messaging().setAPNSToken(deviceToken, type: .sandbox)
  31. #else
  32. Messaging.messaging().setAPNSToken(deviceToken, type: .prod)
  33. #endif
  34. }
  35.  
  36. func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
  37. debugPrint("Unable to register for remote notifications: (error.localizedDescription)")
  38. }
  39.  
  40. //MARK: - UNUserNotificationCenterDelegate
  41. func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
  42. debugPrint(userInfo)
  43. }
  44.  
  45. func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
  46.  
  47. let userInfo = notification.request.content.userInfo
  48. debugPrint(userInfo)
  49. completionHandler([.badge, .alert, .sound])
  50. }
  51.  
  52.  
  53. func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
  54. let userInfo = response.notification.request.content.userInfo
  55. print(userInfo)
  56. completionHandler()
  57. }
  58. }
  59.  
  60. extension AppDelegate: MessagingDelegate {
  61.  
  62. //MARK: - MessagingDelegate
  63. func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
  64. print(fcmToken)
  65. }
  66.  
  67. func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {
  68. print(remoteMessage.appData)
  69. }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement