我正在使工作设备适应设备功能,以便向主题发送推送通知。当我向 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"
来修复它。希望这对其他人有所帮助。