2017-03-06 18 views
3

誰もがFirebaseとiOSの通知で何人かの問題を読んでいるが、誰もが同じ問題を抱えているようだ。Firebaseの通知(データのみ)がフォアグラウンドで受信されない

今私はFOREGROUNDアプリの状態で話している場合:

let notificationsParameters:[String:AnyObject] = [ 
     "to": "iphoneID", 
     "content-available":true, 
     "priority":"high", 
// "notification" : [ 
//    "body" : "great match!", 
//    "title" : "Portugal vs. Denmark", 
//    "icon" : "myicon" 
//   ], 
     "data" : [ 
      "Nick" : "Mario", 
      "Room" : "PortugalVSDenmark" 
     ] 
    ] 

が、私は、通知の一部を削除し、一度:

アプリケーションは、このようなexemple用パラメータ通知を持って今までに通知を受け取ります私は誰もが通知を受け取る必要があります参照してくださいフォアグランドで維持しても、アプリケーションは何も受信しません。

クローズド/バックグラウンドでもコンテンツにも利用可能であることを優先して追加しました。私が読んでいるのは、自分の問題を解決するためのものですが、そうではありません。

ここであなたが関与コード持っている:私はこれは私が受け取るsuppouseあるものであることを理解したよう(APPデリゲートIN)

APP DELEGATE

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { 
     // Override point for customization after application launch. 



     GMSServices.provideAPIKey(Constants.GoogleMaps.APIKey) 
     FIRApp.configure() 


     /* NOTFICATIONS */ 

     let notificationsType:UIUserNotificationType = [UIUserNotificationType.Alert, UIUserNotificationType.Badge, UIUserNotificationType.Sound] 
     let notificationSettings = UIUserNotificationSettings(forTypes: notificationsType, categories: nil) 
     application.registerForRemoteNotifications() 
     application.registerUserNotificationSettings(notificationSettings) 

     NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(self.tokenRefreshNotification(_:)), name: kFIRInstanceIDTokenRefreshNotification, object: nil) 


     return true 
    } 

NOTIFICATIONS

を通知のデータ

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], 
        fetchCompletionHandler completionHandler: (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) 
    } 

    func tokenRefreshNotification(notification:NSNotification) { 
     let refreshedToken = FIRInstanceID.instanceID().token() 
     print("InstanceID token: \(refreshedToken)") 

     connectToFCM() 
    } 

    func connectToFCM() { 
     FIRMessaging.messaging().connectWithCompletion { (error) in 
      if error != nil { 
       print("GMS ERROR: \(error)") 
      } 
      else { 
       print("Connected to GMS") 
      } 
     } 
    } 

CALLあなたはちょうど私が知っている必要がある任意の更なる情報

func sendNotification() { 

     let notificationsParameters:[String:AnyObject] = [ 
      "to": "iphoneID", 
      "content-available":true, 
      "priority":"high", 
      //   "notification" : [ 
      //    "body" : "great match!", 
      //    "title" : "Portugal vs. Denmark", 
      //    "icon" : "myicon" 
      //   ], 
      "data" : [ 
       "Nick" : "Mario", 
       "Room" : "PortugalVSDenmark" 
      ] 
     ] 


     let URL = NSURL(string: "https://fcm.googleapis.com/fcm/send")! 
     let URLRequest = NSMutableURLRequest(URL: URL) 

     URLRequest.HTTPMethod = "POST" 

     URLRequest.setValue("key=\(Constants.Firebase.NotificationsKey)", forHTTPHeaderField: "Authorization") 
     URLRequest.setValue("application/json", forHTTPHeaderField: "Content-Type") 

     let encoding = Alamofire.ParameterEncoding.JSON 

     let encoded = encoding.encode(URLRequest, parameters: (notificationsParameters)).0 
     Alamofire.request(encoded) 
    } 

FIREBASE通知に!

let notificationsParameters:[String:AnyObject] = [ 
      "to": "iphoneID", 
      "content-available":true, 
      "priority":"high", 
      "notification" : [ 
       "sound" : " " 
      ], 
      "data" : [ 
       "Nick" : "Mario", 
       "Room" : "PortugalVSDenmark" 
      ] 
     ] 

アプリが殺されているときは、通知を受信しませんが、あなたはデータのみを受け取ることができるようになります:私は、一つの解決策は次のように通知パラメータを設定しているのと同じ問題を抱えている人のために

+0

は、あなたは、これが解決持っていますか..? –

答えて

1

前景と背景にあるときの通知、幸運!

0

content_availableパラメータにハイフンではなくアンダースコアが必要です。 FCMを使用してデータのみ(サイレント)通知を行う場合は、notificationオブジェクトを使用する必要はありません。

Egは、あなたのFCM送信要求本体に

{ 
    "to": "iPhoneID", 
    "content_available": true, 
    "priority": "high", 
    "data": { 
     "nick": "mario", 
     "room": "PortugalVSDenmark" 
    } 
} 
関連する問題