Nic*_*rbo 5 objective-c firebase swift firebase-cloud-messaging
I've implemented Firebase FCM for push notifications in my app, and it works great - except from, as my topic describes, it gives me this response aprox 6/10 times:
{
error = InternalServerError;
}
)
, "multicast_id": 8370179651570531666, "failure": 1, "canonical_ids": 0, "success": 0]
Run Code Online (Sandbox Code Playgroud)
The rest of the times, it delivers the notification as expected!
I'm sending my notifications from my app, through https://fcm.googleapis.com/fcm/send as a POST-request like this:
func sendFcmNotificationToUser(title: String, message: String, to: String, sound: Bool, badge: Bool) {
// Prepare json data
let mapDict = ["title": title, "body": message, "badge": 1, "sound": "default"]
let json = ["priority": "high", "notification": mapDict, "to": to]
do {
let jsonData = try NSJSONSerialization.dataWithJSONObject(json, options: .PrettyPrinted)
// create post request
let url = NSURL(string: "https://fcm.googleapis.com/fcm/send")!
let request = NSMutableURLRequest(URL: url)
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("key=xxxxxxxxxxxxx", forHTTPHeaderField: "Authorization")
request.HTTPMethod = "POST"
request.HTTPBody = jsonData
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) { data, response, error in
if error != nil {
print("__111 \(error!.description)")
return
}
do {
if let responseJSON = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as? [String: AnyObject] {
print("__222 \(responseJSON)")
}
} catch {
print("Failed2222")
}
}
task.resume()
} catch {
print("Failed11111")
}
}
Run Code Online (Sandbox Code Playgroud)
I have googled around, but haven't been able to find a solution to this - so my guess is, that it's a Firebase problem. Have anyone of you experienced the same?
I guess that my solution will be to check the response for this error, and if it occurs, I'll send the request again, until it succeeds.. Also maybe add a timer, to add a little delay between my requests. Does that seem like a legit solution to you guys?
Otherwise I don't really know what to do :-)
Just some bonus info: When I send messages/notifications from the FCM console, it also only delivers sometimes.. But if I send from an APNs tester directly from my Mac, it delivers every time, so my APNS certificate seems to work properly!
Have a lovely day :-)
| 归档时间: |
|
| 查看次数: |
464 次 |
| 最近记录: |