通知不能解雇

时间:2017-11-07 13:47:16

标签: android notifications

我正在我的Android应用中创建通知,但我希望当用户向左或向右滑动通知栏时,通知会从所有其他简单通知中清除。 我试过了我的东西,但它没有用。 这是我的通知代码

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);
    }

我对通知不太了解。有人知道发生了什么。

2 个答案:

答案 0 :(得分:2)

As indicated in comments,您需要删除startForeground,因为这绝不允许用户删除通知,而是使用NotificationManager#notify

答案 1 :(得分:0)

AFAIK,无法前台服务正在运行时隐藏通知(服务以startForGround()开头)。谷歌的规则是让用户看到该应用程序能够在后台运行。

尝试删除startForGround()。或者,如果您仍想保留startForGround()并想尝试一下,请尝试使用以下代码替换设置标志:

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

详细答案已在此处发布:https://stackoverflow.com/a/14373655/5282585