34

I know that you can launch Activities from the action buttons using PendingIntents. How do you make it so that the a method gets called when the user clicks the notification action button?

public static void createNotif(Context context){
    ...
    drivingNotifBldr = (NotificationCompat.Builder) new NotificationCompat.Builder(context)
            .setSmallIcon(R.drawable.steeringwheel)
            .setContentTitle("NoTextZone")
            .setContentText("Driving mode it ON!")
            //Using this action button I would like to call logTest
            .addAction(R.drawable.smallmanwalking, "Turn OFF driving mode", null)
            .setOngoing(true);
    ...
}

public static void logTest(){
    Log.d("Action Button", "Action Button Worked!");
}
Faizan Syed
  • 483
  • 1
  • 4
  • 6

1 Answers1

67

You can't directly call methods when you click action buttons.

You have to use PendingIntent with BroadcastReceiver or Service to perform this. Here is an example of PendingIntent with BroadcastReciever.

First lets build a Notification

public static void createNotif(Context context){

    ...
    //This is the intent of PendingIntent
    Intent intentAction = new Intent(context,ActionReceiver.class);

    //This is optional if you have more than one buttons and want to differentiate between two
    intentAction.putExtra("action","actionName");

    pIntentlogin = PendingIntent.getBroadcast(context,1,intentAction,PendingIntent.FLAG_UPDATE_CURRENT);
    drivingNotifBldr = (NotificationCompat.Builder) new NotificationCompat.Builder(context)
            .setSmallIcon(R.drawable.steeringwheel)
            .setContentTitle("NoTextZone")
            .setContentText("Driving mode it ON!")
            //Using this action button I would like to call logTest
            .addAction(R.drawable.smallmanwalking, "Turn OFF driving mode", pIntentlogin)
            .setOngoing(true);
    ...

}

Now the receiver which will receive this Intent

public class ActionReceiver extends BroadcastReceiver {

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

        //Toast.makeText(context,"recieved",Toast.LENGTH_SHORT).show();

        String action=intent.getStringExtra("action");
        if(action.equals("action1")){
            performAction1();
        }
        else if(action.equals("action2")){
            performAction2();

        }
        //This is used to close the notification tray
        Intent it = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
        context.sendBroadcast(it);
    }

    public void performAction1(){

    }

    public void performAction2(){

    }

}

Declare Broadcast Receiver in Manifest

<receiver android:name=".ActionReceiver" />

Hope it helps.

Edric
  • 24,639
  • 13
  • 81
  • 91
Prince Bansal
  • 1,635
  • 15
  • 24
  • Thank you, this makes a lot of sense. However, I'm a little confused as to why you passed `intentAction` into `addAction`. Would it be `pIntentlogin`? – Faizan Syed Dec 24 '16 at 12:04
  • Updated. My bad! :) – Prince Bansal Dec 24 '16 at 12:06
  • Thank you so much! – Faizan Syed Dec 24 '16 at 12:11
  • 11
    This should be the tutorial on Android Developers Guide. – Prime624 Jan 22 '19 at 05:21
  • This is an excellent answer, but one part needs a warning; adding an extra to the intent will cause confusion further down the road: "A common mistake: create multiple PendingIntent objects with Intents that only vary in their "extra" contents, expecting to get a different PendingIntent each time. This does not happen. If you use two Intent objects that are equivalent as per {@link Intent#filterEquals(Intent) Intent.filterEquals}, then you will get the same PendingIntent for both of them. There are two typical ways to deal with this..." – anthropic android Jul 15 '19 at 17:56
  • How can I access the `this` object of the `createNotif` method from the broadcast receiver? – Donald Duck Aug 29 '21 at 14:59
  • Moslty you'll be using firebaseExtender, which will have getBaseContext() @DonaldDuck – Nandha Kumar Oct 04 '21 at 15:51