Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase Messaging iOS 14

I have had Firebase Cloud Messaging setup in my app for a while. I recently updated one of my devices to iOS 14, and stopped receiving them on that device. A different device with iOS 13 still receives them. I'm sorry if this is a dumb issue haha but here is my App Delegate:


let appDelegate : AppDelegate = UIApplication.shared.delegate as! AppDelegate


@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate, MessagingDelegate {
    
    var customerId = ""



    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {


    application.registerForRemoteNotifications()

        FirebaseApp.configure()
        
        // Update this to your stripe PUBLISHABLE KEY
        STPPaymentConfiguration.shared().publishableKey = "private"
        
        let apiToken = "private"
        EasyPostApi.sharedInstance.setCredentials(apiToken, baseUrl: "https://api.easypost.com/v2/")
        STPTheme.default().accentColor = .red
        
        Messaging.messaging().delegate = self
        
        return true
    }

    // MARK: UISceneSession Lifecycle
    func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
        // Called when a new scene session is being created.
        // Use this method to select a configuration to create the new scene with.
        return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
    }

    func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
        // Called when the user discards a scene session.
        // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
        // Use this method to release any resources that were specific to the discarded scenes, as they will not return.
    }
    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
      Messaging.messaging().apnsToken = deviceToken
        
    }
    
    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
      print(userInfo)
    print("test")
    }

    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
                     fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
        print(userInfo)
        print("test")

      completionHandler(UIBackgroundFetchResult.newData)
    }
    
    func registerForPushNotifications() {
        // 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 })
        // For iOS 10 data message (sent via FCM
        Messaging.messaging().delegate = self
        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)")
          }
        }
        
    }

}

I have tried sending messages using both the "test" button in the firebase console, and just publishing a message like normal. It does register for notifications properly, and after checking settings, it still looks correct.

like image 524
Max S. Avatar asked Jun 30 '20 20:06

Max S.


People also ask

Does firebase cloud messaging work on iOS?

For Apple client apps, you can receive notification and data payloads up to 4000 bytes over the Firebase Cloud Messaging APNs interface.

What is FCM in iOS?

FCM delivers all messages targeting Apple apps through APNs. To learn more about receiving APNs notifications via UNUserNotificationCenter, see Apple's documentation on Handling Notifications and Notification-Related Actions.

Can you use Firebase with Swift?

Add Firebase Realtime Database to your app Use Swift Package Manager to install and manage Firebase dependencies. Visit our installation guide to learn about the different ways you can add Firebase SDKs to your Apple project, including importing frameworks directly and using CocoaPods.

Does Firebase use IDFA?

Firebase SDKs do not access IDFA, though some have integrations with Google Analytics that may involve IDFA access.


1 Answers

I had the same exact issue. A fix I found for now was to disable method swizzling:

  • To your iOS app Info.plist, add FirebaseAppDelegateProxyEnabled and set it to FALSE (boolean value 0)

Looks like you already have didRegisterForRemoteNotificationsWithDeviceToken setup, so keep that as it.

Hope this helps!

like image 121
m179 Avatar answered Oct 01 '22 20:10

m179