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 :
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.
you should try this. this is worked for me.
Notification data receiving code
Notification implementing code