skip to Main Content

While configuring my iOS app to receive push notifications through Firebase Cloud Messaging, I’ve been stuck trying to make the push notifications sent by the server work. However, when I test using the FCM console, the test notification arrives in the iPhone.

I have tried a lot of different configurations in the AppDelegate, with swizzling on and off.

Does anyone have any clue on what could I be possibly missing?

2

Answers


  1. Chosen as BEST ANSWER

    Turns out it was necessary to send the notification content specifically for APNS, in the APNS field of the message. Found out by reading this topic.


  2. At all, you should add several methods. Also, you can use https://icloud.developer.apple.com/dashboard/notifications for the test, for this you will need deviceToken from point 2. Also, your server should add parameters sound: default and mutable-content: 1.

    1.

    func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String?) {
            let dataDict: [String: String] = ["token": fcmToken ?? ""]
            NotificationCenter.default.post(
                name: Notification.Name("FCMToken"),
                object: nil,
                userInfo: dataDict
            )
        }
    
    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
           let deviceTokenString = deviceToken.reduce("", { $0 + String(format: "%02X", $1) })
            Messaging.messaging().apnsToken = deviceToken
    }
    
    Messaging.messaging().token { [weak self] token, error in
                    if let error = error {
                        print("Error fetching FCM registration token: (error)")
                    } else if let token = token {
                        print("FCM registration token: (token)")
                        self?.registerFCMToken(token: token) // send your FCM token to server
                    }
                }
    
    func userNotificationCenter(_ center: UNUserNotificationCenter,
                                    willPresent notification: UNNotification) async
        -> UNNotificationPresentationOptions {
            return [[.sound, .badge, .banner]]
        }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search