簡體   English   中英

Firebase | 遠程通知未顯示,迅速3.0

[英]Firebase | Remote Notification does not show up, swift 3.0

我有一個問題,Firebase Remote通知沒有顯示。 我的通知在目標->功能中啟用,並且還安裝了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中顯示的內容:

在此處輸入圖片說明

如果您需要任何其他信息,只需詢問。

感謝您的幫助。

UPDATE(我添加了一些代碼):

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

我遇到了同樣的問題,我的解決方法是從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控制台中的狀態。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM