java - 如何访问使用 FCM API 发送的 Firebase Cloud 消息的详细信息?
问题描述
我已经构建了一个原型 lambda 函数,它可以通过根据存储在 Firebase 中的规则查询我的数据库来发送自动推送通知。此功能计划每天运行。通过下面的这个函数,我调用了 Messaging 对象
private void sentAutomatedMessages(List<String> tokens, CardAbandonmentRule rule) {
for (String token : tokens) {
//Create Messaging object for every user that fits in this user
Messaging msgHandler = new Messaging(rule.getTitle(), rule.getMessage(), token);
try {
msgHandler.handleSingleDevicePush();
} catch (IOException e) {
e.printStackTrace();
}
}
}
类定义和发送推送通知的方法 =>
public class Messaging {
private static final String PROJECT_ID = "<project_id>";
private static final String BASE_URL = "https://fcm.googleapis.com";
private static final String FCM_SEND_ENDPOINT = "/v1/projects/" + PROJECT_ID + "/messages:send";
private static final String MESSAGING_SCOPE = "https://www.googleapis.com/auth/firebase.messaging";
private static final String[] SCOPES = {MESSAGING_SCOPE};
private String title;
private String message;
private String token;
public Messaging(String title, String message, String token) {
this.title = title;
this.message = message;
this.token = token; // <FCM_token>
}
/**
* Retrieve a valid access token that can be use to authorize requests to the FCM REST
* API.
*
* @return Access token.
* @throws IOException
*/
private static String getAccessToken() throws IOException {
GoogleCredential googleCredential = GoogleCredential
.fromStream(new FileInputStream("<firebase_private_key.json>"))
.createScoped(Arrays.asList(SCOPES));
googleCredential.refreshToken();
return googleCredential.getAccessToken();
}
/**
* Create HttpURLConnection that can be used for both retrieving and publishing.
*
* @return Base HttpURLConnection.
* @throws IOException
*/
private static HttpURLConnection getConnection() throws IOException {
// [START use_access_token]
URL url = new URL(BASE_URL + FCM_SEND_ENDPOINT);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
String accessToken = getAccessToken();
System.out.println(accessToken);
httpURLConnection.setRequestProperty("Authorization", "Bearer " + accessToken);
httpURLConnection.setRequestProperty("Content-Type", "application/json; UTF-8");
return httpURLConnection;
// [END use_access_token]
}
/**
* Construct the body of a notification message request.
*
* @return JSON of notification message.
*/
private JsonObject buildNotificationMessage() {
JsonObject jNotification = new JsonObject();
jNotification.addProperty("title", this.title);
jNotification.addProperty("body", this.message);
JsonObject jMessage = new JsonObject();
jMessage.add("notification", jNotification);
jMessage.addProperty("token", this.token);
JsonObject jFcm = new JsonObject();
jFcm.add("message", jMessage);
return jFcm;
}
/**
* Send request to FCM message using HTTP.
*
* @param fcmMessage Body of the HTTP request.
* @throws IOException
*/
private static void sendtoSingleDevice(JsonObject fcmMessage) throws IOException {
HttpURLConnection connection = getConnection();
connection.setDoOutput(true);
DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream());
outputStream.writeBytes(fcmMessage.toString());
outputStream.flush();
outputStream.close();
int responseCode = connection.getResponseCode();
if (responseCode == 200) {
String response = inputstreamToString(connection.getInputStream());
System.out.println("Message sent to Firebase for delivery, response:");
System.out.println(response);
} else {
System.out.println("Unable to send message to Firebase:");
String response = inputstreamToString(connection.getErrorStream());
System.out.println(response);
}
}
/**
* Public method to send Push Notification
*
* @throws IOException
*/
public void handleSingleDevicePush() throws IOException {
JsonObject notificationMessage = buildNotificationMessage();
sendtoSingleDevice(notificationMessage);
}
在我运行之后buildNotificationMessage()
,对象的形成如下例所示。
// Example Notification Message to send over HTTP
{
"message": {
"notification": {
"title": "title",
"body": "body"
},
"token": "<FCM_token>"
}
}
回复是=>
{ "name": "projects/<project_id>/messages/1542324302450893"}
我必须开发一个仪表板来列出发送的消息、打开率和分析。但是,我需要一些指导。
1 - 作为FCM REST APIname
的响应,我能做些什么?我没有在文档中看到任何有关获取消息详细信息的内容。
2 - 是否有更好的方式为多个唯一 FCM 令牌发送批量消息?我看到了一些关于设备组的东西,但 Firebase 说这是出于不同的目的。
通常,“组”是指属于单个用户的一组不同设备。
谢谢
解决方案
在我与 Firebase 支持人员联系后,他们建议我使用 Google 的 BigQuery 来查看用于 Firebase 消息传递功能的数据集。
在 Firebase 设置中启用 BigQuery 集成后,您只需转到 BigQuery 控制台。
如下查询,将为您提供给定消息的详细信息。
SELECT *
FROM `<project_name>.firebase_messaging.data`
WHERE
_PARTITIONTIME = TIMESTAMP('<date as YYYY-MM-DD>')
AND message_id = '<your message id>'
AND instance_id = '<your instance id>'
ORDER BY event_timestamp;
链接以查看更多示例并阅读有关 FCM 的 BigQuery 集成 => 了解消息传递
推荐阅读
- javascript - Express.js 服务器:使用中间件的 PUT 请求
- java - 成功登录后 Spring Boot Active Directory 身份验证不重定向
- c# - Asp.Net C# mvc。运行编译的应用程序
- java - 模拟 openInputStream 读取方法
- ios - 如何在 Swift 的 UIButton 扩展中使用 UIButton 实例作为参数创建函数?
- javascript - 如何在jquery ajax中向成功函数发送附加参数
- java - 重置游戏以在java中重新开始
- python - 覆盖字符串列表中属性的 getter 和 setter
- php - 为什么我不能在带有约束的急切加载关系上 orderBy()?
- wpf - WPF - ToggleButton 模板触发器在第一次调用后停止触发