Kelvin Lau Kelvin Lau - 1 month ago 30
iOS Question

Firebase Push Notifications

I've got

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

Here's what I have in my
AppDelegate.swift
file:

import UIKit
import Firebase
import UserNotifications

@UIApplicationMain
final class AppDelegate: UIResponder {
var window: UIWindow?

override init() {
super.init()
FIRApp.configure()
}
}

// 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

application.registerForRemoteNotifications()

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)")
}

connectToFcm()
}

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) {
connectToFcm()
}
}

// 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"]!)")

print(userInfo)

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

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.