Firebase FCM推送,错误注册失败| Android的

Xco*_*OOB 5 android content-type google-api firebase firebase-cloud-messaging

我之前已经看过这个话题,但我认为这不是同一个场景.我正在尝试通过FCM(Firebase云消息传递)向所有其他设备发送来自一台设备(将成为管理设备)的推送通知,而我正准备通过他们的文档.我试图订阅主题或保持简单仍然得到相同的错误,"MissingRegistration".

 String jsonData = "{\"to\":\"/topics/news\",\"notification\":{\"title\":\"title\",\"body\":\"text\" }}";
                byte[] postData = jsonData.getBytes(Charset.forName("UTF-8"));
                int postDataLength = postData.length;

                URL url = new URL("https://fcm.googleapis.com/fcm/send");
                HttpURLConnection con = (HttpURLConnection) url.openConnection();
                con.setDoInput(true);
                con.setDoOutput(true);
                con.setInstanceFollowRedirects(true);
                con.setRequestMethod("POST");

                con.setRequestProperty("Content-Type","application/json");
                con.setRequestProperty("Authorization","key=AIzaSyB70J***-z34q2_h*******qjZsM5zBIf8Y"); //I've added stars :-) 
                con.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
                con.setRequestProperty("Content-Type","charset=UTF-8");
                con.setRequestProperty("Content-Length",Integer.toString(postDataLength));

                con.setUseCaches(false);

                DataOutputStream wr = new DataOutputStream(con.getOutputStream());
                wr.write(postData);

                InputStream inputStream= con.getInputStream();
                BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
                String line = null;
                String outPut = "";
                while (((line = reader.readLine()) != null)){
                    outPut += line;
                }
Run Code Online (Sandbox Code Playgroud)

Ran*_*man 11

我正在使用/尝试使用 Postman Web API 客户端应用程序向 FCM URL 发送 FCM 推送通知:https : //fcm.googleapis.com/fcm/send

我用错了Content-Type: application/x-www-form-urlencoded,所以我把它改成了

Content-Type:  application/json
Run Code Online (Sandbox Code Playgroud)

这基本上是必需的,因为我们将推送通知作为 JSON 有效负载发送。

干杯!


小智 8

我也有同样的错误,但我的是在服务器端(从rails推送到android),但我的问题是我忘了在标题中指定内容类型(我使用RestClient发布到firebase).希望它可以帮助某人

RestClient.post( "https://fcm.googleapis.com/fcm/send",
             {:to => reg_id,:notification => options }.to_json,{:content_type => :json, :accept => :json,:Authorization => "key=*****"}
Run Code Online (Sandbox Code Playgroud)