向FCM POST快速发送报文时出现"error":"InvalidRegistration"错误 [英] "error":"InvalidRegistration" error while sending message to FCM post swift

查看:42
本文介绍了向FCM POST快速发送报文时出现"error":"InvalidRegistration"错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

这篇关于向FCM POST快速发送报文时出现&quot;error&quot;:&quot;InvalidRegistration&quot;错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆