1

I want to add an action on a Button of a Notification (yes or no) like when some one clicks on it, it had to be passed states like yes or no to a layout without opening app. below is a sample image :

enter image description here

this is my code

private void NotifyKool() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            CharSequence name = "My notification";
            String desc = "My notification desc";
            int importance = NotificationManager.IMPORTANCE_DEFAULT;
            NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID, name, importance);
            notificationChannel.setDescription(desc);

            NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            notificationManager.createNotificationChannel(notificationChannel);

            BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
                @Override
                public void onReceive(Context context, Intent intent) {
                    NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
                    manager.cancelAll();
                    Toast.makeText(context, intent.getAction(), Toast.LENGTH_SHORT).show();
                    attendance = intent.getAction();
                    if (attendance.equals("NO")) {
                        attend_sts = "offline";
                     
                    }
                }
            };

            Intent yes = new Intent();
            yes.setAction("Yes");
            PendingIntent pendingYes =
                    PendingIntent.getBroadcast(Attendance.this, 0, yes, PendingIntent.FLAG_ONE_SHOT);

            Intent no = new Intent();
            no.setAction("No");
            PendingIntent pendingNo =
                    PendingIntent.getBroadcast(Attendance.this, 1, no, PendingIntent.FLAG_ONE_SHOT);

            NotificationCompat.Builder builder = new NotificationCompat.Builder(Attendance.this, CHANNEL_ID)
                    .setSmallIcon(R.drawable.ic_baseline_error_outline_24)
                    .setContentTitle("My notification")
                    .setContentText("Hello World!")
                    .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                    .setContentIntent(pendingYes)
                    .setContentIntent(pendingNo)
                    .setAutoCancel(true)
                    .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                    .addAction(R.drawable.ic_baseline_thumb_up_24, "yes",
                            pendingYes)
                    .addAction(R.drawable.ic_baseline_thumb_down_24, "no",
                            pendingNo);

            NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(Attendance.this);
            notificationManagerCompat.notify(1, builder.build());

            IntentFilter intentFilter = new IntentFilter();
            intentFilter.addAction("Yes");
            intentFilter.addAction("No");

            registerReceiver(broadcastReceiver, intentFilter);
        }
    }


if you have an idea, please help me, thank you.

EAG
  • 33
  • 6
  • You've generated a `PendingIntent` for a broadcast `Intent`, but the target is an `Activity` (`MainActivity5`). If you want the action to send a broadcast `Intent`, you'll need the target to be a `BroadcastReceiver`. – David Wasser May 03 '22 at 15:54
  • Also, what do you mean you want something **passed to a layout without opening the app**. Please explain more about what you are trying to do here. – David Wasser May 03 '22 at 15:55
  • Were you able to resolve this? Can you answer my question? – David Wasser May 04 '22 at 11:52
  • @DavidWasser, I change my code due to some changes. then i got the toast when button clicks. but i want to get button state to the Java file. can you help me? – EAG May 04 '22 at 11:55
  • When you say "Java file", do you mean `Activity`? Is your app running? You need to try to explain better what you are trying to accomplish. – David Wasser May 04 '22 at 11:57
  • This is purpose of generating notification: when I set a time for work, when time reached, automatically generate a notification with action buttons and when user click one of buttons, then pass the button state as string via intent to the MainActivity5 without opening app. then according to the state, I have to do another work. – EAG May 04 '22 at 11:57
  • yah app was running. java file was MainActivity.java – EAG May 04 '22 at 11:59
  • `Activity`s have UI. If you want something to happen in the background without showing any UI, you need a `Service` or a `BroadcastReceiver` and do the work there. – David Wasser May 04 '22 at 11:59
  • Or, if you want this information available in your app UI (that is already running), you can have the `Activity` register for a broadcast `Intent`, and the notification can send the data in a broadcast`Intent` which your `Activity` will then receive. – David Wasser May 04 '22 at 12:01
  • What do you mean by "without opening the app"?? – David Wasser May 04 '22 at 12:02
  • without opening app means, in pendingIntent, when we use getActivity, then when action button clicks, then app was open. but i need to pass the string without opening app. can i do it?? – EAG May 04 '22 at 12:02
  • yah your idea was matched for my problem. so how notification send data in broadcast intent to activity. can you share a example code or something. – EAG May 04 '22 at 12:06
  • And I already added BroadCastReceiver for the Activity also. can you see edited code again please. – EAG May 04 '22 at 12:11
  • Generally speaking your code looks ok. However, you don't want to register 2 separate actions. Better would be to have an action like "your.package.name.ACTION" and then have the "yes" or "no" added to the `Intent` as extras. In any case, you need to set your package name on the `Intent` with `Intent.setPackage()` because implicit broadcast `Intent`s are a security issue and are no longer allowed. – David Wasser May 04 '22 at 12:19
  • 1
    finally it worked. thank you for your valuable responses again! keep it up @DavidWasser – EAG May 04 '22 at 15:41

1 Answers1

1

you should try this. this is worked for me.

BroadcastReceiver broadcastReceiver;

Notification data receiving code


broadcastReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {

                NotificationManager manager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
                manager.cancelAll();

                String state = intent.getAction();
                if (state.equals("Yes")) {
                    Toast.makeText(context, state, Toast.LENGTH_LONG).show();

                } else if (state.equals("No")) {
                    Toast.makeText(context, state, Toast.LENGTH_LONG).show();
                }
            }
        };

Notification implementing code

 public void NotifyKool() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            CharSequence name = "My notification";
            String desc = "My notification desc";
            int importance = NotificationManager.IMPORTANCE_DEFAULT;
            NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID, name, importance);
            notificationChannel.setDescription(desc);


            NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            notificationManager.createNotificationChannel(notificationChannel);

            Intent intent = new Intent(); //same
            intent.setAction("Yes");
            intent.putExtra("RES",true);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
            PendingIntent pendingIntent1 = PendingIntent.getBroadcast(this, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);

            // Update the "extra" in the Intent
            intent.setAction("No");
            intent.putExtra("RES",false); //but different
            PendingIntent pendingIntent2 = PendingIntent.getBroadcast(this, 2, intent, PendingIntent.FLAG_UPDATE_CURRENT);


            NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity7.this, CHANNEL_ID)
                    .setSmallIcon(R.drawable.ic_baseline_error_24)
                    .setContentTitle("Attendance check")
                    .setContentText("Do you come to office today?")
                    .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                    .setContentIntent(pendingIntent1)
                    .setContentIntent(pendingIntent2)
                    .setAutoCancel(true)
                    .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                    .addAction(R.drawable.ic_baseline_thumb_up_24, "Yes",
                            pendingIntent1)
                    .addAction(R.drawable.ic_baseline_thumb_down_24, "No",
                            pendingIntent2);

            NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(MainActivity7.this);
            notificationManagerCompat.notify(1, builder.build());

            IntentFilter intentFilter = new IntentFilter();
            intentFilter.addAction("Yes");
            intentFilter.addAction("No");

            registerReceiver(broadcastReceiver, intentFilter);

        }

    }

EAG
  • 33
  • 6