Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.11 KB | None | 0 0
  1. - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
  2.  
  3.  
  4. NSLog(@"Unable to register for remote notifications: %@", error);
  5.  
  6. }
  7.  
  8. // This function is added here only for debugging purposes, and can be removed if swizzling is enabled.
  9. // If swizzling is disabled then this function must be implemented so that the
  10. //APNs device token can be paired to
  11. // the FCM registration token.
  12. - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
  13.  
  14.  
  15. NSLog(@"APNs device token retrieved: %@", deviceToken);
  16. // With swizzling disabled you must set the APNs device token here.
  17.  
  18. [FIRMessaging messaging].APNSToken = deviceToken;
  19. }
  20.  
  21. func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
  22. print("Firebase registration token: (fcmToken)")
  23. UserDefaults.standard.set(fcmToken, forKey: "DeviceToken")
  24. }
  25.  
  26. func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
  27. FirebaseApp.configure()
  28. if(launchOptions?[UIApplication.LaunchOptionsKey.remoteNotification] != nil){
  29.  
  30. }
  31. InstanceID.instanceID().instanceID { (result, error) in
  32. if let error = error {
  33. print("Error fetching remote instance ID: (error)")
  34. } else if let result = result {
  35. print("Remote instance ID token: (result.token)")
  36.  
  37. }
  38. }
  39.  
  40. Messaging.messaging().isAutoInitEnabled = true
  41. if #available(iOS 10.0, *) {
  42. // For iOS 10 display notification (sent via APNS)
  43. UNUserNotificationCenter.current().delegate = self
  44.  
  45. let authOptions: UNAuthorizationOptions = [.alert,.sound] // .badge,
  46. UNUserNotificationCenter.current().requestAuthorization(
  47. options: authOptions,
  48. completionHandler: {_, _ in })
  49. } else {
  50. let settings: UIUserNotificationSettings =
  51. UIUserNotificationSettings(types: [.alert,.sound], categories: nil)
  52. application.registerUserNotificationSettings(settings)
  53. }
  54.  
  55. application.registerForRemoteNotifications()
  56.  
  57. Messaging.messaging().delegate = self
  58.  
  59.  
  60. return true
  61. }
  62.  
  63. var firebaseToken: String = ""
  64.  
  65. func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
  66.  
  67. FirebaseApp.configure()
  68. self.registerForFirebaseNotification(application: application)
  69. Messaging.messaging().delegate = self
  70. return true
  71. }
  72.  
  73. func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
  74. Messaging.messaging().apnsToken = deviceToken
  75. }
  76.  
  77. func registerForFirebaseNotification(application: UIApplication) {
  78. if #available(iOS 10.0, *) {
  79. // For iOS 10 display notification (sent via APNS)
  80. UNUserNotificationCenter.current().delegate = self
  81.  
  82. let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
  83. UNUserNotificationCenter.current().requestAuthorization(
  84. options: authOptions,
  85. completionHandler: {_, _ in })
  86. } else {
  87. let settings: UIUserNotificationSettings =
  88. UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
  89. application.registerUserNotificationSettings(settings)
  90. }
  91.  
  92. application.registerForRemoteNotifications()
  93. }
  94. }
  95.  
  96. extension AppDelegate: MessagingDelegate, UNUserNotificationCenterDelegate {
  97.  
  98. //MessagingDelegate
  99. func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
  100. self.firebaseToken = fcmToken
  101. print("Firebase token: (fcmToken)")
  102. }
  103.  
  104. func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {
  105. print("didReceive remoteMessage: (remoteMessage)")
  106. }
  107.  
  108. //UNUserNotificationCenterDelegate
  109. func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
  110. print("APNs received with: (userInfo)")
  111.  
  112.  
  113. }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement