Determine addAction click for Android notifications Determine addAction click for Android notifications android android

Determine addAction click for Android notifications


It's because you're using FLAG_UPDATE_CURRENT with Intents that have the same action

From the docs:

if the described PendingIntent already exists, then keep it but its replace its extra data with what is in this new Intent.

When you specify pendingIntentMaybe and pendingIntentNo, the system uses the PendingIntent created for pendingIntentYes, but it overwrites the extras. Thus, all three variables refer to the same object, and the last extras specified were for pendingIntentNo.

You should specify an alternative action for each Intent. You can still have one BroadcastReceiver, and just have it intercept all three actions. This would be less confusing semantically as well :)

Your Notification poster:

//Yes intentIntent yesReceive = new Intent();  yesReceive.setAction(YES_ACTION);PendingIntent pendingIntentYes = PendingIntent.getBroadcast(this, 12345, yesReceive, PendingIntent.FLAG_UPDATE_CURRENT);mBuilder.addAction(R.drawable.calendar_v, "Yes", pendingIntentYes);//Maybe intentIntent maybeReceive = new Intent();  maybeReceive.setAction(MAYBE_ACTION);PendingIntent pendingIntentMaybe = PendingIntent.getBroadcast(this, 12345, maybeReceive, PendingIntent.FLAG_UPDATE_CURRENT);mBuilder.addAction(R.drawable.calendar_question, "Partly", pendingIntentMaybe);//No intentIntent noReceive = new Intent();  noReceive.setAction(NO_ACTION);PendingIntent pendingIntentNo = PendingIntent.getBroadcast(this, 12345, noReceive, PendingIntent.FLAG_UPDATE_CURRENT);mBuilder.addAction(R.drawable.calendar_x, "No", pendingIntentNo);

Your receiver:

@Overridepublic void onReceive(Context context, Intent intent) {    String action = intent.getAction();    if(YES_ACTION.equals(action)) {        Log.v("shuffTest","Pressed YES");    } else if(MAYBE_ACTION.equals(action)) {        Log.v("shuffTest","Pressed NO");    } else if(NO_ACTION.equals(action)) {        Log.v("shuffTest","Pressed MAYBE");    }}           


STEP_BY_STEP

Step 1

public void noto2() // paste in activity{    Notification.Builder notif;    NotificationManager nm;    notif = new Notification.Builder(getApplicationContext());    notif.setSmallIcon(R.drawable.back_dialog);    notif.setContentTitle("");    Uri path = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);    notif.setSound(path);    nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);    Intent yesReceive = new Intent();    yesReceive.setAction(AppConstant.YES_ACTION);    PendingIntent pendingIntentYes = PendingIntent.getBroadcast(this, 12345, yesReceive, PendingIntent.FLAG_UPDATE_CURRENT);    notif.addAction(R.drawable.back_dialog, "Yes", pendingIntentYes);    Intent yesReceive2 = new Intent();    yesReceive2.setAction(AppConstant.STOP_ACTION);    PendingIntent pendingIntentYes2 = PendingIntent.getBroadcast(this, 12345, yesReceive2, PendingIntent.FLAG_UPDATE_CURRENT);    notif.addAction(R.drawable.back_dialog, "No", pendingIntentYes2);    nm.notify(10, notif.getNotification());}

Step 1.5

I created a global class AppConstant

 public class AppConstant    {  public static final String YES_ACTION = "YES_ACTION";  public static final String STOP_ACTION = "STOP_ACTION";  } 

Step 2:

 public class NotificationReceiver extends BroadcastReceiver {@Overridepublic void onReceive(Context context, Intent intent) {    // TODO Auto-generated method stub    String action = intent.getAction();    if (AppConstant.YES_ACTION.equals(action)) {        Toast.makeText(context, "YES CALLED", Toast.LENGTH_SHORT).show();    }    else  if (AppConstant.STOP_ACTION.equals(action)) {        Toast.makeText(context, "STOP CALLED", Toast.LENGTH_SHORT).show();    }}

}

Step 3

 <receiver android:name=".NotificationReceiver">        <intent-filter>            <action android:name="YES_ACTION"/>            <action android:name="STOP_ACTION"/>        </intent-filter>    </receiver>


in My case it worked for me after adding the intent-filter

 <receiver android:name=".AlarmReceiver">      <intent-filter>           <action android:name="YES_ACTION"/>           <action android:name="NO_ACTION"/>           <action android:name="MAYBE_ACTION"/>      </intent-filter>  </receiver>