在 iOS 上,FCM 通知在后台收到,但不在前台收到

发布于 2025-01-11 16:39:41 字数 2321 浏览 0 评论 0原文

我在后台收到推送到 iOS 设备的 FCM 消息,但在前台却收不到。我发现很多文章中其他人也有同样的问题,并通过添加 willPresentNotification 函数解决了它。但这并不能解决我的问题。我的 FCM 代码如下:

class FCMManager: NSObject, MessagingDelegate, UNUserNotificationCenterDelegate {

static let shared = FCMManager()
let database = Database.database().reference()

public func registerForPushNotifications() {
    UNUserNotificationCenter.current().delegate = self
    let authOptions: UNAuthorizationOptions = [.alert, .sound, .announcement, .badge, .carPlay, .providesAppNotificationSettings]
    UNUserNotificationCenter.current()
        .requestAuthorization( options: authOptions,
                               completionHandler: { granted, _ in
            guard granted else {
                print("Apple push notifications are not registered")
                return
            }
            print("Apple push notifications are registered")
        })
    Messaging.messaging().delegate = self
    DispatchQueue.main.async {
        UIApplication.shared.registerForRemoteNotifications()
    }
    updatePushTokenIfNeeded()
 }

 func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String?) {
     updatePushTokenIfNeeded()
  }
}

我的通知有效负载设置为:

 ["to" : "fcm_token",
  "notification" : ["title" : "Message Title",
                    "body" : "Message Body",
                    "sound" : "default",
                    "badge" : 1]
 ]

然后我的应用程序委托如下:

class AppDelegate: UIResponder, UIApplicationDelegate, MessagingDelegate, UNUserNotificationCenterDelegate {

  var window: UIWindow?
  let notificationCenter = UNUserNotificationCenter.current()

  func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
      FirebaseApp.configure()
      let fcmManager = FCMManager()
      fcmManager.registerForPushNotifications()
      return true
  }

  func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
      if #available(iOS 14, *) {
          completionHandler([.banner])
      } else {
          completionHandler([.alert])
      }
  }
}

I am getting FCM messages pushed to my iOS device fine when in the background but not in the foreground. I have found a number of articles where others had the same issue and solved it by adding the willPresentNotification function. However this isn't solving the issue for me. My FCM code is as follows:

class FCMManager: NSObject, MessagingDelegate, UNUserNotificationCenterDelegate {

static let shared = FCMManager()
let database = Database.database().reference()

public func registerForPushNotifications() {
    UNUserNotificationCenter.current().delegate = self
    let authOptions: UNAuthorizationOptions = [.alert, .sound, .announcement, .badge, .carPlay, .providesAppNotificationSettings]
    UNUserNotificationCenter.current()
        .requestAuthorization( options: authOptions,
                               completionHandler: { granted, _ in
            guard granted else {
                print("Apple push notifications are not registered")
                return
            }
            print("Apple push notifications are registered")
        })
    Messaging.messaging().delegate = self
    DispatchQueue.main.async {
        UIApplication.shared.registerForRemoteNotifications()
    }
    updatePushTokenIfNeeded()
 }

 func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String?) {
     updatePushTokenIfNeeded()
  }
}

My notification payloads are set up as:

 ["to" : "fcm_token",
  "notification" : ["title" : "Message Title",
                    "body" : "Message Body",
                    "sound" : "default",
                    "badge" : 1]
 ]

Then my app delegate is as follows:

class AppDelegate: UIResponder, UIApplicationDelegate, MessagingDelegate, UNUserNotificationCenterDelegate {

  var window: UIWindow?
  let notificationCenter = UNUserNotificationCenter.current()

  func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
      FirebaseApp.configure()
      let fcmManager = FCMManager()
      fcmManager.registerForPushNotifications()
      return true
  }

  func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
      if #available(iOS 14, *) {
          completionHandler([.banner])
      } else {
          completionHandler([.alert])
      }
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文