0

Below is my code. I have put actions (yes or no ) using addaction to the notification.But the problem is i cannot detect if it pressed or not.Plz help.. Thanks

@SuppressWarnings("deprecation")
@Override
protected void onIncomingCallReceived(final Context ctx, final String number, final Date start) {
    // Create Intent for notification
    Intent intent = new Intent(ctx, CallReceiver.class);
    PendingIntent pi = PendingIntent.getActivity(ctx, 0, intent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    //Yes intent
    Intent yesReceive = new Intent();
    yesReceive.setAction(YES_ACTION);
    PendingIntent pendingIntentYes = PendingIntent.getBroadcast(ctx, 0, yesReceive, PendingIntent.FLAG_UPDATE_CURRENT);


    //No intent
    Intent noReceive = new Intent();
    noReceive.setAction(NO_ACTION);
    PendingIntent pendingIntentNo = PendingIntent.getBroadcast(ctx, 0, noReceive, PendingIntent.FLAG_UPDATE_CURRENT);


    // Defining notification
    NotificationCompat.Builder nBuilder = new NotificationCompat.Builder(
            ctx).setSmallIcon(R.mipmap.ic_launcher)

            .setContentTitle("Sample text 1 ?")

            .setContentText("Sample text 2").setContentIntent(pi)
            .addAction(R.drawable.ic_done_white_36dp, "Yes", pendingIntentYes)
            .addAction(R.drawable.ic_close_white_36dp, "No", pendingIntentNo);
    // Allows notification to be cancelled when user clicks it
    nBuilder.setAutoCancel(true);

    // Issuing notification

    int notificationId = 0;
    NotificationManager notifyMgr = (NotificationManager) ctx.getSystemService(NOTIFICATION_SERVICE);
    String action = intent.getAction();
    if (CallReceiver.YES_ACTION.equals(action)) {
        Toast.makeText(ctx, "YES CALLED", Toast.LENGTH_SHORT).show();
    } else if (CallReceiver.NO_ACTION.equals(action)) {
        Toast.makeText(ctx, "STOP CALLED", Toast.LENGTH_SHORT).show();
    }
    notifyMgr.notify(notificationId, nBuilder.build());

}

this code is in my CallReciever.class which extends Phonecallreciever where,phonecallreciever extends broadcastreciver.

Androidss
  • 47
  • 11
  • 3
    Does this answer your question? [Determine addAction click for Android notifications](https://stackoverflow.com/questions/15350998/determine-addaction-click-for-android-notifications) – towhid Jun 04 '20 at 06:49

2 Answers2

0

Thats not how it works. You have to observe this actions with BroadcastReciever, registered in running Activity, Fragment or Service.

IntentFilter filter = new IntentFilter();
filter.addAction(YES_ACTION);
filter.addAction(NO_ACTION);

BroadcastReceiver receiver = new BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (CallReceiver.YES_ACTION.equals(action)) {
             Toast.makeText(ctx, "YES CALLED", Toast.LENGTH_SHORT).show();
        } else if (CallReceiver.NO_ACTION.equals(action)) {
             Toast.makeText(ctx, "STOP CALLED", Toast.LENGTH_SHORT).show();
        }
    }
}
context.registerReceiver(receiver, filter);

Also don't forget to unregister it when Activity, Fragment or Service to be destroyed.

dniHze
  • 2,162
  • 16
  • 22
  • the thing is on clicking yes button, i want to save the details in db..i.e (final Context ctx, final String number, final Date start) obtained from incoming call....if do wat you say ..i wont be able to obatin the details in Onrecive function – Androidss Jul 20 '17 at 10:51
  • @Androidss I don't think so. In `onRecieve(Context context, Intent intent)` you have all what you need: 1. context, which you can use to open db or access system services; 2. intent, which has all the shared data through `Intent`. Try to use them to store all the data you need. – dniHze Jul 20 '17 at 12:00
0

In every action you need to declare a PendingIntent which will handle the action after the user clicks it. In this PendingIntent you can easily pass data if you use Intent.putExtra(key,value) and then retrieve this data from the Broadcast Receiver that you have declared.

  //done action button
    Intent doneIntent = new Intent(this, NotificationDoneReceiver.class);
    doneIntent.putExtra("id", id);
    doneIntent.putExtra("notification_id",unique_id);
    PendingIntent donePendingIntent = PendingIntent.getBroadcast(this, (unique_id * 16), doneIntent, 0);

 //create the notification
    Notification n = builder
            //set title
            .setContentTitle(title)

            .setPriority(NotificationCompat.PRIORITY_MAX)

            .setWhen(0)
                    //set content
            .setContentText(getString(R.string.time_for_medicine) + " " + medicine)
                    //set intent to launch on content click
            .setContentIntent(pIntent)
                    //cancel notification on click
            .setAutoCancel(true)
                    //add the actions
            .addAction(R.drawable.ic_done, getString(R.string.took_it), donePendingIntent)

            .build();

Broadcast Receiver for the done action.

public class NotificationDoneReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    int id = intent.getExtras().getInt("id");
    int notification_id = intent.getExtras().getInt("notification_id");

    RealmDatabase db = new RealmDatabase(context);
    MedicationPlanEvent medicationPlanEvent = db.getMedicationPlanEventBasedOnID(id);

    if(medicationPlanEvent!=null){
        Medication medication = new Medication(medicationPlanEvent.getMedicine(), TimeManager.getCurrentDate(),medicationPlanEvent.getDose(),TimeManager.getCurrentTime(),"",medicationPlanEvent.getDose_meter());
        medication.setId(db.getMedicationNextKey());

        db.insertMedication(medication);
    }


    //notification manager
    NotificationManager notificationManager =
            (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);

    notificationManager.cancel(notification_id);

    Intent overviewIntent = new Intent(context.getApplicationContext(), OverviewActivity.class);
    overviewIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(overviewIntent);


}

}

Nick Zisis
  • 288
  • 2
  • 12