Piyush Upadhyay
Piyush Upadhyay

Reputation: 105

FCM notification reply to server

There is a push notification service that sends a message from a web server to an app device. The app has onMessageReceived() method implemented. However, not all the messages are being delivered and I have read somewhere that the delivery_receipt_request field, when set to true then (FCM), replies to the server mentioning the message being either delivered or not. I want to know that how can I catch that reply from the app if the message is delivered to my sender's side code.

Upvotes: 0

Views: 1863

Answers (1)

JMA
JMA

Reputation: 1825

Option 1: Via XMPP

You need to run an XMPP client on your backend. This client should connect to FCM with your project parameters. You will then be able to process message delivery stanzas sent to you by FCM. Here are the baby steps:

  1. Follow the steps in https://www.npmjs.com/package/node-xcs to setup the XMPP client, exporting environmental variables with your FCM project parameters.
  2. You can send messages through there.
  3. Listen for message delivery stanzas:

    client.on('stanza', function(stanza) {
        //HERE IS WHERE YOU PROCESS THE STANZA
        console.log('Please process me. I AM, the stanza: ', stanza.toString())
    })

The stanza you will get for message delivery will look like this:

<message id="">
  <gcm xmlns="google:mobile:data">
  {
      "category":"com.example.yourapp", // to know which app sent it
      "data":
      {
         “message_status":"MESSAGE_SENT_TO_DEVICE",
         “original_message_id”:”m-1366082849205”
         “device_registration_id”: “REGISTRATION_ID”
      },
      "message_id":"dr2:m-1366082849205",
      "message_type":"receipt",
      "from":"gcm.googleapis.com"
  }
  </gcm>
</message>

Currently only CCS (XMPP) supports upstream messaging. Knock yourself out.

Option 2: Via HTTP

Now, if you decide to use the FCM HTTP protocol instead to send the messages, then you will have to interpret the response that you get back when you make the HTTP call. You can tell whether the message was delivered or not by looking at the HTTP response header and the error in the body of the response. The structure of the response is described here: https://firebase.google.com/docs/cloud-messaging/http-server-ref#interpret-downstream

Again, knock yourself out.

Upvotes: 1

Related Questions