Kelvin Lau Kelvin Lau - 9 months ago 129
iOS Question

Firebase Push Notifications

I've got

working, but I can't get the traditional banner notifications working.

Here's what I have in my

import UIKit
import Firebase
import UserNotifications

final class AppDelegate: UIResponder {
var window: UIWindow?

override init() {

// MARK: - UIApplicationDelegate
extension AppDelegate: UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(options: authOptions, completionHandler: { _, _ in })
UNUserNotificationCenter.current().delegate = self

FIRMessaging.messaging().remoteMessageDelegate = self


NotificationCenter.default.addObserver(self, selector: #selector(tokenRefreshNotification), name: .firInstanceIDTokenRefresh, object: nil)
return true

func tokenRefreshNotification(notification: Notification) {
if let refreshedToken = FIRInstanceID.instanceID().token() {
print("InstanceID token: \(refreshedToken)")


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

func applicationDidBecomeActive(_ application: UIApplication) {

// MARK: - UNUserNotificationCenterDelegate
extension AppDelegate: UNUserNotificationCenterDelegate {
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
let userInfo = notification.request.content.userInfo

print("Message ID: \(userInfo["gcm.message_id"]!)")


completionHandler([.alert, .sound])

// MARK:
extension AppDelegate: FIRMessagingDelegate {
func applicationReceivedRemoteMessage(_ remoteMessage: FIRMessagingRemoteMessage) {
print("FIRMessagingRemoteMessage Received: \(remoteMessage.appData)")

Everything is configured according to the documentation. I'm getting console outputs when I send messages to my app via the notifications console on Firebase.

However, banners don't appear when notifications are sent. When the app is backgrounded, notifications don't appear to be sent either - only when the app is in the foreground.

Looking for help. Thanks in advance!

Answer Source

Try disabling method swizzling.

Add FirebaseAppDelegateProxyEnabled to info.plist and set it to NO.

Then add this code to the app delegate

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
  FIRInstanceID.instanceID().setAPNSToken(deviceToken, type: FIRInstanceIDAPNSTokenTypeSandbox)

Delete your app from the device and reinstall it. please test if application:didRegisterForRemoteNotificationsWithDeviceToken is called. If its called, now try sending a notification from the firebase console.