2017-03-06 68 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" 
     ] 
    ] 

但一旦我刪除了部分的通知應用程序沒有收到任何東西,即使保持在前景,因爲我看到每個人都應該收到通知。

我的優先級高,甚至封閉/背景和內容可用,我讀的是suppoused解決我的問題,但事實並非如此。

在這裏,你有參與代碼:

應用程序委託

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 
    } 

通知(IN APP代表)

據我瞭解這是什麼suppouse,我將接受通知的數據

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 TO火力NOTIFICATIONS

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

任何進一步的信息,你只需要告訴我!

+0

你有這個決心..? –

回答

1

對於那些誰有我,一個解決方法是設置通知參數這樣同樣的問題:

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

您將不會收到通知時,應用程序被殺害,但你將只能接收數據在前臺和後臺通知,祝你好運!

0

你在正確的軌道上,但在content_available參數中需要一個下劃線而不是連字符。如果要使用FCM進行僅數據(靜默)通知,則無需使用notification對象。

例如,在您的FCM發送請求體:

{ 
    "to": "iPhoneID", 
    "content_available": true, 
    "priority": "high", 
    "data": { 
     "nick": "mario", 
     "room": "PortugalVSDenmark" 
    } 
}