0

Firebaseリモート通知が表示されないという問題があります。私の通知はTarget - > capabilitiesで有効になり、Firebaseもインストールされます。 Firebaseのウェブサイトで、通知を送信しようとすると、即座に終了します。 0デバイスを受信しました。Firebase |リモート通知が表示されない、スウィフト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 
} 

そして、それはそれはFirebaseに示したものです:

enter image description here

あなたが他の情報が必要な場合は、単に尋ねます。

ありがとうございます。私は同じ問題aを持っていた

// [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

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) 

他のコード

UPDATEは(私はいくつかのコードを追加しました)私の場合、解決策は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フィニッシュ)助けてくれますか?ありがとう! – user6083214

+0

私が言ったようにinfo.plistから行を削除しましたか?設定で通知を受け入れてもよろしいですか? –

+0

FirebaseAppDelegateProxyEnabledが削除されました。通知がターゲットにあります - > 'AppName' - >機能 - >プッシュ通知がオンに設定されています! – user6083214

関連する問題