0

我有一個Firebase Remote通知未顯示的問題。我的通知已在Target - >功能中啓用,並且Firebase也已安裝。在Firebase網站上,當我嘗試發送通知時,它會立即關閉。收到0個設備。Firebase |遠程通知不會顯示,swift 3.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 
} 

而這正是它表明在火力地堡:

enter image description here

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

感謝您的幫助。

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

回答

0

我有同樣的問題在我的情況下,解決方案是從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控制檯中。

+0

添加了所有內容。它說每一個開放的「連接到FMC」。但是如果我嘗試發送通知,它仍然不會顯示,並且Firebase沒有響應(Insant finish)您能幫助我嗎?謝啦! – user6083214

+0

您是否像我說的那樣從info.plist中刪除了該行?你確定你的設置允許通知嗎? –

+0

FirebaseAppDelegateProxyEnabled已被刪除,就像你說的。通知位於目標 - >'AppName' - >功能 - >推送通知設置爲ON! – user6083214

相關問題