5

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

Arun J
  • 687
  • 4
  • 14
  • 27
Krot
  • 187
  • 3
  • 13
  • What kind of Service you are using ? Is it `START_NOT_STICKY`? – ADM Mar 27 '18 at 11:01
  • yes, it is **START_NOT_STICKY** – Krot Mar 27 '18 at 11:01
  • I did mention this thread on my post above. I have tried the following solution from the official documentation: https://developer.android.com/guide/topics/media-apps/audio-app/building-a-mediabrowserservice.html but it still did not work for me :( – Krot Mar 27 '18 at 11:05
  • 1
    Possible duplicate of [How to cancel a foreground service from using the notification (swipe dismiss) or clear all notifications?](https://stackoverflow.com/questions/12872204/how-to-cancel-a-foreground-service-from-using-the-notification-swipe-dismiss-o) – Gautam Mar 27 '18 at 11:19
  • @Krot have you got the solution ? – mob_web_dev Jul 16 '18 at 11:31

2 Answers2

3

You can make the notification dismissible by swiping by;

stopForeground(false);

And to dismiss the notification totally;

stopForeground(true);
Ndivhuwo
  • 280
  • 2
  • 10
0

this code worked for me exactly

stopForeground(true);
Mahdi Zareei
  • 1,299
  • 11
  • 18