我第一次使用Firebase,对云消息传递的使用感到有些困惑。
环境:
我有一个Spring Microservice,应该通过其令牌向应用程序发送消息(数据而不是通知)。 (与平台无关) 作为协议,我想使用推荐的Http v1。
我创建了一个Firebase帐户,并添加了一个测试项目,现在我想在那里添加数据。 据我所知,没有办法看到消息已推送进行调试吗?
我没有App来测试流程,因为它将由外部开发。
当前行为:
I just want to create a first test message. Therefor I use the following code.
// myProject from settings- general->ProjectID
String FIREBASE_API_URL = "https://fcm.googleapis.com/v1/projects/myProject/messages:send";
// From settings->cloud-messaging
String FIREBASE_SERVER_KEY = "newServerKey";
RestTemplate restTemplate = new RestTemplate();
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.set("Authorization", "Bearer" + FIREBASE_SERVER_KEY);
httpHeaders.set("Content-Type", "application/json");
JSONObject json = new JSONObject();
JSONObject message = new JSONObject();
message.put("title","Hallo");
message.put("body","TEst");
json.put("data", message);
//I was hoping that this has not to be a valid token for testing.
json.put("to", "receiverFcmKey");
System.out.println("Sending :" + json.toString());
HttpEntity<String> httpEntity = new HttpEntity<>(json.toString(), httpHeaders);
for(int i =0; i<1; i++) {
System.out.println("Count:"+ i);
System.out.println(restTemplate.postForObject(FIREBASE_API_URL, httpEntity, String.class));
}
在这里我得到错误:
Exception in thread "main" org.springframework.web.client.HttpClientErrorException$Unauthorized: 401 Unauthorized: [no body]
Here我已经读到我可能在我的google apis设置中启用了身份验证。
问题: 我不知道我哪里出了错。我真的必须首先在Google设置中启用api吗? 标头中的身份验证设置是否错误? (在Bearer之后我尝试了空白) 我认为身份验证过程将由服务器密钥完成。
这只是一个测试环境设置,足以看到auth进程有效且消息已发送。