Firebase Cloud Message 通过 HTTP v1 api 使用主题函数返回 400

凯文

我试图用 fcm 测试主题函数。

这是错误消息在此处输入图片说明这是我的请求 json

在此处输入图片说明

这是我的服务器代码

 @Override
    public void sendTopic(TopicRequestVo requestVo) throws IOException {
            Gson gs = new Gson();
            GoogleTopicRequestVo googlereqVo = new GoogleTopicRequestVo();
            Payload payload = requestVo.getPayload();
            //读取token
            InputStream inputStream = new ClassPathResource("/static/server-account.json").getInputStream();
            GoogleCredential googleCredential = GoogleCredential
                    .fromStream(inputStream)
                    .createScoped(Arrays.asList("https://www.googleapis.com/auth/firebase.messaging"));
            if (googleCredential.refreshToken()) {
                System.out.println("refresh success");
            }
            System.out.println("refresh fail");
            System.out.println(googleCredential.getAccessToken());
            //设置通知消息
            googlereqVo.setNotification(new NotificationInfo(payload.getTitle(),payload.getBody()));
            //设置数据消息
    //        Map<String, String> data = requestVo.getData();
    //        googlereqVo.setData(data);
            googlereqVo.setTopic(requestVo.getTopic());
            GoogleV2RequestVo req = new GoogleV2RequestVo();
            req.setMessage(googlereqVo);
            try {
                HttpURLConnection conn = getConnection(Constant.API_URL_TOPIC_FCM, googleCredential.getAccessToken());
                OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream(), StandardCharsets.UTF_8);
                String s = gs.toJson(req);
                System.out.println(s);
                wr.write(s);
                wr.flush();
                BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                String line = null;
                while ((line = reader.readLine()) != null) {
                    System.out.println(line);
                }
                wr.close();
                reader.close();
            } catch (Exception e) {
                e.printStackTrace();
            }


     //API_URL_TOPIC_FCM = "https://fcm.googleapis.com/v1/projects/rlpushmsgdemo/messages:send HTTP/1.1";
     private HttpURLConnection getConnection(String path,String accesstoken)throws IOException{
            URL url = new URL(path);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setUseCaches(false);
            conn.setDoInput(true);
            conn.setDoOutput(true);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("connection", "Keep-Alive");
            conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            conn.setRequestProperty("Authorization", "Bearer " + accesstoken);
            conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
            return conn;
        }

我正在用谷歌的指南做类似的代码,我不知道哪里错了,它几乎困扰了我 1 小时,谁能告诉我原因,在此先感谢。

道格史蒂文森

您的 API_URL_TOPIC_FCM 值看起来不正确:

 https://fcm.googleapis.com/v1/projects/rlpushmsgdemo/messages:send HTTP/1.1

HTTP/1.1 不应该是 URL 的一部分。你可能是这个意思:

 https://fcm.googleapis.com/v1/projects/rlpushmsgdemo/messages:send

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何使用Firebase Cloud Messaging通过Web浏览器订阅主题

FCM HTTP V1 API返回404,以获取未注册的令牌

使用新的HTTP v1 API时,Firebase Data消息未传递到iOS

Google Cloud Logging:是否使用V2 API搜索v1条目?

AWS API Gateway应禁止使用TLS v1

Firebase Cloud Messaging(FCM)-HTTP V1 API或旧版HTTP API?

Firebase Cloud Messaging HTTP V1 API:如何通过REST调用获取Auth 2.0访问令牌?

如何在PHP中使用FCM HTTP v1 API

Azure API管理-如何使用Powershell通过版本控制架构“路径”和版本标识符“ v1”添加API

使用GCloud Emulator的Google Cloud PubSub V1

Firebase Cloud Functions通过API密钥保护HTTPS端点

通过HTTP触发的Firebase Cloud Function执行两次

C#Firebase Cloud Message使用主题消息发送多个Web JS通知

部署时,firebase函数INVALID_ARGUMENT HTTP错误400

FireBase sendMessage函数更新到v1 Google Cloud Endpoint

如何通过Firebase Cloud Functions在本地运行Express API?

访问https://api.paypal.com/v1/payments/payment/时获得Http响应代码400

为使用Cloud Endpoints v1的Cloud Run启用CORS

Google Sheets API返回HTTP错误400

Google Map无法在设备上使用api v1

Google Map api“路线”返回的HTTP响应代码:400

结合使用woocommerce rest api v1和http和javascript(非https)

HTTP发布请求:错误400,Firebase主题消息传递

Firebase HTTP 请求的 Cloud Functions 函数

通过 Google Cloud API 区分云函数的 HTTP 和 onCall Trigger

通过 Firebase Cloud 函数调用第三方 API(使用 Blaze 计划)。反应/还原

带有可选数据的 Android FireBase 通知,使用 V1 API,不适用于后台

Firebase 云消息传递 HTTP v1 发送错误 - 400 客户端错误:对 url 的错误请求:https://fcm.googleapis.com/v1/projects/<PROJECT_ID>/messages:send

使用 stripe.redirectToCheckout() 时不断获取“https://api.stripe.com/v1/payment_pages 400”