即使在提供通知 ID 后,单击操作按钮也不会删除通知 [英] Notification not getting removed on clicking action button even after providing the notification id

本文介绍了即使在提供通知 ID 后,单击操作按钮也不会删除通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在发送一个通知,其中有 2 个操作按钮,即接受"和拒绝".

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

我正在关注这个 Github 存储库.

当用户点击接受"时,会检查某些条件并相应地执行逻辑.

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

UPDATE 2.0 - 问题是点击接受"按钮后,操作成功发生但通知没有从状态栏中消失,因为这里生成的id:m =(new Random()).nextInt(10000); 与这里不同:actionIntent.putExtra("id", NotificationARBroadcastReceiver.m); 每一次!

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!

这是通知的代码:

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);

这是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();
    }

这是NotificationARBroadcastReceiver.java文件:

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);

    }
}

这是 MyBroadcastSender.java 文件:

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();

    }
}

这里是 MyBroadcastReceiver.java 文件:

// 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();
            }
        }
    }

getNotificationNewRequestService() 中,我将通知 ID 作为额外的内容放在 "id" 中,然后在 MyBroadcastSender.java 中,我'我把这个额外的作为 int id = intent.getIntExtra("id", 1); 然后再次作为 newIntent.putExtra("id1", id);然后最后在 MyBroadcastReceiver.java 中获取它作为 int id2 = intent.getIntExtra("id1", 1); 并尝试使用它作为 删除通知NotificationARBroadcastReceiver.mNotifyMgr.cancel(id2);.

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.

我想要的是知道如何将相同的通知 ID 从 NotificationARBroadcastReceiver.java(这是一个单独的 Java 文件)传送到 MyBroadcastReceiver(这是MyService.java中的一个内部类)?

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- 这是我打印出mmMainid 的值时发生的情况, id1:

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

推荐答案

编辑:

移动:

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

之前:

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

结果:

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);

然后,您可以检查idid1id2 中的值.不要忘记使用从 m 获得的相同 ID 调用 .notify().

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.

您还可以创建 getRandomNotificationId()getLastGeneratedNotificationId() 方法.每当您生成 Id 时,将其存储在 public static 整数变量中,以便您可以在整个类中访问它.

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.

问题可能是您在初始化之前从 NotificationARBroadcastReceiver 访问了 m.所以,它肯定是0.而且,你提到了一些关于 println 错误的事情,你在使用 System.out.println() 吗?

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()?

编辑前:

正如您在新编辑中看到的那样,在开始之前尝试关闭通知:

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.

这篇关于即使在提供通知 ID 后,单击操作按钮也不会删除通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆