How can I know which button clicked on it inside notification?

Viewed 67

I have three buttons in each notification (the main notification button, the timer button, and the disable button).

  • The main notification button runs the foreground service
  • The timer button runs the foreground service, but shows the toast message
  • The disable button removes notification from status bar

Is there a way to use one foreground service for all three buttons? If so, how do I determine which button was clicked within the service? Or must I create three foreground services for each button?

My Code

PendingIntent pendingIntentMain = PendingIntent.getService(context, 0, new Intent(context, ForegroundService.class).putExtra("main", "a"), PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent pendingIntentTimer = PendingIntent.getService(context, 0, new Intent(context, ForegroundService.class).putExtra("timer", "b"), PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent pendingIntentDisable = PendingIntent.getService(context, 0, new Intent(context, ForegroundService.class).putExtra("disable", "c"), PendingIntent.FLAG_UPDATE_CURRENT);

NotificationCompat.Builder builder = new NotificationCompat.Builder(context, String.valueOf(NOTIFICATION_ID))
    .addAction(R.mipmap.ic_launcher, context.getString(R.string.timer), pendingIntentTimer)
    .addAction(R.mipmap.ic_launcher, context.getString(R.string.disable), pendingIntentDisable)
    .setContentIntent(pendingIntentMain)
    .setOngoing(true)
    .setAutoCancel(false)
    .setShowWhen(false)
    .setSmallIcon(R.mipmap.ic_launcher)
    .setContentTitle(context.getString(R.string.app_name))
    .setContentText(context.getString(R.string.tap_to_enable_service))
    .setPriority(NotificationCompat.PRIORITY_DEFAULT);

Foreground Service

public class ForegroundService extends Service {

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.w("ABC", "" + intent.getStringExtra("main"));
        Log.w("ABC", "" + intent.getStringExtra("timer"));
        Log.w("ABC", "" + intent.getStringExtra("disable"));
        Log.w("ABC", "" + intent.getExtras());
        return super.onStartCommand(intent, flags, startId);
    }

}

The Log.w inside onStartCommand shows nothing

1 Answers

No need to create different Foreground Services, You can use Intent Action instead of passing intent with data. Like,

Intent intent = new Intent(this , ClipTextObserverService.class);
intent.setAction("specify_name_of_the_action")

PendingIntent.getService(
            this,
            1464,
            intent,
            PendingIntent.FLAG_UPDATE_CURRENT
        );

Change the Flag accordingly. Pass this pendingIntent in addAction() method. Then Check for the action in onStartCommand() method of Service.Like,

@Override
public int onStartCommand(Intent Intent , Int flags , Int startId) {

switch (intent.getAction()) {
 case "your_action_name":
    // TODO == Perform your action here.
    break;
}

return super.onStartCommand(intent, flags , startId);
}
Related