【发布时间】:2019-10-17 13:06:11
【问题描述】:
我正在使工作设备适应设备功能,以便向主题发送推送通知。当我向 FcmToken 发送推送时,它工作得很好,但是当我将它发送到一个主题时,我得到一个"error":"InvalidRegistration"error。此注册是否与服务器密钥相关,或者我缺少与请求一起发送的注册?
你能看出我错在哪里了吗?
一如既往,非常感谢。
这是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()
}
【问题讨论】:
标签: ios swift firebase-cloud-messaging