是否可以使用 Firebase Cloud Messaging API 列出所有推送通知?
Is it possible to use Firebase Cloud Messaging API to list all push notifications?
我正在搜索以了解 Firebase FCM 的范围 API。我的公司要求我实施自定义 FCM 管理面板。假设管理员可以使用该面板查看默认情况下已发送的所有推送通知 Firebase FCM ,将推送通知发送到单个设备或订阅预定义的主题。
构建用于向单个设备发送推送通知的列表视图和基本表单后,我将构建一个后端服务器以自动触发 Firebase API 端点以推送通知。但是,我在 Firebase API 端遇到了一些麻烦。
- 是否可以使用 Firebase API 创建 FCM 管理面板?
- 在 FCM 的 Firebase API 中,除了 send 之外,还有其他 public 方法吗?
编辑:我构建了一个原型 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();
}
}
}
class定义和发送推送通知的方法=>
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 API 的响应给出的 name
我能做什么?我在 documentation 中没有看到有关获取消息详细信息的任何内容。
2 - 是否有更好的方法 为多个唯一 FCM 令牌发送批量消息?我看到了一些关于 device groups 的东西,但 Firebase 说它是为了不同的目的。
Typically, "group" refers a set of different devices that belong to a single user.
谢谢
firebaser 在这里
无法API获取通过 FCM 发送的所有消息的列表。如果您需要这样的列表,您必须自己创建它,方法是在您调用 FCM 发送消息时向其中添加消息。
我正在搜索以了解 Firebase FCM 的范围 API。我的公司要求我实施自定义 FCM 管理面板。假设管理员可以使用该面板查看默认情况下已发送的所有推送通知 Firebase FCM ,将推送通知发送到单个设备或订阅预定义的主题。
构建用于向单个设备发送推送通知的列表视图和基本表单后,我将构建一个后端服务器以自动触发 Firebase API 端点以推送通知。但是,我在 Firebase API 端遇到了一些麻烦。
- 是否可以使用 Firebase API 创建 FCM 管理面板?
- 在 FCM 的 Firebase API 中,除了 send 之外,还有其他 public 方法吗?
编辑:我构建了一个原型 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();
}
}
}
class定义和发送推送通知的方法=>
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 API 的响应给出的 name
我能做什么?我在 documentation 中没有看到有关获取消息详细信息的任何内容。
2 - 是否有更好的方法 为多个唯一 FCM 令牌发送批量消息?我看到了一些关于 device groups 的东西,但 Firebase 说它是为了不同的目的。
Typically, "group" refers a set of different devices that belong to a single user.
谢谢
firebaser 在这里
无法API获取通过 FCM 发送的所有消息的列表。如果您需要这样的列表,您必须自己创建它,方法是在您调用 FCM 发送消息时向其中添加消息。