Kelvin Lau
Kelvin Lau

Reputation: 6781

How may I trigger banner notifications using FCM?

How do I show banners in my app using Firebase's FCM feature? I was able to receive and show banner notifications via the apple provided delegate method:

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
  completionHandler([.alert, .badge, .sound])
}

However, once I try to use the FCM, this method is no longer called. Instead, it's this method that receives notification information:

extension FirebaseNotificationManager: FIRMessagingDelegate {
  func applicationReceivedRemoteMessage(_ remoteMessage: FIRMessagingRemoteMessage) {
    let appData = remoteMessage.appData
    appData.forEach { print($0) }
  }
}

I can get a printout of the appData, but I'm not sure how to actually display the banner alert.

Any help is appreciated!

Upvotes: 1

Views: 1086

Answers (2)

Kelvin Lau
Kelvin Lau

Reputation: 6781

After a day of research and experimentation, I've concluded that connecting to Firebase FCM is not necessary, for this scenario.

If you connect to FCM, you'll need to rely on Firebase's applicationReceivedRemoteMessage method to handle incoming push notifications. I still can't figure out how to display banner notifications via that method.

However, the main goal is to access the key-value pairs sent over by the push notification. It's not necessary to go through Firebase's custom object to access those.

Every UNNotification object has a userInfo dictionary (though it's fairly heavily nested):

let userInfo = response.notification.request.content.userInfo

This will contain the same information found in a FIRMessagingRemoteMessage:

let appData = remoteMessage.appData

Upvotes: 0

Nauman Malik
Nauman Malik

Reputation: 1346

i am using this for iOS 10 and working for me.

- (void)userNotificationCenter:(UNUserNotificationCenter *)center
   willPresentNotification:(UNNotification *)notification
     withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler 
 {
        NSDictionary *userInfo = notification.request.content.userInfo;

        // Print full message.
         NSLog(@"%@", userInfo);

// Change this to your preferred presentation option
      completionHandler(UNNotificationPresentationOptionAlert);

}

Upvotes: 0

Related Questions