I have an app which associate some entities with unique ID and notify about the entities to user, which i gonna use notificationID same as entity ID.

I have built notification with dismiss action based on the follow sample solution, by follow i mean exactly the same without any modification, since it works.

So far things doing well until i try to create 2 notification with different ID using the sample. Problem arise, the dismiss button only receive notificationID of the first notification:

The first notification behave normally as expected.

But the second notification getExtra() in BroadcastReceiver takes the notificationID of the FIRST notification instead, make the cancel notification just keep cancelling the first notification.

My create Notification function, i just call this function twice with different ID:

Void createNoti(int NOTIFICATION_ID){

    Intent buttonIntent = new Intent(context, ButtonReceiver.class);
    buttonIntent.putExtra("notificationId",NOTIFICATION_ID);

    PendingIntent btPendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, buttonIntent,0);

    NotificationCompat.Builder mb = new  NotificationCompat.Builder(getBaseContext());
    mb.addAction(R.drawable.ic_Action, "My Action", btPendingIntent);
    manager.notify(NOTIFICATION_ID, mb.build()); 

}

BroadcastReceiver class:

public class ButtonReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

    int notificationId = intent.getIntExtra("notificationId", 0);

    NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    manager.cancel(notificationId);
}

}

0 Answers