'Android Studio delete notification when close app

I know there is already a topic for this, but it didn't work anyway, so try creating a new one.

MainActivity

@Override
protected void onDestroy() {
    super.onDestroy();
    MusicService musicService = new MusicService();
    musicService.cancelAllNotification(getApplicationContext());
}

MusicService

    public void cancelAllNotification(Context ctx){
    NotificationManager nMgr = (NotificationManager)  ctx.getSystemService(Context.NOTIFICATION_SERVICE);
    if (nMgr != null) {
        nMgr.cancelAll();
    } 


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source