我正在处理的应用程序中,我想发送推送通知给特定用户
我拿到他的代币了,存起来。
也得到了我的API密钥。
我尝试用postman发送POST请求,它成功地完成了,我想在我的代码中以编程方式完成它
private void notif(){
String json = "{\"to\": \"<USER_FCM>\",\"notification\": {\"title\": \"Check this Mobile (title)\",\"body\": \"Rich Notification testing (body)\"}";
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
try {
JSONObject jsonObject = new JSONObject(json);
} catch (JSONException e) {
e.printStackTrace();
}
try {
HttpPost request = new HttpPost("https://fcm.googleapis.com/fcm/send");
StringEntity params =new StringEntity("details={\"name\":\"myname\",\"age\":\"20\"} ", ContentType.APPLICATION_JSON);
request.addHeader("content-type", "application/json");
request.addHeader("Authorization","key=<API_KEY>");
request.setEntity(params);
HttpResponse response;
response = httpClient.execute(request);
String test = response.getEntity().getContent().toString();
Log.d("RESPONSESS",test);
}catch (Exception ex) {
} finally {
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
先谢了
1条答案
按热度按时间ddarikpa1#
下面是我如何将推送通知发送到特定的移动的令牌。我希望它能有所帮助,
依赖,