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