Sunny Bhadana
Sunny Bhadana

Reputation: 241

Notification is not swipable to dismiss

I'm creating a Notification in my android app but I want that the notification is cleared from the Notification Bar when user swipe it left or right like all other simple notifications. I have tried my thing but it is not working. This is my Notification Code

private void showNotificationPause() {
// Using RemoteViews to bind custom layouts into Notification
        RemoteViews views = new RemoteViews(getPackageName(),
                R.layout.status_bar_old);
        RemoteViews bigViews = new RemoteViews(getPackageName(),
                R.layout.status_bar_expanded);
// showing default album image
        views.setViewVisibility(R.id.status_bar_icon, View.GONE);
        views.setViewVisibility(R.id.status_bar_album_art, View.VISIBLE);
        bigViews.setImageViewBitmap(R.id.status_bar_album_art,
                Constants.getDefaultAlbumArt(this));

        Intent notificationIntent = new Intent(this, MusicActivity.class);
        notificationIntent.setAction(Constants.ACTION.MAIN_ACTION);
//        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
//                | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
                notificationIntent, 0);

        Intent previousIntent = new Intent(this, NotificationService.class);
        previousIntent.setAction(Constants.ACTION.PREV_ACTION);
        PendingIntent ppreviousIntent = PendingIntent.getService(this, 0,
                previousIntent, 0);

        Intent playIntent = new Intent(this, NotificationService.class);
        playIntent.setAction(Constants.ACTION.PLAY_ACTION);
        PendingIntent pplayIntent = PendingIntent.getService(this, 0,
                playIntent, 0);

        Intent nextIntent = new Intent(this, NotificationService.class);
        nextIntent.setAction(Constants.ACTION.NEXT_ACTION);
        PendingIntent pnextIntent = PendingIntent.getService(this, 0,
                nextIntent, 0);

        Intent closeIntent = new Intent(this, NotificationService.class);
        closeIntent.setAction(Constants.ACTION.STOPFOREGROUND_ACTION);
        PendingIntent pcloseIntent = PendingIntent.getService(this, 0,
                closeIntent, 0);

        views.setOnClickPendingIntent(R.id.status_bar_play, pplayIntent);
        bigViews.setOnClickPendingIntent(R.id.status_bar_play, pplayIntent);

        views.setOnClickPendingIntent(R.id.status_bar_next, pnextIntent);
        bigViews.setOnClickPendingIntent(R.id.status_bar_next, pnextIntent);

        views.setOnClickPendingIntent(R.id.status_bar_prev, ppreviousIntent);
        bigViews.setOnClickPendingIntent(R.id.status_bar_prev, ppreviousIntent);

        views.setOnClickPendingIntent(R.id.status_bar_collapse, pcloseIntent);
        bigViews.setOnClickPendingIntent(R.id.status_bar_collapse, pcloseIntent);

        views.setImageViewResource(R.id.status_bar_play,
                R.drawable.play);
        bigViews.setImageViewResource(R.id.status_bar_play,
                R.drawable.play);

        views.setTextViewText(R.id.status_bar_track_name, song_name.get(pos));
        bigViews.setTextViewText(R.id.status_bar_track_name, song_name.get(pos));

        views.setTextViewText(R.id.status_bar_artist_name, singer.get(pos));
        bigViews.setTextViewText(R.id.status_bar_artist_name, singer.get(pos));

        bigViews.setTextViewText(R.id.status_bar_album_name, "");
        status = new NotificationCompat.Builder(this)
                .setAutoCancel(true)
                .setOngoing(false)
                .setContentIntent(PendingIntent.getActivity(getApplicationContext(),
                        Constants.NOTIFICATION_ID.FOREGROUND_SERVICE,
                        notificationIntent, 0))
                .build();

        status.flags = Notification.FLAG_AUTO_CANCEL;
//        status = new Notification.Builder(this)
//                .setAutoCancel(true)
//                .setOngoing(false)
//                .setContentIntent(PendingIntent.getActivity(getApplicationContext(), Constants.NOTIFICATION_ID.FOREGROUND_SERVICE, new Intent(), 0))
//                .build();
        status.contentView = views;
        status.bigContentView = bigViews;
//        status.flags = Notification.FLAG_AUTO_CANCEL;
//        status.flags = Notification.FLAG_LOCAL_ONLY;
//        status.flags = Notification.F;
        status.icon = R.drawable.status;
        status.contentIntent = pendingIntent;
        Uri uri = Uri.parse(mySongs.get(pos));
        NotificationTarget notificationTarget = new NotificationTarget(getApplicationContext(),
                bigViews,
                R.id.status_bar_album_art,
                status,
                101
        );
        NotificationTarget notificationTarget_sm = new NotificationTarget(getApplicationContext(),
                views,
                R.id.status_bar_album_art,
                status,
                101);

        MediaMetadataRetriever mmr = new MediaMetadataRetriever();
        try {
            mmr.setDataSource(getApplicationContext(), Uri.parse(URLDecoder.decode(uri.toString())));
            if (mmr.getEmbeddedPicture() != null) {
                Glide
                        .with(getApplicationContext())
                        .load(mmr.getEmbeddedPicture())
                        .asBitmap()
                        .placeholder(R.drawable.logo2)
                        .into(notificationTarget_sm);
                Glide
                        .with(getApplicationContext())
                        .load(mmr.getEmbeddedPicture())
                        .asBitmap()
                        .error(R.drawable.logo2)
                        .placeholder(R.drawable.logo2)
                        .into(notificationTarget);
            }
        }catch (Exception e){

        }
        NotificationManager notificationManager= (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(Constants.NOTIFICATION_ID.FOREGROUND_SERVICE, status);

//        status.notify();
//        startForeground(Constants.NOTIFICATION_ID.FOREGROUND_SERVICE, status);
    }

I don't know much about Notification. Is anybody have idea what happened.

Upvotes: 1

Views: 273

Answers (2)

nhoxbypass
nhoxbypass

Reputation: 10152

AFAIK, you cannot hide notification while your foreground service is running (service start with startForGround()). It is Google's rule for the users to see that the application is able to run in background.

Try remove startForGround(). Or if you still want to keep startForGround() and want to give it a try, try replace setting flags with following code:

notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_VIBRATE;
notification.defaults |= Notification.DEFAULT_LIGHTS;
notification.flags |= Notification.FLAG_AUTO_CANCEL;

Detail answer already posted here: https://stackoverflow.com/a/14373655/5282585

Upvotes: 0

Pavneet_Singh
Pavneet_Singh

Reputation: 37404

As indicated in comments, You need to remove the startForeground because this will never let user to remove the notification instead use NotificationManager#notify

Upvotes: 2

Related Questions