我试图用 fcm 测试主题函数。
这是我的服务器代码
@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] 删除。
我来说两句