Search code examples
androidserviceonclickforeground

How to press ON a foreground service Notification?


I have a foreground service notification and i want when the user closes the app and presses the notification to open the application again.I implemented an onclicklistener with onClick() method but it did nothing.If that is not possible i want the "Play" button to be shown in the notification because it is only being shown when i expand the notification.

Here is the service class:

public class MyForeGroundService extends Service {

public MyForeGroundService() {
}

@Override
public IBinder onBind(Intent intent) {
    // TODO: Return the communication channel to the service.
    throw new UnsupportedOperationException("Not yet implemented");
}

@Override
public void onCreate() {
    super.onCreate();
    Log.d(TAG_FOREGROUND_SERVICE, "My foreground service onCreate().");
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    if (intent != null) {
        String action = intent.getAction();

        switch (action) {
            case ACTION_START_FOREGROUND_SERVICE:
                startForegroundService();
                Toast.makeText(getApplicationContext(), "Foreground service is started.", Toast.LENGTH_LONG).show();
                break;
            case ACTION_STOP_FOREGROUND_SERVICE:
                stopForegroundService();
                Toast.makeText(getApplicationContext(), "Foreground service is stopped.", Toast.LENGTH_LONG).show();
                break;
            case ACTION_PLAY:
                Toast.makeText(getApplicationContext(), "You click Play button.", Toast.LENGTH_LONG).show();
                break;
            case ACTION_PAUSE:
                Toast.makeText(getApplicationContext(), "You click Pause button.", Toast.LENGTH_LONG).show();
                break;
        }
    }
    return super.onStartCommand(intent, flags, startId);
}

/* Used to build and start foreground service. */
private void startForegroundService() {
    Log.d(TAG_FOREGROUND_SERVICE, "Start foreground service.");

    // Create notification default intent.
    Intent intent = new Intent();
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);

    // Create notification builder.
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);

    // Make notification show big text.
    NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle();
    bigTextStyle.setBigContentTitle("Music player implemented by foreground service.");
    bigTextStyle.bigText("Android foreground service is a android service which can run in foreground always, it can be controlled by user via notification.");
    // Set big text style.
    builder.setStyle(bigTextStyle);

    builder.setWhen(System.currentTimeMillis());
    builder.setSmallIcon(R.mipmap.ic_launcher);
    Bitmap largeIconBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.strava);
    builder.setLargeIcon(largeIconBitmap);
    // Make the notification max priority.
    builder.setPriority(Notification.PRIORITY_MAX);
    // Make head-up notification.
    builder.setFullScreenIntent(pendingIntent, true);

    // Add Play button intent in notification.
    Intent playIntent = new Intent(this, MyForeGroundService.class);
    playIntent.setAction(ACTION_PLAY);
    PendingIntent pendingPlayIntent = PendingIntent.getService(this, 0, playIntent, 0);
    NotificationCompat.Action playAction = new NotificationCompat.Action(android.R.drawable.ic_media_play, "Play", pendingPlayIntent);
    builder.addAction(playAction);

    // Build the notification.
    Notification notification = builder.build();

    // Start foreground service.
    startForeground(1, notification);
}

private void stopForegroundService() {
    Log.d(TAG_FOREGROUND_SERVICE, "Stop foreground service.");

    // Stop foreground service and remove the notification.
    stopForeground(true);

    // Stop the foreground service.
    stopSelf();
}

}

Solution

  • So to add an "onClick" effect to a notification you should use the ".setContentIntent(pendingIntent)" method in the NotificationCompat.Builder.

    For the play button to always be visible is a little bit more tricky from what I read, some say that "setPriority(Notification.PRIORITY_MAX) + setWhen(0)" can solve this, others say that it depends on device and if you have other notifications, are you connected to USB etc and that there is no single working solution for this.