简体   繁体   中英

Oreo Heads up notification - how to detect/ intercept user swiping notification to status bar?

Using NotificationCompat.Builder , I am creating a incoming call notification, PRIORITY_HIGH notification with vibrate and ongoing set to true.

When notify() is called it also starts the ringtone player, playing the ringtone.

Currently this works perfectly. The answer and decline actions work fine, the only nuance is if you swipe away the notification, its demoted to the status bar (that is fine) but I also want the ringtone player to stop at this point.

Is there a way I can be told, or tell that the heads up notification has been swiped up into the status bar?

Answer in comment to accepted answer.

Problem was resolved by adding ringtone to the builder with .setSound(). Thanks AA

what you need is a deleteIntent

deleteIntent

The intent to execute when the notification is explicitly dismissed by the user, either with the "Clear All" button or by swiping it away individually. This probably shouldn't be launching an activity since several of those will be sent at the same time.

You can set the PendingIntent to a BroadcastReceiver and then perform any action you want.

Intent intent = new Intent(this, MyBroadcastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 0, intent, 0);
Builder builder = new Notification.Builder(this):
builder.setDeleteIntent(pendingIntent);

And for the BroadcastReceiver

public class MyBroadcastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        // stop your ringtone here
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM