我正在根据设备功能调整工作设备,以便向主题发送推送通知。当我将推送发送到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()
}
答案 0 :(得分:0)
我终于找到了问题所在。无法按照我的想法在树中定义主题,而且一开始我就缺少/
,因此从"to" : "topics/Bologna/Shop-promotions"
更改为"to" : "/topics/Bologna-Shop-promotions"
解决了我的问题。我实际上是通过在订阅主题"Bologna/Shop-promotions"
时检查错误而发现并通过将其更改为"Bologna-Shop-promotions"
或"/topics/Bologna-Shop-promotions"
来修复的。希望这对其他人有帮助。