Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
277 views
in Technique[技术] by (71.8m points)

android - Notification not getting removed on clicking action button even after providing the notification id

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...