我如何知道通知中点击了哪个按钮?

huangapple go评论74阅读模式
英文:

How can I know which button was clicked in the notification?

问题

这是我的通知代码;

public void  NotificationSettings(Context context){
        Intent stateIntent = new Intent(context, MyBroadcastReceiver.class);
        stateIntent.putExtra("id", 100);
        PendingIntent pendingIntent =
                PendingIntent.getBroadcast(context, 0, stateIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        builder = new NotificationCompat.Builder(context, "access2020")
                .setSmallIcon(R.drawable.ic_baseline_add_alert_24)
                .setContentTitle("Academy通知")
                .setContentText("嘿,这是一个重要的通知")
                .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                .addAction(R.drawable.ic_baseline_add_alert_24, "设置为活动", pendingIntent)
                .addAction(R.drawable.ic_baseline_add_alert_24, "关闭", pendingIntent);

        notificationManager = NotificationManagerCompat.from(context);
    }

和我的广播

public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "测试", Toast.LENGTH_SHORT).show();
        int notificationId = intent.getIntExtra("id", 0);
        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
        notificationManager.cancel(notificationId);
        }
英文:

I am developing an application, I have 2 buttons in an on the notification. How can I know which button the user has clicked?

This my Notification codes;

public void  NotificationSettings(Context context){
        Intent stateIntent = new Intent(context, MyBroadcastReceiver.class);
        stateIntent.putExtra("id", 100);
        PendingIntent pendingIntent =
                PendingIntent.getBroadcast(context, 0, stateIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        builder = new NotificationCompat.Builder(context, "access2020")
                .setSmallIcon(R.drawable.ic_baseline_add_alert_24)
                .setContentTitle("Academy Notification")
                .setContentText("Hey this is an important notifications")
                .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                .addAction(R.drawable.ic_baseline_add_alert_24, "Set Active", pendingIntent)
                .addAction(R.drawable.ic_baseline_add_alert_24,"Dismiss", pendingIntent);

        notificationManager = NotificationManagerCompat.from(context);
    }

and My Broadcast

public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "test", Toast.LENGTH_SHORT).show();
        int notificationId = intent.getIntExtra("id", 0);
        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
        notificationManager.cancel(notificationId);
        }

答案1

得分: 1

你应该将一个标识符作为附加信息传递给意图,然后在你的广播接收器中进行检索。

public void NotificationSettings(Context context) {
    // 为“设置活动”操作添加附加标识符
    Intent setActiveStateIntent = new Intent(context, MyBroadcastReceiver.class);
    setActiveStateIntent.putExtra("id", 100);
    setActiveStateIntent.putExtra("action", "Action.SetActive");
    PendingIntent setActivePendingIntent =
            PendingIntent.getBroadcast(context, 0, setActiveStateIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    // 为“取消”操作添加附加标识符
    Intent dismissStateIntent = new Intent(context, MyBroadcastReceiver.class);
    dismissStateIntent.putExtra("id", 100);
    dismissStateIntent.putExtra("action", "Action.Dismiss");
    PendingIntent dismissPendingIntent =
            PendingIntent.getBroadcast(context, 0, dismissStateIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    builder = new NotificationCompat.Builder(context, "access2020")
            .setSmallIcon(R.drawable.ic_baseline_add_alert_24)
            .setContentTitle("Academy Notification")
            .setContentText("Hey this is an important notifications")
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)
            .addAction(R.drawable.ic_baseline_add_alert_24, "Set Active", setActivePendingIntent)
            .addAction(R.drawable.ic_baseline_add_alert_24, "Dismiss", dismissPendingIntent);

    notificationManager = NotificationManagerCompat.from(context);
}

然后在你的广播接收器中,你可以执行以下操作:

public void onReceive(Context context, Intent intent) {
    if (intent.getStringExtra("action").equals("Action.Dismiss")) {
        // 执行取消操作
    } else if (intent.getStringExtra("action").equals("Action.SetActive")) {
        // 执行设置活动逻辑
    } else {
        // 处理无效操作
    }
}
英文:

You should pass an identifier to the intent as an extra and then retrieve in your BroadcastReceiver.

public void  NotificationSettings(Context context){
        // put an extra identifier for Set Active Action
        Intent setActiveStateIntent = new Intent(context, MyBroadcastReceiver.class);
        setActiveStateIntent.putExtra("id", 100);
        setActiveStateIntent.putExtra("action", "Action.SetActive");
        PendingIntent setActivePendingIntent =
                PendingIntent.getBroadcast(context, 0, setActiveStateIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        // put an extra identifier for Dismiss
        Intent dismissStateIntent = new Intent(context, MyBroadcastReceiver.class);
        dismissStateIntent.putExtra("id", 100);
        dismissStateIntent.putExtra("action", "Action.Dismiss");
        PendingIntent dismissPendingIntent =
                PendingIntent.getBroadcast(context, 0, dismissStateIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        builder = new NotificationCompat.Builder(context, "access2020")
                .setSmallIcon(R.drawable.ic_baseline_add_alert_24)
                .setContentTitle("Academy Notification")
                .setContentText("Hey this is an important notifications")
                .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                .addAction(R.drawable.ic_baseline_add_alert_24, "Set Active", setActivePendingIntent)
                .addAction(R.drawable.ic_baseline_add_alert_24,"Dismiss", dismissPendingIntent);

        notificationManager = NotificationManagerCompat.from(context);
}

Then in your BroadcastReceiver you can do the following:

public void onReceive(Context context, Intent intent) {
        if(intent.getStringExtra("action").equals("Action.Dismiss")) {
             // perform your dismiss action
        } else if (intent.getStringExtra("action").equals("Action.SetActive")) {
            // perform your set active logic
        } else {
           // handle invalid action
        }
        
}

答案2

得分: 0

以下是您提供的代码的翻译部分:

我用以下方式解决了,但我仍然不确定这是否是正确的方法:

public void NotificationSettings(Context context) {
    Intent stateIntent0 = new Intent(context, MyBroadcastReceiver.class);
    Intent stateIntent1 = new Intent(context, MyBroadcastReceiver.class);

    stateIntent0.putExtra("id", 100);
    stateIntent1.putExtra("id", 200);

    PendingIntent pendingIntent0 =
            PendingIntent.getBroadcast(context, 0, stateIntent0, PendingIntent.FLAG_UPDATE_CURRENT);

    PendingIntent pendingIntent1 =
            PendingIntent.getBroadcast(context, 1, stateIntent1, PendingIntent.FLAG_UPDATE_CURRENT);

    builder = new NotificationCompat.Builder(context, "lemubitA")
            .setSmallIcon(R.drawable.ic_baseline_add_alert_24)
            .setContentTitle("Lemubit Academy Notification")
            .setContentText("Hey this is an important notification")
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)
            .addAction(R.drawable.ic_baseline_add_alert_24, "Set Active", pendingIntent0)
            .addAction(R.drawable.ic_baseline_add_alert_24, "Dismiss", pendingIntent1);

    notificationManager = NotificationManagerCompat.from(context);
}

这是我的广播部分:

int notificationId = intent.getIntExtra("id", 0);
Toast.makeText(context, notificationId + "", Toast.LENGTH_SHORT).show();
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
notificationManager.cancel(notificationId);

请注意,这里的翻译可能不包括某些变量和方法名,因为您明确指示只翻译代码部分。

英文:

I solved with this way but still i am not sure it is the right way

public void  NotificationSettings(Context context){
        Intent stateIntent0 = new Intent(context, MyBroadcastReceiver.class);
        Intent stateIntent1 = new Intent(context, MyBroadcastReceiver.class);


        stateIntent0.putExtra("id", 100);
        stateIntent1.putExtra("id", 200);


        PendingIntent pendingIntent0 =
                PendingIntent.getBroadcast(context, 0, stateIntent0, PendingIntent.FLAG_UPDATE_CURRENT);

        PendingIntent pendingIntent1 =
                PendingIntent.getBroadcast(context, 1, stateIntent1, PendingIntent.FLAG_UPDATE_CURRENT);

        builder = new NotificationCompat.Builder(context, "lemubitA")
                .setSmallIcon(R.drawable.ic_baseline_add_alert_24)
                .setContentTitle("Lemubit Academy Notification")
                .setContentText("Hey this is an important notifications")
                .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                .addAction(R.drawable.ic_baseline_add_alert_24, "Set Active", pendingIntent0)
                .addAction(R.drawable.ic_baseline_add_alert_24,"Dismiss", pendingIntent1);

        notificationManager = NotificationManagerCompat.from(context);
    }

AND this my Broadcast

int notificationId = intent.getIntExtra("id", 0);
        Toast.makeText(context, notificationId+"", Toast.LENGTH_SHORT).show();
        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
        notificationManager.cancel(notificationId);

huangapple
  • 本文由 发表于 2020年9月22日 22:51:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/64012310.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定