ios - 火力基地 |远程通知不显示,swift 3.0

标签 ios swift firebase firebase-cloud-messaging firebase-notifications

我遇到了 Firebase 远程通知不显示的问题。我的通知已在“目标”->“功能”中启用,并且还安装了 Firebase。在 Firebase 网站上,当我尝试发送通知时,它会立即关闭。收到 0 台设备。

这是我的代码:

    import UIKit
    import UserNotifications

    import Siren

    import Firebase
    import FirebaseDatabase
    import FirebaseInstanceID
    import FirebaseMessaging

    @UIApplicationMain
    class AppDelegate: UIResponder, UIApplicationDelegate {

        var window: UIWindow?

        override init() {
            super.init()
            FIRApp.configure()
            FIRDatabase.database().persistenceEnabled = true
        }

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

    //
    let notificationSettings = UIUserNotificationSettings(types: [.badge, .alert, .sound], categories: nil)

    UIApplication.shared.registerUserNotificationSettings(notificationSettings)

    application.registerForRemoteNotifications()
    application.registerUserNotificationSettings(notificationSettings)


    return true
}

这就是 Firebase 中显示的内容:

enter image description here

如果您需要任何其他信息,请询问。

感谢您的帮助。

更新(我添加了一些代码):

应用程序委托(delegate):

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
                     fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
        // 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
        // Print message ID.
        print("Message ID: \(userInfo["gcm.message_id"]!)")
        // Print full message.
        print("%@", userInfo)
    }
    // [END receive_message]
    // [START refresh_token]
    func tokenRefreshNotification(_ notification: Notification) {
        if let refreshedToken = FIRInstanceID.instanceID().token() {
            print("InstanceID token: \(refreshedToken)")
        }
        // Connect to FCM since connection may have failed when attempted before having a token.
        connectToFcm()
    }

didFinishLaunchingWithOptions:

// Add observer for InstanceID token refresh callback.
        NotificationCenter.default.addObserver(self,
                                               selector: #selector(self.tokenRefreshNotification),
                                               name: .firInstanceIDTokenRefresh,
                                               object: nil)

其他代码:

// [START connect_to_fcm]
    func connectToFcm() {
        FIRMessaging.messaging().connect { (error) in
            if (error != nil) {
                print("Unable to connect with FCM. \(error)")
            } else {
                print("Connected to FCM.")
            }
        }
    }
    // [END connect_to_fcm]
    func applicationDidBecomeActive(_ application: UIApplication) {
        connectToFcm()
    }
// [START disconnect_from_fcm]
func applicationDidEnterBackground(_ application: UIApplication) {
    FIRMessaging.messaging().disconnect()

最佳答案

我遇到了同样的问题,就我而言,解决方案是从 info.plist 文件中删除 FirebaseAppDelegateProxyEnabled 行。但是,我也没有看到您正在使用 didReceiveRemoteNotification 和观察者。

将此也添加到您的 appDelegate 中:

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
                     fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
        // 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
        // Print message ID.
        print("Message ID: \(userInfo["gcm.message_id"]!)")
        // Print full message.
        print("%@", userInfo)
    }
    // [END receive_message]
    // [START refresh_token]
    func tokenRefreshNotification(_ notification: Notification) {
        if let refreshedToken = FIRInstanceID.instanceID().token() {
            print("InstanceID token: \(refreshedToken)")
        }
        // Connect to FCM since connection may have failed when attempted before having a token.
        connectToFcm()
    }

而且我也没有在 didFinishLaunchingWithOptions 中看到观察者:

// Add observer for InstanceID token refresh callback.
        NotificationCenter.default.addObserver(self,
                                               selector: #selector(self.tokenRefreshNotification),
                                               name: .firInstanceIDTokenRefresh,
                                               object: nil)

您是否也可以对此进行测试,以便查看用户是否连接到云消息传递:

// [START connect_to_fcm]
    func connectToFcm() {
        FIRMessaging.messaging().connect { (error) in
            if (error != nil) {
                print("Unable to connect with FCM. \(error)")
            } else {
                print("Connected to FCM.")
            }
        }
    }
    // [END connect_to_fcm]
    func applicationDidBecomeActive(_ application: UIApplication) {
        connectToFcm()
    }
// [START disconnect_from_fcm]
func applicationDidEnterBackground(_ application: UIApplication) {
    FIRMessaging.messaging().disconnect()

此外,您能否说明发送通知后 Firebase 控制台中的状态是什么。

关于ios - 火力基地 |远程通知不显示,swift 3.0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40388062/

相关文章:

ios - 推送通知问题 iOS TestFlight

ios - Swift iOS 13,使用移动网络(4g/3g)时未获取 APNS 设备 token

java - 从 Firebase 下载文件 |应用程序可以在 Debug模式下运行,但不能在 Release模式下运行

ios - Xcode 不会添加捆绑资源

ios - 无法更改UINavigationBar提示颜色和字体

ios - 为 'Slide' iOS UIView 设置动画但关联的 subview 不移动

swift - 在使用断点 Swift 时查找变量的值

firebase - Cloud Firestore上一组对象的数组vs. map vs.子集合

javascript - 使用基于文档的数据存储实现复杂搜索的最佳方法

javascript - 插入音频元素会导致 CSS 错误