Notification not getting removed on clicking actio

2019-01-20 12:10发布

问题:

I'm delivering a notification which has 2 action buttons namely "Accept" and "Reject".

I'm following this Github repo.

When user clicks "Accept", certain conditions are checked and the logic is performed accordingly.

UPDATE 2.0 - The problem is that upon clicking "Accept" button, operation is happening successfully but the notification isn't disappearing from the status bar because the id generating here: m = (new Random()).nextInt(10000); is different from here: actionIntent.putExtra("id", NotificationARBroadcastReceiver.m); every single time!

Here's the code for notification:

Intent notificationIntent = new Intent(getBaseContext(), NotificationARBroadcastReceiver.class);
notificationIntent.putExtra(NotificationARBroadcastReceiver.NOTIFICATION, getNotificationNewRequestService());
PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), m, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, 0, pendingIntent);

Here's getNotificationNewRequestService():

private Notification getNotificationNewRequestService() {

        mBuilder =
                new NotificationCompat.Builder(getBaseContext())
                        .setSmallIcon(R.mipmap.app_icon_1)
                        .setContentTitle("Title")
                        .setContentText("text...");

        Intent resultIntent = new Intent(getBaseContext(), Profile.class);

        PendingIntent resultPendingIntent =
                PendingIntent.getActivity(
                        getBaseContext(),
                        0,
                        resultIntent,
                        PendingIntent.FLAG_UPDATE_CURRENT
                );

        // for action button
        Intent actionIntent = new Intent(getBaseContext(), MyBroadcastSender.class);
        actionIntent.putExtra("id", NotificationARBroadcastReceiver.m);
        PendingIntent actionPendingIntent = PendingIntent
                .getBroadcast(getBaseContext(),
                        0, actionIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        mBuilder.setAutoCancel(true);
        mBuilder.setContentIntent(resultPendingIntent);
        mBuilder.addAction(R.drawable.ic_accepted_request_black_24dp, "Accept", actionPendingIntent);
        mBuilder.addAction(R.drawable.ic_close_black_24dp, "Reject", null);

        return mBuilder.build();
    }

Here's NotificationARBroadcastReceiver.java file:

public class NotificationARBroadcastReceiver extends BroadcastReceiver {

    public static String NOTIFICATION = "notification";
    public static NotificationManager mNotifyMgr;
    public static int m;

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

        m = (new Random()).nextInt(10000);
        Log.d("mMain", String.valueOf(m));

        mNotifyMgr =
                (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);

        Notification notification = intent.getParcelableExtra(NOTIFICATION);
        notification.defaults |= Notification.DEFAULT_SOUND;
        notification.defaults |= Notification.DEFAULT_VIBRATE;
        mNotifyMgr.notify(m, notification);

    }
}

Here's MyBroadcastSender.java file:

public class MyBroadcastSender extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {

        Toast.makeText(context, "Broadcast Received by MyBroadcastSender.", Toast.LENGTH_SHORT).show();

        int id = intent.getIntExtra("id", 1);

        // send back to your class
        Intent newIntent = new Intent();
        newIntent.setAction(context.getString(R.string.broadcast_id));
        newIntent.putExtra("id1", id);
        context.sendBroadcast(newIntent);
        context.sendBroadcast(new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS));
        Toast.makeText(context, "Broadcast sent back.", Toast.LENGTH_SHORT).show();

    }
}

and here's MyBroadcastReceiver.java file:

// BroadcastReceiver
    public class MyBroadcastReceiver extends BroadcastReceiver {

        public MyBroadcastReceiver(){
            super();
        }

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

            int id2 = intent.getIntExtra("id1", 1);

            if (intent.getAction() != null && intent.getAction().equals(getString(R.string.broadcast_id))) {

                NotificationARBroadcastReceiver.mNotifyMgr.cancel(id2);

                Intent intent1 = new Intent(MyService.this, MainActivity.class);
                intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent1);

                Toast.makeText(context, "Broadcast received by MyBroadcastReceiver. Now, you can perform actions.",
                        Toast.LENGTH_SHORT).show();

            } else {
                Toast.makeText(context, "Intent is null.", Toast.LENGTH_SHORT).show();
            }
        }
    }

In getNotificationNewRequestService(), I'm putting notification id as an extra in "id", then in MyBroadcastSender.java, I'm getting this extra as int id = intent.getIntExtra("id", 1); and then putting again as newIntent.putExtra("id1", id); and then finally getting it in MyBroadcastReceiver.java as int id2 = intent.getIntExtra("id1", 1); and trying to remove the notification using it as NotificationARBroadcastReceiver.mNotifyMgr.cancel(id2);.

Sorry for this much code, I've to upload it all as they all are necessary.

What I want is to know how to deliver the same notification id from NotificationARBroadcastReceiver.java (which is a separate java file) to MyBroadcastReceiver(which is an inner class in MyService.java)?

Update 1.0- this is what happened when I printed out the values of m, mMain, id, id1:

D/m: 0
D/mMain: 9994
D/id: 0
D/id1: 0

回答1:

Assuming getNotificationService() == getNotificationNewRequestService() Looks like the NotificationARBroadcastReceiver isn't called before the notfication is built and displayed.

You would do better to generate the notification id where you create the notification and just add it to the intent there as well you don't need to make.

So call getNotificationNewRequestService() from NotificationARBroadcastReceiver.recieve() and make sure the notification ids match up.



回答2:

Edit:

move:

m = (new Random()).nextInt(10000);

before:

actionIntent.putExtra("id", NotificationARBroadcastReceiver.m); // this will be 'm'

Result:

int m = (new Random()).nextInt(10000);
Intent actionIntent = new Intent(getBaseContext(), MyBroadcastSender.class);
actionIntent.putExtra("id", m);
Log.d(getClass().getSimpleName(), "Notification Id is : " + m);

then, you can check what values are in id, id1 and id2. Don't forget to call .notify() with same Id you got from m.

You can, also, create getRandomNotificationId() and getLastGeneratedNotificationId() methods. Whenever you generate an Id, store it in public static integer variable, so that you can access it throughout the class.

Problem might be that you are accessing m from NotificationARBroadcastReceiver before initializing it. So, it will definitely be 0. And, you mentioned something about println error, are you using System.out.println()?

Before Edit:

As seen on your new edit, try closing notification before starting it:

m = (...);

// some code here

mNotifyMgr.cancel(m);
mNotifyMgr.notify(m, notification);

and see if your issue gets resolved.