Note that there are some explanatory texts on larger screens.

plurals
  1. PODetermine addAction click for Android notifications
    text
    copied!<p>I'm trying to use the new notifications interface. I've added 3 buttons to the notifications, and I want to save something to my database once each of them is clicked.</p> <p>The notification itself works well and is shown when called, I just don't know how to capture each of the three different button clicks.</p> <p>I'm using a <code>BroadcastReceiver</code> to catch the clicks, but I don't know how to tell which button was clicked.</p> <p>This is the code of <code>AddAction</code>(I've excluded the rest of the notification, as its working well) - </p> <pre><code> //Yes intent Intent yesReceive = new Intent(); yesReceive.setAction(CUSTOM_INTENT); Bundle yesBundle = new Bundle(); yesBundle.putInt("userAnswer", 1);//This is the value I want to pass yesReceive.putExtras(yesBundle); PendingIntent pendingIntentYes = PendingIntent.getBroadcast(this, 12345, yesReceive, PendingIntent.FLAG_UPDATE_CURRENT); mBuilder.addAction(R.drawable.calendar_v, "Yes", pendingIntentYes); //Maybe intent Intent maybeReceive = new Intent(); maybeReceive.setAction(CUSTOM_INTENT); Bundle maybeBundle = new Bundle(); maybeBundle.putInt("userAnswer", 3);//This is the value I want to pass maybeReceive.putExtras(maybeBundle); PendingIntent pendingIntentMaybe = PendingIntent.getBroadcast(this, 12345, maybeReceive, PendingIntent.FLAG_UPDATE_CURRENT); mBuilder.addAction(R.drawable.calendar_question, "Partly", pendingIntentMaybe); //No intent Intent noReceive = new Intent(); noReceive.setAction(CUSTOM_INTENT); Bundle noBundle = new Bundle(); noBundle.putInt("userAnswer", 2);//This is the value I want to pass noReceive.putExtras(noBundle); PendingIntent pendingIntentNo = PendingIntent.getBroadcast(this, 12345, noReceive, PendingIntent.FLAG_UPDATE_CURRENT); mBuilder.addAction(R.drawable.calendar_x, "No", pendingIntentNo); </code></pre> <p>This is the code of the <code>BroadcastReceiver</code>- </p> <pre><code> public class AlarmReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Log.v("shuffTest","I Arrived!!!!"); //Toast.makeText(context, "Alarm worked!!", Toast.LENGTH_LONG).show(); Bundle answerBundle = intent.getExtras(); int userAnswer = answerBundle.getInt("userAnswer"); if(userAnswer == 1) { Log.v("shuffTest","Pressed YES"); } else if(userAnswer == 2) { Log.v("shuffTest","Pressed NO"); } else if(userAnswer == 3) { Log.v("shuffTest","Pressed MAYBE"); } } } </code></pre> <p>I've registered the <code>BroadcastReceiver</code> in the Manifest. Also, I want to mention that the <code>BroadcastReceiver</code> is called when I click one of the buttons in the notification, but the intent always includes an extra of '2'.</p> <p>This is the notifcation iteslf - <img src="https://i.stack.imgur.com/9B1gi.png" alt="notification"></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload