Alex Dem
Alex Dem

Reputation: 21

Delete notification after clicking on 'Delete'

I have a notification in my app with the following code:

@SuppressLint("RestrictedApi")
    void ShowNotification(String title, String text, int color) {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            channelId = getString(R.string.default_notification_channel_id);
            String description = getString(R.string.channel_description);
            NotificationChannel channel = new NotificationChannel(channelId, "MyNew notification", NotificationManager.IMPORTANCE_DEFAULT);
            channel.setDescription(description);
            // Register the channel with the system; you can't change the importance or other notification behaviors after this
            NotificationManager notificationManager = getSystemService(NotificationManager.class);
            notificationManager.createNotificationChannel(channel);
        }
        Intent deleteIntent = new Intent(this, MainActivity.class);
        deleteIntent.setAction(Intent.ACTION_DELETE);
        deleteIntent.putExtra(EXTRA_NOTIFICATION_ID, 0);
        PendingIntent deletePendingIntent = PendingIntent.getService(this, 0, deleteIntent, 0);

        // Create pending intent, mention the Activity which needs to be
        //triggered when user clicks on notification
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                new Intent(this, MainActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);


        NotificationCompat.Builder mNotify = new NotificationCompat.Builder(this, channelId);
        mNotify.setLights(color, 100 , 200);
        mNotify.setSmallIcon(R.drawable.ic_action_name);
        mNotify.setLargeIcon(BitmapFactory. decodeResource (getResources() , R.drawable.ic_action_name)) ;
        mNotify.setContentTitle(title);
        mNotify.setOngoing(true);

        mNotify.setShowWhen(true);
        mNotify.setContentText(text);
        mNotify.setStyle(new NotificationCompat.BigTextStyle().bigText("Much longer text that cannot fit one line..."));
        mNotify.setDefaults(Notification.DEFAULT_SOUND);

        mNotify.addAction(R.mipmap.shield3_foreground, "Delete", deletePendingIntent);
        mNotify.setContentIntent(contentIntent);//Activity which needs to be triggered when user clicks on notification
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        int mId = 1001;
        try {
            mNotificationManager.notify(mId, mNotify.build());

        }
        catch (Exception e) { e.printStackTrace(); }
    }

My notifications works fine, but the problem is that, when I click on 'Delete' in Notification, it does not remove notification.

I understand, that I have to use cancel() in my code, but have no idea how to use it here and where to use this method. What should I do, in order to use 'Delete' on my notification? Thanks.

Upvotes: 0

Views: 163

Answers (1)

liveAnyway
liveAnyway

Reputation: 649

Use BroadCastReceiver

  1. deleteIntent set by BroadCastReceiver.class
  2. deletePendingIntent set by PendingIntent.getBroadCast()
  3. cancel(id) or cancelAll() in BroadCastReceiver

more detail : android codelabs help you.

update

In your code, your deleteIntent is for MainActivity and deletePendingIntent is from Service, It's not matched.

If you don't want Service or BroadcastReceiver and just want to use MainActivity, Change code like below

Intent deleteIntent = new Intent(context, MainActivity.class);
deleteIntent.setAction(Intent.ACTION_DELETE);
//getService() -> getActivity()
PendingIntent deletePendingIntent = PendingIntent.getActivity(context, 0, deleteIntent, 0);

and you need to handle that action

//onCreate() in MainActivity
String action = getIntent().getAction();
if (action != null && action.equals(Intent.ACTION_DELETE)) {
    notificationUtil.deleteNotification(this);
    //if you don't want open app
    finish();
}

//...

//in NotificationUtil
void deleteNotification(Context context) {
    NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.cancel(mId);
}

It's bad idea

Upvotes: 1

Related Questions