Android Notification button(Action buttons) to pass data when buttons pressed

2.2k views Asked by At

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.

1

There are 1 answers

0
EAG On

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

        }

    }