Panther007
Panther007

Reputation: 275

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!

Upvotes: 8

Views: 9838

Answers (3)

Luke Hutchison
Luke Hutchison

Reputation: 9220

awesome_notifications_fcm lets you delete notifications. But it has limitations if you don't buy a license token, and the source is made available under a commercial license. Since the source is out there though, you could study it and see how they achieve this separately on Android and iOS.

https://github.com/rafaelsetragni/awesome_notifications_fcm

Upvotes: 0

Danish Khan
Danish Khan

Reputation: 39

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

Upvotes: 1

Frank van Puffelen
Frank van Puffelen

Reputation: 599041

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.

Upvotes: 17

Related Questions