firebase-ios-sdk: iOS: Notification doesn't appear if my app is in foreground but works if it's in background
-
Using APNS Auth key instead of certificates.
-
React Native Project
-
xcode: 11.3.1
-
pods:
pod ‘RNFirebase’, :path => ‘…/node_modules/react-native-firebase/ios’ pod ‘Firebase/Analytics’ pod ‘Firebase/Core’ pod ‘Firebase/Messaging’
Here’s my AppDelegate:
//
// AppDelegate.swift
import Foundation
import UIKit
import AVFoundation
import CoreLocation
import CoreMotion
import Firebase
import Siren
import UserNotifications
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
let gcmMessageIDKey = "gcm.message_id"
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
FirebaseApp.configure()
self.initializeFCM(application)
return true
}
private func initializeFCM(_ application: UIApplication)
{
print("initializeFCM")
if #available(iOS 10.0, *)
{
UNUserNotificationCenter.current().delegate = self
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { (accepted, error) in
!accepted ? print("Notification access denied.") : print("Notification access accepted.")
}
} else {
let notificationTypes: UIUserNotificationType = [.alert,.badge, .sound]
let settings = UIUserNotificationSettings(types: notificationTypes, categories: nil);
application.registerUserNotificationSettings(settings);
}
Messaging.messaging().delegate = self
application.registerForRemoteNotifications();
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
print("DEVICE TOKEN = \(deviceToken)")
Messaging.messaging().apnsToken = deviceToken
}
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
print(error)
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
if let messageID = userInfo[(gcmMessageIDKey as AnyObject) as! NSObject] {
print("Message ID: \(messageID)")
}
print(userInfo)
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
if let messageID = userInfo[gcmMessageIDKey] {
print("Message ID: \(messageID)")
}
print(userInfo)
completionHandler(UIBackgroundFetchResult.newData)
}
}
extension AppDelegate : MessagingDelegate {
// [START refresh_token]
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
print("Firebase registration token: \(fcmToken)")
Messaging.messaging().shouldEstablishDirectChannel = true
let dataDict:[String: String] = ["token": fcmToken]
NotificationCenter.default.post(name: Notification.Name("FCMToken"), object: nil, userInfo: dataDict)
}
func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {
print("Received data message: \(remoteMessage.appData)")
}
}
@available(iOS 10.0, *)
extension AppDelegate: UNUserNotificationCenterDelegate {
// Receive displayed notifications for iOS 10 devices.
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
let userInfo = notification.request.content.userInfo
if let messageID = userInfo[gcmMessageIDKey] {
print("Message ID: \(messageID)")
}
// Print full message.
print(userInfo)
// Change this to your preferred presentation option
completionHandler([.alert, .badge, .sound])
}
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void) {
let userInfo = response.notification.request.content.userInfo
// Print message ID.
if let messageID = userInfo[gcmMessageIDKey] {
print("Message ID: \(messageID)")
}
// Print full message.
print(userInfo)
completionHandler()
}
}
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Comments: 18 (8 by maintainers)
I’d set my delegate property in application(didFinishLaunchingWithOptions) method. I can’t figure out what’s going wrong.
For reference I’d shared my appDelegate above(in template) If you can have a look. @morganchen12
@ryanwilson I’ll surely update the version and get back to you if I face any other issue. Thank you !