0

I have a Flutter app, which runs a never ending foreground service and it is defined like this:

    private void startForeground() {
    Intent notificationIntent = new Intent(this, MainActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
            notificationIntent, 0);
    startForeground(NOTIF_ID, new NotificationCompat.Builder(this,
            NOTIF_CHANNEL_ID)
            .setOngoing(true)
            .setContentTitle("service")
            .setContentText("Service is running background")
            .setContentIntent(pendingIntent)
            .build());
}

and I start service like this:

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
    Notification notification = notificationBuilder.setOngoing(true)
            .setSmallIcon(R.drawable.smile)
            .setContentTitle("App is running in background")
            .setPriority(NotificationManager.IMPORTANCE_MIN)
            .setCategory(Notification.CATEGORY_SERVICE)
            .build();
    startForeground(2, notification);

My foreground service is showing notification all the time.

Is it possible to start a Flutter app when user press on notification which is showed by foreground service, even if the Flutter app isn't running in background? Thanks.

Jure
  • 78
  • 5

1 Answers1

1

To launch an Activity when clicking on a notification, you can use the PendingIntent. You already have this implemented in the first sample, but here are some links. Android docs Stackoverflow question

Intent notificationIntent = new Intent(this, MainActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
            notificationIntent, 0);
builder.setContentIntent(pendingIntent);
medyas
  • 1,166
  • 13
  • 21