【问题标题】:Firebase Messages in Swift not showing up when application isn't active当应用程序不活动时,Swift 中的 Firebase 消息不显示
【发布时间】:2017-09-14 04:15:31
【问题描述】:

我正在尝试在我的应用程序中实现 firebase 消息传递,但我只在应用程序处于活动状态时收到通知(我可以在控制台中看到它们,但只有当应用程序处于活动状态时。如果它关闭,它们在应用再次激活之前不会出现)。

我打开了后台通知,并将我的 p12 证书加载到了 firebase 设置中。

这是我的应用委托中的代码:

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

    Fabric.with([Crashlytics.self])

    FirebaseApp.configure()


    Messaging.messaging().delegate = self
    Messaging.messaging().shouldEstablishDirectChannel = true

    if #available(iOS 11.0, *){
        print("IOS 11")
        UNUserNotificationCenter.current().delegate = self

        let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
        UNUserNotificationCenter.current().requestAuthorization(
            options: authOptions,
            completionHandler: {_, _ in })
    }else 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
}

 func messaging(_ messaging: Messaging, didRefreshRegistrationToken fcmToken: String) {
    print("Firebase registration token: \(fcmToken)")
}

@nonobjc func application(application: UIApplication,
                 didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
    Messaging.messaging().apnsToken = deviceToken as Data
}

func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {
    // Convert to pretty-print JSON
    guard let data =
        try? JSONSerialization.data(withJSONObject: remoteMessage.appData, options: .prettyPrinted),
        let prettyPrinted = String(data: data, encoding: .utf8) else {
            return
    }
    print("Received direct channel message:\n\(prettyPrinted)")
}

func application(received remoteMessage: MessagingRemoteMessage){

    print("Received Remote Message")
    guard let data =
        try? JSONSerialization.data(withJSONObject: remoteMessage.appData, options: .prettyPrinted),
        let prettyPrinted = String(data: data, encoding: .utf8) else {
            return
    }
    print("Received direct channel message:\n\(prettyPrinted)")

}

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
    print(userInfo)
}

编辑 1: 这是我在控制台中收到的示例通知

{
  "from" : "480981200252",
  "notification" : {
    "e" : "1",
    "body" : "hey"
  },
  "collapse_key" : "com.amplesftwr.thelifeofcarl"
}

编辑 2: 这是我的背景模式设置的照片:

【问题讨论】:

  • 只是为了确定,这适用于 iOS 10,但不适用于 11?
  • 我不确定它是否适用于 10,我正在使用 11。
  • 更新:在 ios 10 上的结果相同
  • 您能截取您启用的背景模式的屏幕截图吗?您的应用在前台正确接收,因此您不能在与后台相关的设置中设置正确的内容。
  • 同时检查您是否为您的应用/设备“允许”通知

标签: ios swift firebase firebase-cloud-messaging


【解决方案1】:

在您的项目设置中的功能下,确保不仅打开了“推送通知”,而且还打开了“后台模式”。

在您的 .plist 文件中,您还应该有一个“必需的背景模式”键,并且在您的项目中确保您也有 .entitlements 文件。

您可能要检查的另一件事是,当您转到 Firebase 控制台时,请尝试使用此选项将消息发送给应用程序的所有用户,而不是仅向该特定用户的令牌发送通知Firebase 控制台。

如果它有效,那么您的问题可能与 FCM 令牌有关。

如果它仍然不起作用,那么您可能需要检查您的钥匙链和您 Apple 开发者帐户上的所有证书,以确保一切都是绿色/正常并且没有重复!

不幸的是,我无法评论您的帖子,所以我将其添加在这里作为答案,希望它可以帮助您解决问题,但如果没有,请告诉我,我会尽力而为 :)

【讨论】:

  • 亚历克斯,感谢您的所有建议。推送通知已打开。 plist 需要后台模式,我已经在推送整个应用程序,而不是单个用户。可悲的是,我在发布之前已经尝试了所有这些事情:/
  • 更新:我什至尝试撤销证书,然后重新添加它(也添加到 firebase),但仍然是同样的问题
  • 感谢本杰明的更新!明天早上我会回复你,也许睡个好觉后事情会更清楚:)
  • 顺便问一下,您是发送通知到真机还是模拟器?
  • 仍然没有运气,即使睡了一夜好觉。这是一个真实的设备。目前我已经尝试了几种设备
猜你喜欢
  • 1970-01-01
  • 2018-05-29
  • 1970-01-01
  • 1970-01-01
  • 2017-07-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多