0

My application is implementing Firebase Cloud messaging to send notifications. Whenever I use the Firebase console to test the Firebase Notifications, the notifications are being handled by userNotificationCenter functions will present and didReceiveRemoteNotification and not by the Firebase applicationReceivedRemoteMessage function, am I missing something? Also, the userNotification functions do not have any data when I try to print the notification that just came from Firebase. here is my set up:

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate, FIRMessagingDelegate {

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        UNUserNotificationCenter.current().delegate = self
        FIRMessaging.messaging().remoteMessageDelegate = self
        FIRApp.configure()
        registerForFireBaseNotifications()
    //Other set up variables
    connectToFcm()

        return true
    }

func registerForFireBaseNotifications(){
        let authOptions: UNAuthorizationOptions = [.alert, .sound, .badge]
        UNUserNotificationCenter.current().requestAuthorization(options: authOptions, completionHandler: {_, _ in })
        application.registerForRemoteNotifications()
    }

    func applicationReceivedRemoteMessage(_ remoteMessage: FIRMessagingRemoteMessage) {
        print("Recieved remote firebase notification: %@", remoteMessage.appData)
    }

    func tokenRefreshNotification(notification: NSNotification) {
        let refreshedToken = FIRInstanceID.instanceID().token()
        print("FCM: Connected to FCM. Token : \(String(describing: refreshedToken))")
        connectToFcm()
    }

    func connectToFcm() {
        // Won't connect since there is no token
        guard FIRInstanceID.instanceID().token() != nil else {
            print("FCM: Token does not exist.")
            return
        }

        // Disconnect previous FCM connection if it exists.
        FIRMessaging.messaging().disconnect()

        FIRMessaging.messaging().connect { (error) in
            if error != nil {
                print("FCM: Unable to connect with FCM. \(error.debugDescription)")
            } else {
                print("Connected to FCM.")
            }
        }
    }

//Non firebase notifications
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (_ options: UNNotificationPresentationOptions) -> Void) {
    //do something with notifications that came while on foreground
}

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
    //do something with notifications that came from background
}

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    FIRInstanceID.instanceID().setAPNSToken(deviceToken, type: FIRInstanceIDAPNSTokenType.sandbox)
}
paul590
  • 1,385
  • 1
  • 22
  • 43

1 Answers1

0

I was able to keep digging and found the answer that I was looking for. For anybody that is struggling with this and understanding how the Firebase notifications work with IOS. The code above for the set up is correct. The main issue that I was having was that notification were not being addressed through the Firebase function applicationReceivedRemoteMessage. This was incorrect from my understanding! Firebase console allows you to send messages as notifications only! which means your app will get the notification through apns! If you want to fire the applicationReceivedRemoteMessage function you need to send a message as a data json object. Which you can do so through postman look more here: Unable to send data message using firebase console . Hope this helps!

paul590
  • 1,385
  • 1
  • 22
  • 43