【问题标题】:Firebase | Remote Notification does not show up, swift 3.0火力基地 |远程通知不显示,swift 3.0
【发布时间】:2016-11-02 19:20:23
【问题描述】:

我遇到了一个问题,即 Firebase 远程通知不显示。我的通知在 Target -> 功能中启用,并且还安装了 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 中显示的内容:

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

感谢您的帮助。

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

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()

【问题讨论】:

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


    【解决方案1】:

    我遇到了同样的问题,就我而言,解决方案是从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 控制台中的状态。

    【讨论】:

    • 添加了所有内容。它显示每个打开的“已连接到 FMC”。但是,如果我尝试发送通知,它仍然没有出现,并且 Firebase 没有响应(即时完成)你能帮我吗?谢谢大佬!
    • 您是否像我说的那样从 info.plist 中删除了该行?您确定在设置中允许通知吗?
    • FirebaseAppDelegateProxyEnabled 已删除,如您所说。通知在 Targets -> 'AppName' -> Capabilities -> Push Notifications 设置为 ON!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多