How to get callback when MediaStyle notification is dismissed when onGoing is set to true?

506 views Asked by At

I am displaying notification with following code:

 val notification = NotificationCompat.Builder(this, AudioNotificationHandler.CHANNEL_ID)
            .setContentTitle("abc")
            .setSmallIcon(
                R.drawable.ic_certyylogo
            )
            .setStyle(
                androidx.media.app.NotificationCompat.MediaStyle()
                    .setMediaSession(mediaSession.sessionToken)
                    .setCancelButtonIntent(
                        PendingIntent.getBroadcast(
                            this,
                            23,
                            Intent("abc"),
                            PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT
                        )
                    )
                    .setShowCancelButton(true)
            )
            .setOngoing(true)
            .setDeleteIntent(
                PendingIntent.getBroadcast(
                    this,
                    23,
                    Intent("abc"),
                    PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT
                )
            )
            .build()

        notification.flags = Notification.FLAG_NO_CLEAR

        (this.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager).notify(
            34,
            notification
        )

If the onGoing flag is set to false: The notification can be swiped to dismiss if the player is in paused state and corresponding delete event is fired.

If the onGoing flag is set to true: The notification can still be swiped to dismiss if the player is in paused state but the corresponding event is not fired.

Am I doing something wrong? How to get event to be fired when the notification is swiped away when onGoingFlag is set to true??

1

There are 1 answers

0
carlosrafaelgn On

In order to receive the callback, you need to register a receiver, and watch for the intent there:

Intent intent = new Intent(theApplication, ExternalReceiver.class);
intent.setAction("com.example.app.exit");
PendingIntent intentExit = PendingIntent.getBroadcast(theApplication, 0, intent, PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT);

notification = ...
            .setOngoing(false) // Very important!
            .setDeleteIntent(intentExit);

Then, on the ExternalReceiver class:

public final class ExternalReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        switch (intent.getAction()) {
        case "com.example.app.exit":
            // Handle the intent here
            break;
        }
    }
}

And don't forget to add the ExternalReceiver to the manifest:

<application...>
    ...
    <receiver
        android:name="package.ExternalReceiver"
        android:exported="true" >
        <intent-filter>
            ...
            <action android:name="com.example.app.exit" />
        </intent-filter>
    </receiver>
</application>