简体   繁体   中英

Is it possible to use Firebase Cloud Messaging API to list all push notifications?

I'm searching to understand the scope of Firebase FCM API. My company asked me to implement a custom FCM management panel. Hypothetically, admin can use that panel to see all the push notifications that has been sent by default Firebase FCM , send a push notification to single device or subscribe to a predefined topic.

After building the list view and basic form for sending push notification to a single device, I'll build a backend server to automatically trigger Firebase API endpoints to push notifications. However, I'm having some trouble on the Firebase API-side.

  1. Is it possible to use Firebase API to create a FCM management panel ?
  2. Is there any other public method beside send in Firebase API for FCM ?

Edit : I've build a prototype lambda function that can send automated push notifications by querying my database based on the rules stored in Firebase. This function is scheduled to run everyday. By this function below, I am calling the Messaging object

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();
        }
    }
}

The class definition and the methods for sending push notifications =>

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);
}

After I run the buildNotificationMessage() , the object is formed like example below.

// Example Notification Message to send over HTTP
{
  "message": {
    "notification": {
      "title": "title",
      "body": "body"
    },
    "token": "<FCM_token>"
  }
}

The response is =>

{  "name": "projects/<project_id>/messages/1542324302450893"}

I have to develop a dashboard for listing the sent messages, open rate and analytics . However, I need some guidance.

1 - What can I do with this name given as a response from the FCM REST API ? I didn't see anything in the documentation for getting the details of messages.

2 - Is there a better way for sending bulk messages for multiple unique FCM token ? I see some stuff about device groups but Firebase says it's for a different purpose.

Typically, "group" refers a set of different devices that belong to a single user.

Thanks

firebaser here

There is no API to get a list of all messages that were sent via FCM. If you need such a list, you will have to create it yourself by adding the messages to it as you call FCM to send them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM