具有FCM令牌的设备的Firebase通知已发送但未收到

Rba*_*bar 6 ios firebase swift firebase-cloud-messaging firebase-notifications

我正在尝试使用FCM令牌从firebase通知控制台向特定设备发送简单的推送通知.firebase通知控制台将通知显示为已发送但设备未收到通知.我已经尝试发送通知,然后等待查看控制台是否记录didReceiveRemoteNotification,但通知需要花费太长时间(小时)才能显示为在firebase控制台中发送(即使我将优先级设置为high).

App代表

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)")
        }
Run Code Online (Sandbox Code Playgroud)

^我的猜测是该问题可能与"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)")
        }
Run Code Online (Sandbox Code Playgroud)

^我的猜测是该问题可能与"gcm_message_id"/"gcmMessageId"/"gcm.message_id"有关,因为我在下面尝试的三种方法中的每种方法都不同

        // Print full message.
        print(userInfo)

        completionHandler(UIBackgroundFetchResult.newData)
    }

    // End of [FCM Setup]
    ///////////////////////
}
Run Code Online (Sandbox Code Playgroud)

查看控制器

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 ?? "")")
      }
}
Run Code Online (Sandbox Code Playgroud)

权利和启用推送通知

我添加了推送权利 看这里 并启用推送通知的后台模式 看这里 并将GoogleService-Info.plist添加到我的项目中.

发送通知的方法

我正在从Firebase Notifications控制台创建通知(如下所示),因此通知本身的结构应该没有问题. 在此输入图像描述

我尝试了以下方法来解决问题,但所有方法都产生了相同的结果:

有谁知道为什么通知被标记为在firebase通知控制台中发送但未显示在设备上?

Lut*_*man 6

尝试将以下代码添加到 didRegisterForRemoteNotificationsWithDeviceToken func:

Messaging.messaging().apnsToken = deviceToken
Run Code Online (Sandbox Code Playgroud)

所以它看起来像这样:

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {

    Messaging.messaging().apnsToken = deviceToken
}
Run Code Online (Sandbox Code Playgroud)

这对我来说是工作。


Dou*_*oug 5

我在处理推送通知时使用的几个故障排除步骤如下:

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

我希望这可以帮助你,因为你工作得到了所有想通.

  • 感谢您的疑难解答提示!奇怪的是,从我的手机中删除应用程序然后再从XCode运行/安装解决了它.代码本身中没有任何内容必须更改,并且通知现在正如预期的那样工作.独自完成Quickstart项目也有助于防止我感觉好像我已经完全疯了,因为它按预期运行并且具有与我自己相同的代码(https://github.com/firebase/quickstart- IOS /斑点/主/消息/ MessagingExampleSwift/AppDelegate.swift).再次感谢您的提示,并希望答案/问题/评论能够帮助他人! (3认同)