Firebase FCM 推送通知停止工作 iOS 11.1.1

Firebase FCM push notifications stopped working iOS 11.1.1

我正在使用 Firebase FCM 从 iOS 设备发送推送通知。推送通知确实有效,直到昨天。

当我现在发送推送通知时,一切都显示成功,但设备上什么也没有收到。

如果我直接通过 curl 请求发送,这是响应:

{"multicast_id":7815294000653973158,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1510474219556035%ca9ff0f8ca9ff0f8"}]}

如果我从 firebase 控制台上的通知仪表板发送,它会显示已成功完成。

我做了以下但没有成功:

  1. 已锁定 'FirebaseInstanceID'、“2.0.0”的广告连播,根据
  2. 在开发人员控制台上生成了一个新的 APN 密钥并替换了 Firebase 中 FCM 设置中的现有密钥
  3. 下载了新的 GoogleService-Info.plist 并替换了现有的
  4. 检查包 ID 等是否全部匹配
  5. 将 firebase pods 更新为最新:

Using Firebase (4.5.0) Using FirebaseAnalytics (4.0.4) Using FirebaseAuth (4.3.1) Using FirebaseCore (4.0.10) Using FirebaseFirestore (0.9.1) Using FirebaseInstanceID (2.0.5) Using FirebaseMessaging (2.0.6)

  1. 打开和关闭消息混合
  2. 根据设置Messaging.messaging().shouldEstablishDirectChannel = true
  3. 已从 firebase 控制台删除此 iOS 应用并从头开始重新设置通知
  4. 确保功能中的远程通知仍然打开
  5. 重新启动了我的设备

我的设置: 我在覆盖 init() 方法的 AppDelegate 文件中调用 FirebaseApp.configure():

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

在 AppDelegate 中 didFinishLaunchingWithOptions:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    // get push notification token id for user
    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()

    return true
}

然后我将我的令牌保存在 userdefaults 中,稍后保存到数据库:

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    if let refreshedToken = InstanceID.instanceID().token() {
        defaults.set(refreshedToken, forKey: Constant.UserDefaults.token)
        print("Token generated: ", refreshedToken)
    } else {
        print("Could not save token becuase error with instance id token")
    }
}

这里生成的token我也是用上面提到的curl测试,都显示成功。

我的 GoogleService-Info.plist:

我的Info.plist:

如果需要任何其他信息,请告诉我。

我的 Android 应用程序仍然像以前一样工作。我正在努力了解 iOS 应用程序可能会在一天内发生什么变化,或者我可能在一天内破坏了什么:)

如有任何帮助,我们将不胜感激。

好的,已修复。我通过将此添加到我的 Info.plist 来禁用 firebase 的消息混合:FirebaseAppDelegateProxyEnabled: NO

此外,我删除了所有 firebase 消息传递委托方法。并在 didRegisterForRemoteNotificationsWithDeviceToken 中生成我自己的 APN 令牌,并在生成令牌后在 didRegisterForRemoteNotificationsWithDeviceToken 方法中设置 Messaging.messaging().apnsToken = deviceToken。

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    if let refreshedToken = InstanceID.instanceID().token() {
        defaults.set(refreshedToken, forKey: Constant.UserDefaults.token)

        Messaging.messaging().apnsToken = deviceToken

        print("Token generated: ", refreshedToken)
    } else {
        print("Could not save token becuase error with instance id token")
    }
}

之前我使用了 firebase swizzling,它似乎由于某种原因停止工作了。

因为这对我来说是一次艰难的经历,我想post我现在推荐的步骤来为 FCM 启用 iOS 客户端:

  1. 在 Apple Developer 控制台中生成您的 APN,然后生成 select 个 APN。 Select 继续并下载您的 APN 证书。记下您的密钥 ID。

然后在 firebase 控制台的设置下,云消息,上传你的 APN 密钥,添加密钥 ID 和 bundle id,不要上传任何 p12 证书。

  1. 通过将此添加到您的 Info.plist:
  2. 来禁用 firebase 消息混合

FirebaseAppDelegateProxyEnabled: NO

  1. 然后在您的 AppDelegate.swift 文件中的 didFinishLaunchingWithOptions 方法中添加以下内容:

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    // [START set_messaging_delegate]
    Messaging.messaging().delegate = self
    // [END set_messaging_delegate]
    
    // get push notification token id for user
    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()
    
    return true
    

    }

由于 swizzling 被禁用,您需要注意生成 APN 令牌并将其分配给 firebase 消息传递 apnsToken。您可以通过在 AppDelegate.swift 文件中使用以下方法来执行此操作:

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    if let refreshedToken = InstanceID.instanceID().token() {
        defaults.set(refreshedToken, forKey: Constant.UserDefaults.token)

        Messaging.messaging().apnsToken = deviceToken

        print("Token generated: ", refreshedToken)
    } else {
        print("Could not save token becuase error with instance id token")
    }
}

然后我将令牌保存在 userdefaults 中,以便稍后保存到数据库中,见上文:

defaults.set(refreshedToken, forKey: Constant.UserDefaults.token)

您可以通过复制在控制台中打印出的令牌并使用 FCM 消息控制台来测试您的令牌是否正常工作。或者通过在终端中使用 curl 请求,如下所示。请注意,您必须将 YOUR_LEGACY_SERVER_KEY 替换为旧版服务器密钥,您可以在设置和云消息传递下的 firebase 控制台中找到它,并将 YOUR_TOKEN 替换为控制台中打印的令牌(上面生成的):

curl -X "POST" "https://fcm.googleapis.com/fcm/send" \
 -H "Authorization: key=YOUR_LEGACY_SERVER_KEY” \
 -H "Content-Type: application/json" \
 -d $'{
"notification": {
"body": "Testing with direct FCM API",
"title": "Test Message",
"badge": "0",
"sound": "default"
},
"registration_ids": [YOUR_TOKEN]
}'