Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to delete push notification data message sent by FCM?

In my application, I am offering video and voice calls between users. I'm using Firebase Realtime database and FCM as well to do so.

Here is the data that my notification is delivering:

data: { 
    channel_id: channel_id,
    user_id: user_id
}

In my FirebaseMessagingService class, I retrieve the data and open the Video/Call activity like so:

if(remoteMessage.getData().size > 0) {
        Intent intent = new Intent(this, VideoCallActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        intent.putExtra("channel_id", remoteMessage.getData().get("channel_id"));
        intent.putExtra("user_id", remoteMessage.getData().get("user_id"));
        getApplicationContext().startActivity(intent);

    }

Everything is working perfectly even when the application is in background. However, I'm facing this issue:

If an user didn't have internet connection when he was called, when he connects to internet his phone will automatically start the VideoCallActivity even if the call has been aborted (by the caller) and the notification removed from Firebase real time database.

So, please is there a way to cancel sent notifications, or know the delivery status of those notifications, or another way around to do so? That will allow me to add the list of missed calls for users.

Thanks!

like image 702
Panther007 Avatar asked Feb 02 '19 23:02

Panther007


2 Answers

In a scenario like this, I know of two options:

  1. Send another FCM message when the call is cancelled
  2. Only use FCM to send a tickle

Send another FCM message when the call is cancelled

One of the properties you can give an FCM message is an collapse_key. When you send a message with a collapse_key, that message replaces any previous message with the same collapse_key value.

You can use this to send a "nope, forget about it" message, when the user cancels the call.

Only use FCM to send a tickle

Alternatively you can have the FCM message just be a so-called tickle: an almost empty message, that just tells the app to wake up and go check its queue of incoming messages in the database.

That way: if the call is still in the database, the app can pick it up. But if there is no call in the database, it can just do nothing.

You'll want to do this type of database interaction in a service, so that it can run when the app is backgrounded.

like image 115
Frank van Puffelen Avatar answered Oct 23 '22 07:10

Frank van Puffelen


Giving an expiration time is feasible as suggested in the documentation Check Here

Setting the lifespan of a message

On Android and Web/JavaScript, you can specify the maximum lifespan of a message. The value must be a duration from 0 to 2,419,200 seconds (28 days), and it corresponds to the maximum period of time for which FCM stores and attempts to deliver the message. Requests that don't contain this field default to the maximum period of four weeks.

Here are some possible uses for this feature:

  • Video chat incoming calls
  • Expiring invitation events
  • Calendar events
like image 1
Danish Khan Avatar answered Oct 23 '22 05:10

Danish Khan