我正在尝试使用FCM token 将简单的推送通知从Firebase通知控制台发送到特定设备。 Firebase通知控制台将通知显示为已发送,但设备未收到该通知。我尝试发送通知,然后等待查看控制台是否从didReceiveRemoteNotification日志,但是通知花费的时间(小时)太长,无法显示为在Firebase控制台中发送(即使将优先级设置为high)。

应用程序委托(delegate)

import UIKit
import Firebase
import FirebaseStorage
import FirebaseDatabase
import FirebaseMessaging
import CoreData
import UserNotifications

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.

        // Use Firebase library to configure APIs
        FirebaseApp.configure()

        /////
        // For Firebase Cloud Messaging (FCM)
        if #available(iOS 10.0, *) {
            // For iOS 10 display notification (sent via APNS)
            UNUserNotificationCenter.current().delegate = self
            let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
            UNUserNotificationCenter.current().requestAuthorization(
                options: authOptions,
                completionHandler: {_, _ in })
        } else {
            let settings: UIUserNotificationSettings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
            application.registerUserNotificationSettings(settings)
        }
        application.registerForRemoteNotifications()
        // End of [for Firebase Cloud Messaging (FCM)]
        /////

        return true
    }

    ///////////////////////
    // FCM Setup

    // Monitor token generation for FCM: Be notified whenever the FCM token is updated
    func messaging(_ messaging: Messaging, didRefreshRegistrationToken fcmToken: String) {
        print("Firebase registration token: \(fcmToken)")
    }

    // Monitor token generation for FCM:
    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        Messaging.messaging().apnsToken = deviceToken
    }    // Handle messages received through the FCM APNs interface
    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
        print("didReceiveRemoteNotification")
        // If you are receiving a notification message while your app is in the background,
        // this callback will not be fired till the user taps on the notification launching the application.
        // TODO: Handle data of notification

        // With swizzling disabled you must let Messaging know about the message, for Analytics
        // Messaging.messaging().appDidReceiveMessage(userInfo)

        // Print message ID.
        // gcm_message_id
        if let messageID = userInfo["gcmMessageIDKey"] {
            print("Message ID: \(messageID)")
        }

^我的猜测是,该问题可能与“gcm_message_id”/“gcmMessageId”/“gcm.message_id”有关,因为在我下面尝试的三种方法中,每种方法都不相同
        // Print full message.
        print(userInfo)
    }

    // Handle messages received through the FCM APNs interface
    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
        print("didReceiveRemoteNotification (withCompletionHandeler)")
        // If you are receiving a notification message while your app is in the background,
        // this callback will not be fired till the user taps on the notification launching the application.
        // TODO: Handle data of notification

        // With swizzling disabled you must let Messaging know about the message, for Analytics
        // Messaging.messaging().appDidReceiveMessage(userInfo)

        // Print message ID.
        if let messageID = userInfo["gcmMessageIDKey"] {
            print("Message ID: \(messageID)")
        }

^我的猜测是,该问题可能与“gcm_message_id”/“gcmMessageId”/“gcm.message_id”有关,因为在我下面尝试的三种方法中,每种方法都不相同
        // Print full message.
        print(userInfo)

        completionHandler(UIBackgroundFetchResult.newData)
    }

    // End of [FCM Setup]
    ///////////////////////
}

View Controller
class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Retrieve the current registration token for Firebase Cloud Messaging (FCM)
        let token = Messaging.messaging().fcmToken
        print("FCM token: \(token ?? "")")
      }
}

权利和启用推送通知

我已经添加了推送权利ios - 带有FCM token 的设备的Firebase通知已发送但未收到-LMLPHP并为推送通知ios - 带有FCM token 的设备的Firebase通知已发送但未收到-LMLPHP启用了后台模式,并将GoogleService-Info.plist添加到了我的项目中。

发送通知的方法

我正在从Firebase通知控制台创建通知(如下所示),因此通知本身的结构应该没有问题。
ios - 带有FCM token 的设备的Firebase通知已发送但未收到-LMLPHP

我尝试了以下方法来解决此问题,但是所有方法都产生了相同的结果:
  • Google Firebase Documentation
  • Remote Notifications w/ firebase tutorial
  • Google Firebase Quickstart

  • 有人知道为什么将通知在Firebase通知控制台中标记为已发送但未显示在设备上吗?

    最佳答案

    在使用推送通知时,我使用了一些故障排除步骤:

  • 首先让推送工作独立于Firebase服务。我用这个tool.
  • 确保您没有任何捆绑包标识符 namespace 冲突。因此,例如,在设备上具有appstore构建,testflight构建和/或开发应用的构建的任何组合。删除该应用程序的一个实例之外的所有实例。捆绑包标识符是您的设备如何知道将推送通知路由到哪个应用程序的方式。
  • 当其他所有方法都失败时,我尝试通过构建一个新的示例项目并将其连接到一个新的firebase项目来隔离问题,并查看是否可以将重点缩小到仅能够在不使用任何其他业务逻辑的情况下进行推送工作我的应用程序。这可以帮助我向自己证明自己并没有发疯,并且可以证明这不是导致困境的神秘网络条件。

  • 希望这对您有所帮助,对您有所帮助。

    10-05 20:41
    查看更多