[英]"error": "InvalidRegistration" while sending push notifications
[英]“error”:“InvalidRegistration” error while sending message to FCM post swift
我正在調整工作設備到設備功能,以便向主題發送推送通知。 當我向FcmToken發送推送時,它工作得很好,但當我將它發送到某個主題時,我收到"error":"InvalidRegistration"
錯誤。 此注冊是否與服務器密鑰相關,或者我缺少哪些注冊與請求一起發送? 你能看出我在哪里錯了嗎? 一如既往地多謝。
這是fcmToken
的工作函數:
static func sendPushNotification(to receiverToken: String, title: String, subtitle: String, body: String) {
let serverKey = firebaseServerKey // AAAA8c3j2...
// let topic = "/topics/<your topic here>" // replace it with partnerToken if you want to send a topic
let url = NSURL(string: "https://fcm.googleapis.com/fcm/send")
let postParams: [String : Any] = [
"to": receiverToken,
"notification": [
// "badge" : 1, sendig the badge number, will cause aglitch
"body": body,
"title": title,
"subtitle": subtitle,
"sound" : true, // or specify audio name to play
"content_available": true, // this will call didReceiveRemoteNotification in receiving app, else won't work
"priority": "high"
// "click_action" : "🚀", // action when user click notification (categoryIdentifier)
],
"data" : [
"data": "ciao",
]
]
let request = NSMutableURLRequest(url: url! as URL)
request.httpMethod = "POST"
request.setValue("key=\(serverKey)", forHTTPHeaderField: "Authorization")
request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
do {
// request.httpBody = try JSONSerialization.data(withJSONObject: postParams, options: JSONSerialization.WritingOptions())
request.httpBody = try JSONSerialization.data(withJSONObject: postParams, options: [.prettyPrinted]) // working
print("My paramaters: \(postParams)")
} catch {
print("Caught an error: \(error)")
}
let task = URLSession.shared.dataTask(with: request as URLRequest) { (data, response, error) in
if let realResponse = response as? HTTPURLResponse {
if realResponse.statusCode != 200 {
print("Not a 200 response")
}
}
if let postString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue) as String? {
print("POST: \(postString)")
}
}
task.resume()
}
這是主題的一個:
static func sendTopicPushNotification(to topic: String, title: String, subtitle: String, body: String) {
let serverKey = firebaseServerKey // AAAA8c3j2...
// let topic = "/topics/<your topic here>" // replace it with partnerToken if you want to send a topic
let url = NSURL(string: "https://fcm.googleapis.com/fcm/send")
let postParams: [String : Any] = [
"priority": "high",
"notification": [
// "badge" : 1, sendig the badge number, will cause aglitch
"body": body,
"title": title,
"subtitle": subtitle,
"text": "some text",
"sound" : true, // or specify audio name to play
// "click_action" : "🚀", // action when user click notification (categoryIdentifier)
],
"to" : "topics/Bologna/Shop-promotions"
// "data" : [
//// "data": "ciao",
// "body": body,
// "title": title,
// "subtitle": subtitle,
// ]
]
let request = NSMutableURLRequest(url: url! as URL)
// request.httpMethod = "POST" // error: POST: {"error":"InvalidParameters: Bad topic or filter provided"}
request.httpMethod = "POST"
request.setValue("key=\(serverKey)", forHTTPHeaderField: "Authorization")
request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
do {
// request.httpBody = try JSONSerialization.data(withJSONObject: postParams, options: JSONSerialization.WritingOptions())
request.httpBody = try JSONSerialization.data(withJSONObject: postParams, options: [.prettyPrinted]) // working
print("sendTopicPushNotification : My paramaters: \(postParams)")
} catch {
print("sendTopicPushNotification : Caught an error: \(error)")
}
let task = URLSession.shared.dataTask(with: request as URLRequest) { (data, response, error) in
if let realResponse = response as? HTTPURLResponse {
if realResponse.statusCode != 200 {
print("sendTopicPushNotification : Not a 200 response : \(realResponse)")
}
print("sendTopicPushNotification : response : \(realResponse)")
}
if let postString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue) as String? {
print("sendTopicPushNotification : POST: \(postString)")
}
}
task.resume()
}
我終於找到了問題所在。 主題不能像我想象的那樣在樹中定義,而且我在開始時缺少/
從"to" : "topics/Bologna/Shop-promotions"
到"to" : "/topics/Bologna-Shop-promotions"
解決了我的問題。 我實際上通過在訂閱主題"Bologna/Shop-promotions"
檢查錯誤並通過將其更改為"Bologna-Shop-promotions"
或"/topics/Bologna-Shop-promotions"
來修復它。 希望這會有助於他人。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.