在先前的通知中禁用了“操作”按钮

时间:2019-04-16 00:58:36

标签: android-notifications

我在notification中放置了两个操作按钮1)编辑2)删除。每次点击都会显示新的notification。在上一个通知中单击“删除”按钮,但在上一个通知中则单击。例如,我已显示4个通知,“删除”按钮在上一个通知中是可单击的,但在前三个通知中不可单击。

我想通过单击操作按钮(删除)来关闭正在进行的通知。

如何解决? Screenshot

private void showStickyNotification(String title) {

        NOTIFICATION_ID = (int) System.currentTimeMillis();
        //necessary to display on android Oreo and above
        createNotificationChannel();

        //Tap to open app
        Intent mIntent = new Intent(this, MainActivity.class);
        mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        PendingIntent mPendingIntent = PendingIntent.getActivity(this, 0, mIntent, PendingIntent.FLAG_ONE_SHOT);
        //delete button
        PendingIntent dismissIntent = NotificationActivity.getDismissIntent(NOTIFICATION_ID, this);
        //edit button
        Intent resultIntent = new Intent(this, MainActivity.class);
        resultIntent.putExtra(Intent.EXTRA_TITLE, title);
        PendingIntent pI = PendingIntent.getActivity(this, num, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID);
        //set icon
        builder.setSmallIcon(R.drawable.ic_notification_black);
        //notification title
        builder.setContentTitle(title.split("\n")[0]);
        //notification description
        builder.setContentText(title);
        //notification priority
        builder.setPriority(NotificationCompat.PRIORITY_HIGH);

        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
        builder.setLargeIcon(bitmap);
        builder.setStyle(new NotificationCompat.BigTextStyle().bigText(title));
        builder.setContentIntent(mPendingIntent);
        builder.setOngoing(true);

        //Add action buttons to notification
        builder.addAction(R.drawable.ic_edit_black, "Edit", pI);
        builder.addAction(R.drawable.ic_delete_black, "Delete", dismissIntent);

        NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(this);
        //show notification
        notificationManagerCompat.notify(NOTIFICATION_ID, builder.build());
        Toast.makeText(this, "Successfully saved in your notification..", Toast.LENGTH_LONG).show();
    }

    private void createNotificationChannel() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            //this name will show when user click settings after swiping notification
            CharSequence name = "Notification Title";
            String description = "The notification description.";
            int importance = NotificationManager.IMPORTANCE_DEFAULT;

            NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID, name, importance);
            notificationChannel.setDescription(description);

            NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            notificationManager.createNotificationChannel(notificationChannel);
        }
    }

通知活动

public class NotificationActivity extends Activity {

    public static final String NOTIFICATION_ID = "NOTIFICATION_ID";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        manager.cancel(getIntent().getIntExtra(NOTIFICATION_ID, -1));
        finish(); // since finish() is called in onCreate(), onDestroy() will be called immediately
    }

    public static PendingIntent getDismissIntent(int notificationId, Context context) {
        Intent intent = new Intent(context, NotificationActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        intent.putExtra(NOTIFICATION_ID, notificationId);
        PendingIntent dismissIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
        return dismissIntent;
    }

}

1 个答案:

答案 0 :(得分:1)

I've edited your method like this:

private void showStickyNotification(String title) {

        NOTIFICATION_ID = (int) System.currentTimeMillis();
        //necessary to display on android Oreo and above
        createNotificationChannel();

        //Tap to open app
        Intent tapIntent = new Intent(this, MainActivity.class);
        tapIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        PendingIntent tapPenIntent = PendingIntent.getActivity(this, 0, tapIntent, PendingIntent.FLAG_ONE_SHOT);
        //edit button
        Intent editIntent = new Intent(getApplicationContext(), MainActivity.class);
        editIntent.putExtra("NotificationMessage", title);
        editIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent editPenIntent = PendingIntent.getActivity(getApplicationContext(), NOTIFICATION_ID, editIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        //delete button
        Intent resultIntent = new Intent(this, MainActivity.class);
        resultIntent.putExtra("notificationID", NOTIFICATION_ID);
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
        stackBuilder.addParentStack(MainActivity.class);
        stackBuilder.addNextIntent(resultIntent);
        PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(num++, PendingIntent.FLAG_UPDATE_CURRENT);

        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID);
        builder.setSmallIcon(R.drawable.ic_notification_black);
        builder.setContentTitle(title.split("\n")[0]);
        builder.setContentText(title);
        builder.setPriority(NotificationCompat.PRIORITY_MAX);
        builder.setLargeIcon(bitmap);
        builder.setStyle(new NotificationCompat.BigTextStyle().bigText(title));
        builder.setNumber(num++);
        builder.setContentIntent(tapPenIntent);
        builder.setOngoing(true);
        builder.setAutoCancel(true);
        //Add action buttons to notification
        builder.addAction(R.drawable.ic_edit_black, "Edit", editPenIntent);
        builder.addAction(R.drawable.ic_delete_black, "Delete", resultPendingIntent);

        notificationManagerCompat = NotificationManagerCompat.from(this);
        //show notification
        notificationManagerCompat.notify(NOTIFICATION_ID, builder.build());

        Toast.makeText(this, "Successfully saved in your notification..", Toast.LENGTH_LONG).show();
    }

and in onCreate

int num;
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

try {
            NotificationManager Nmang = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
            Nmang.cancel(getIntent().getExtras().getInt("notificationID"));
        } catch (Exception e) {
            e.printStackTrace();
        }


}