Search code examples
phpandroidcordovafirebasefirebase-notifications

How to delete specific push notification from API server


I am using this PHP script for sending a notification to Firebase:

...
$fields = [
    'to' => DEVICE_TOKEN,
    'notification' => [
        'title'     => "Title",
        'body' => 'notification body',
        'tag' => 'tag',
    ],
];
$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send' );
curl_setopt( $ch,CURLOPT_POST, true );
curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
$result = curl_exec($ch );
...

and it is properly popping on my android. I can also edit the notification using tag property

But how can I delete notification? If someone read it in browser for example, I need to delete this notification from other devices.

I tried to send empty body in edited notification, but doesn't work.

On the Cordova frontend I'm using cordova-plugin-firebase and I am not sure if the problem is on backend or frontend.


Solution

  • I don't think that it's possible to delete a notification message once it was sent to the device over the Firebase API.

    One possible solution I see is to create a POST notification once the user sees the message to send it to your backend which sends a new notification with a specific "flag" and dosen't show this new notification because of the "flag" (you implement an if check in your app). And when device reads that flag it removes the notification based on the "flag". You do that with cancel() which is in NotificationManager. Documentation