Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swipe to dismiss notification on foreground service

I'm trying to dismiss a notification from the foreground service but yet find any solutions. I'm not using notificationManager.notify(...) but startForeground(...) instead.

My NotificationCompat.Builder

Intent actionCancelNotification = new Intent(MusicPlayerApp.getAppContext(), NotificationReceiver.class);
actionCancelNotification.setAction(ACTION_DISMISS_NOTIFICATION);
actionCancelNotification.putExtra(PLAYBACK_NOTIFICATION_ID, PLAYBACK_NOTI_ID);

PendingIntent dismissNotiPendingIntent = PendingIntent.getBroadcast(MusicPlayerApp.getAppContext(), 0, actionCancelNotification, 0);

    //NotificationCompat.Builder
builder.setCustomBigContentView(getRemoteView())
       .setContentTitle("SongPlayback Notification")
       .setSmallIcon(R.drawable.ic_playback_notification_icon)
       .setLargeIcon(BitmapFactory.decodeResource(MusicPlayerApp.getAppContext().getResources(), R.drawable.ic_playback_notification_icon))
       .setContentText("this is content text")
       .setSubText("sub text")
       .setDeleteIntent(dismissNotiPendingIntent)
       .build();

Even if i setOnGoing(false); it still did not work. I have followed this thread solution: Make a notification from a foreground service cancelable once the service is not in foreground anymore

and this is the new NotificationCompat.Builder as the official document written:

//NotificationCompat.Builder
builder.setCustomBigContentView(getRemoteView())
       .setContentTitle("SongPlayback Notification")
       .setSmallIcon(R.drawable.ic_playback_notification_icon)
       .setLargeIcon(BitmapFactory.decodeResource(MusicPlayerApp.getAppContext().getResources(), R.drawable.ic_playback_notification_icon))
       .setContentText("this is content text")
       .setSubText("sub text")
       //this did not work too
       .setDeleteIntent(MediaButtonReceiver.buildMediaButtonPendingIntent(MusicPlayerApp.getAppContext(), PlaybackStateCompat.ACTION_STOP)) //this d
       .build();

I wonder if any solutions to solve my problem. Thank you

like image 929
Krot Avatar asked Nov 08 '22 09:11

Krot


1 Answers

You can make the notification dismissible by swiping by;

stopForeground(false);

And to dismiss the notification totally;

stopForeground(true);
like image 174
Ndivhuwo Avatar answered Nov 14 '22 21:11

Ndivhuwo