65

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.

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.

I'm using a BroadcastReceiver to catch the clicks, but I don't know how to tell which button was clicked.

This is the code of AddAction(I've excluded the rest of the notification, as its working well) -

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

This is the code of the BroadcastReceiver-

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

}           
}

I've registered the BroadcastReceiver in the Manifest. Also, I want to mention that the BroadcastReceiver is called when I click one of the buttons in the notification, but the intent always includes an extra of '2'.

This is the notifcation iteslf - notification

7
  • that's strange. I don't see any error in your code. If you change the order of addAction() (and the PendingIntent creation), do you still get '2' ?
    – Quanturium
    Mar 12, 2013 at 1:24
  • In that case it just gives me the last addAction() I called
    – Tofira
    Mar 12, 2013 at 8:59
  • I can register three BroadcastReceivers for each of the three buttons, but I really want to avoid that.
    – Tofira
    Mar 12, 2013 at 9:32
  • Why don't you use 3 different intents for the 3 buttons and handle them in a single receiver?
    – Mus
    Mar 17, 2013 at 16:42
  • @Mus, that's exacely what i'm doing. I have 3 different intents, and one receiver.
    – Tofira
    Mar 18, 2013 at 21:56

5 Answers 5

118
+50

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 intent
Intent 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 intent
Intent 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 intent
Intent 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:

@Override
public 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");
    }
}           
7
  • 2
    I have a doubt which I need to clear. I am using a simple service class to start, pause and stop service.So there is one image button which will change its image drawable if I click i.e. play to pause image or vise-verse. In the above code you have mentioned mBuilder.addAction which contain a drawable. I am not able to figure it out where is the id of the image?? is it possible to have two drawable but only for one image button. Jul 18, 2013 at 21:09
  • 3
    I am getting error in YES_ACTION.equals(action) why? how to solve this issue. Jul 19, 2013 at 6:24
  • 1
    where and how to you set YES_ACTION, NO_ACTION... etc... And what will the manifist file look like
    – Cripto
    Dec 29, 2013 at 5:35
  • 2
    please update your answer, the (YES_ACTION) attribute is not explained.
    – midiwriter
    Nov 25, 2014 at 3:05
  • 4
    YES_ACTION, NO_ACTION and MAYBE_ACTION are constants I guess
    – GCallie
    Apr 30, 2015 at 14:31
26

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 {

@Override
public 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>
1
  • @amanpreet. How can we use string resource as value of the "android:name" property Jun 21, 2018 at 5:20
11

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>
0
2

here YES_ACTION must be yourfullpackagename.YES

like

private static final String YES_ACTION = "com.example.packagename.YES";

likewise you can use NO_ACTION or MAYBE_ACTION

In BroadcastReceiver you have to use same YES_ACTION as declared above,

means in BroadcastReceiver class you can check for custom Broadcast by following

public class NotificationReceiver extends BroadcastReceiver {

private static final String YES_ACTION = "com.example.packagename.YES";
@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub
    String action = intent.getAction();
    if(YES_ACTION.equals(action)) {
        Toast.makeText(context, "CALLED", Toast.LENGTH_SHORT).show();
    }
}

}

note : instead of YES in YES_ACTION string you can use other word also.

0

It's work for me

See example image

In your Service

private void sendNotification(String title, String body) {

    Intent activityIntent = new Intent(this, HomeActivity.class);
    activityIntent.setAction(Long.toString(System.currentTimeMillis()));

    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, activityIntent, 0);

    Intent broadcastIntent = new Intent(this, MyReceiver.class);
    broadcastIntent.putExtra("quotes_copy", body);
    PendingIntent actionIntent = PendingIntent.getBroadcast(this, 0, broadcastIntent, PendingIntent.FLAG_UPDATE_CURRENT);


    NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);

    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
        CharSequence name = getString(R.string.channel_name);
        String description = getString(R.string.channel_description);
        int importance = NotificationManager.IMPORTANCE_HIGH;
        NotificationChannel mChannel = new NotificationChannel(NOTI_CHANNEL_ID, name, importance);
        mChannel.setDescription(description);
        mChannel.enableLights(true);
        mChannel.setLightColor(Color.RED);
        mChannel.enableVibration(true);
        mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
        mChannel.setShowBadge(false);
        notificationManager.createNotificationChannel(mChannel);
    }

    NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext(), NOTI_CHANNEL_ID)
            .setSmallIcon(R.drawable.ic_logo)
            .setContentTitle(title)
            .setContentText(body)
            .setAutoCancel(true)
            .setContentIntent(contentIntent)
            .setContentTitle(title)
            .addAction(R.drawable.ic_logo, "Copy", actionIntent);

    random = new Random();
    NOTIFY_ID = random.nextInt(1000);
    notificationManager.notify(NOTIFY_ID, builder.build());

}

In your Receiver Class

 @Override
public void onReceive(Context context, Intent intent) {

    String message = intent.getStringExtra("quotes_copy");

    Toast.makeText(context, message, Toast.LENGTH_SHORT).show();

}

Manifest

<receiver android:name=".Utils.MyReceiver"/>
1
  • Make sure declare receiver in Mainfest Apr 14, 2021 at 8:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.