How to get fcm_option.link from [firebase-messaging-sw.js] Received background message (payload)

Viewed 31

I'm trying to get my FCM web notifications to contain a clickable link to my site. I have generated fcmlink through backend(java).

1.Created Class NotificationWebDataDTO which implements FirebaseNotificationData

public class NotificationWebDataDTO<T> implements FirebaseNotificationData<T> {


NotificationWebSubDataDTO<T> data;


NotificationWebFCMOptionsDTO fcmOptions;

}

  1. In NotificationWebSubDataDTO class i'm having

    public class NotificationWebSubDataDTO<T> {
    
    
    private String title;
    
    
    private String body;
    
    
    private String state;
    
    
    private String parameter;
    
    
    private String timeStamp;
    

}


3. In NotificationWebFCMOptionsDTO class
     
    ```
     public class NotificationWebFCMOptionsDTO {

    
    private String link;
}
  1. After setting values to each element. I've converted NotificationWebDataDTO to JsonObject.
public JsonObject buildNotificationMessage(FirebaseNotificationData<?> notificationDataDTO, String token) {

        JsonObject jsonObject = objectToJson(notificationDataDTO);
        JsonObject jmessage = new JsonObject();
        jmessage.addProperty("token", token);
        if (notificationDataDTO instanceof NotificationDataDTO) {
            jmessage.add("data", jsonObject);
        } else {
            Gson gson = new Gson();
            jmessage.add(notificationDataDTO.mainKey(), gson.toJsonTree(notificationDataDTO));
        }

        JsonObject jfcm = new JsonObject();
        jfcm.add(MESSAGE_KEY, jmessage);

        return jfcm;
    }
  1. Converted NotificationWebDataDTO to JsonObject (example)
{
  "message": {
    "token": "ex token",
    "webpush": {
      "data": {
        "title": "title",
        "body": "ABCBCHD",
        "state": "ABCHD",
        "parameter": "{\"djd\":177,\"djd\":true,\"fnfnf\":1,\"fnfnf\":0}",
        "timeStamp": "1661438135926"
      },
      "fcmOptions": {
        "link": "http://abcnc/#/app/requestStatus/23/true/1/0/"
      }
    }
  }
}
  1. Now Send request to FCM message using HTTP
 private NotificationResponseDTO sendMessage(JsonObject fcmMessage, Integer target) throws IOException {
        HttpURLConnection connection = getConnection(target);
        connection.setDoOutput(true);
        DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream());
        System.out.println(fcmMessage.toString());
        outputStream.writeBytes(fcmMessage.toString());
        outputStream.flush();
        outputStream.close();

        NotificationResponseDTO dto = null;

        int responseCode = connection.getResponseCode();
        if (responseCode == 200) {
            String response = inputstreamToString(connection.getInputStream());
            dto = new NotificationResponseDTO(constants.UPDATE_SUCESS, response);
            slf4jLogger.info("Message sent to Firebase for delivery, response:");
            slf4jLogger.info(response);
        } else {
            String response = inputstreamToString(connection.getErrorStream());
            Gson gson = new Gson();
            dto = gson.fromJson(response, NotificationResponseDTO.class);
            dto.setFailure(constants.UPDATE_SUCESS);
            slf4jLogger.info(response);
        }
        return dto;
    }
  1. But while catching this object through frontend payload i am not getting whole Object of webpush here is my frontend code
self.addEventListener('notificationclick', function (e) {
  console.log("self.addEventListener", e);
  clients.openWindow(e.notification.data).then(function (windowClient) { });
  e.notification.close();
});

messaging.setBackgroundMessageHandler(function(payload) {
  console.log('[firebase-messaging-sw.js] Received background message ', payload);
  const notificationTitle = payload.data.title;
  const notificationOptions = {
    body: payload.data,
    requireInteraction: true,
    icon: 'http://dev.transporto.in/images/Transporto-Logo-Black.png'    
  };
  return self.registration.showNotification(notificationTitle,notificationOptions);

});

In console i am getting

{data: {…}, from: '497325284033', priority: 'normal', fcmMessageId: 'someid'}
data:
body: "SDJNDJ"
parameter: "{\"rq\":001,\"stp\":true,\"list\":1,\"focusSectionId\":0}"
state: "app.RqStatus"
timeStamp: "1661439276370"
title: "SDDN"

there is no fcmoption with link in payload

0 Answers
Related