嗨,我已閱讀大家關於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)
}
任何進一步的信息,你只需要告訴我!
你有這個決心..? –