A scenario in my Android app involves sending a POST request to an HTTP triggered Cloud Function. The issue arises when Firebase registers the event twice on the Firebase console even though the function is called only once. I have ensured that the button triggering the message send disappears after the first click, eliminating the possibility of accidental double clicks. Despite my limited knowledge of Firebase and unsuccessful attempts at finding relevant documentation or similar queries, I am seeking assistance from experts in this community.
Below is the method responsible for sending a message to my FCM cloud function:
public void sendPushToSingleInstance(final Context activity, final String message, final String myId, final String theirId) {
final String url = URL_TO_CLOUD_FUNCTION;
StringRequest myReq = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Toast.makeText(activity, "Success", Toast.LENGTH_SHORT).show();
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
if (error.networkResponse != null)
Toast.makeText(activity, String.valueOf(error.networkResponse.statusCode), Toast.LENGTH_SHORT).show();
else
Toast.makeText(activity, "some error", Toast.LENGTH_SHORT).show();
}
}) {
// Request body implementation
public String getBodyContentType() {
return "application/json; charset=utf-8";
}
// Header details
public byte[] getBody() throws com.android.volley.AuthFailureError {
Map<String, String> rawParameters = new Hashtable<String, String>();
return new JSONObject(rawParameters).toString().getBytes();
};
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("from", myId);
headers.put("message", message);
headers.put("sendingTo", theirId);
return headers;
}
};
Volley.newRequestQueue(activity).add(myReq);
}
To elaborate further, my JavaScript intercepts the HTTP request, segments it, and transmits the message to a topic corresponding to the other user's id rather than targeting a specific device. Below is the JavaScript snippet for my Cloud Function:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.sendMessage = functions.https.onRequest((request, response) => {
var topicId = request.get('sendingTo');
var color = request.get('color');
var from = request.get('from')
console.log('tried to push notification');
const payload = {
notification: {
title: from,
body: color,
sound: "default"
},
};
const options = {
priority: "high",
timeToLive: 60 * 60 * 24
};
admin.messaging().sendToTopic(topicId, payload, options);
});
The logs presented below indicate the repeated invocation of the function: firebase console logs Fueling my quest for answers, I have referred to resources such as: https://firebase.google.com/docs/functions/http-events
In addition, I have scoured multiple StackOverflow discussions without encountering any users facing a similar dilemma. Any insights you could provide would be greatly appreciated.