Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
- NSLog(@"Unable to register for remote notifications: %@", error);
- }
- // This function is added here only for debugging purposes, and can be removed if swizzling is enabled.
- // If swizzling is disabled then this function must be implemented so that the
- //APNs device token can be paired to
- // the FCM registration token.
- - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
- NSLog(@"APNs device token retrieved: %@", deviceToken);
- // With swizzling disabled you must set the APNs device token here.
- [FIRMessaging messaging].APNSToken = deviceToken;
- }
- func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
- print("Firebase registration token: (fcmToken)")
- UserDefaults.standard.set(fcmToken, forKey: "DeviceToken")
- }
- func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
- FirebaseApp.configure()
- if(launchOptions?[UIApplication.LaunchOptionsKey.remoteNotification] != nil){
- }
- InstanceID.instanceID().instanceID { (result, error) in
- if let error = error {
- print("Error fetching remote instance ID: (error)")
- } else if let result = result {
- print("Remote instance ID token: (result.token)")
- }
- }
- Messaging.messaging().isAutoInitEnabled = true
- if #available(iOS 10.0, *) {
- // For iOS 10 display notification (sent via APNS)
- UNUserNotificationCenter.current().delegate = self
- let authOptions: UNAuthorizationOptions = [.alert,.sound] // .badge,
- UNUserNotificationCenter.current().requestAuthorization(
- options: authOptions,
- completionHandler: {_, _ in })
- } else {
- let settings: UIUserNotificationSettings =
- UIUserNotificationSettings(types: [.alert,.sound], categories: nil)
- application.registerUserNotificationSettings(settings)
- }
- application.registerForRemoteNotifications()
- Messaging.messaging().delegate = self
- return true
- }
- var firebaseToken: String = ""
- func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
- FirebaseApp.configure()
- self.registerForFirebaseNotification(application: application)
- Messaging.messaging().delegate = self
- return true
- }
- func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
- Messaging.messaging().apnsToken = deviceToken
- }
- func registerForFirebaseNotification(application: UIApplication) {
- if #available(iOS 10.0, *) {
- // For iOS 10 display notification (sent via APNS)
- UNUserNotificationCenter.current().delegate = self
- let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
- UNUserNotificationCenter.current().requestAuthorization(
- options: authOptions,
- completionHandler: {_, _ in })
- } else {
- let settings: UIUserNotificationSettings =
- UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
- application.registerUserNotificationSettings(settings)
- }
- application.registerForRemoteNotifications()
- }
- }
- extension AppDelegate: MessagingDelegate, UNUserNotificationCenterDelegate {
- //MessagingDelegate
- func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
- self.firebaseToken = fcmToken
- print("Firebase token: (fcmToken)")
- }
- func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {
- print("didReceive remoteMessage: (remoteMessage)")
- }
- //UNUserNotificationCenterDelegate
- func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
- print("APNs received with: (userInfo)")
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement