Foreground service notification takes a few seconds to appear/show up
Asked Answered
A

1

9

Foreground service notification shows too slowly on Android 12. Used ContextCompat.startForegroundService(...) and mContext.startForegroundService(...). It still shows in 5-10 seconds.

Here is an example of my code:

private void createNotificationChannel() {
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
      NotificationChannel channel = new NotificationChannel(CHANNEL_ID, "Counting steps", NotificationManager.IMPORTANCE_DEFAULT);
      channel.enableVibration(false);
      channel.setSound(null, null);
      channel.setShowBadge(false);
      notificationManager.createNotificationChannel(channel);
  }
}

The onStartCommand method:

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

      String input = intent.getStringExtra("numberOfSteps");
        createNotificationChannel();

        Intent notificationIntent = new Intent(this, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this,
          0, notificationIntent, PendingIntent.FLAG_IMMUTABLE);

    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
      Notification.Builder notificationBuilder = new Notification.Builder(this, CHANNEL_ID)
        .setContentTitle("Counting steps")
        .setContentText(input)
        .setSmallIcon(R.drawable.ic_baseline_directions_walk_24)
        .setContentIntent(pendingIntent);


      startForeground(FOREGROUND_ID, notificationBuilder.build());
    }

        return START_STICKY;
    }

How can I start or show a foreground service notification quickly?

Arc answered 21/10, 2022 at 9:55 Comment(0)
A
8

Services that show a notification immediately

If a foreground service has at least one of the following characteristics, the system shows the associated notification immediately after the service starts, even on devices that run Android 12 or higher:

  • The service is associated with a notification that includes action buttons.
  • The service has a foregroundServiceType of mediaPlayback, mediaProjection, or phoneCall.
  • The service provides a use case related to phone calls, navigation, or media playback, as defined in the notification's category attribute.
  • The service has opted out of the behavior change by passing FOREGROUND_SERVICE_IMMEDIATE into setForegroundServiceBehavior() when setting up the notification.
  • On Android 13 (API level 33) or higher, if the user denies the notification permission, they still see notices related to foreground services in the Foreground Services (FGS) Task Manager but don't see them in the notification drawer.

    See: Services that show a notification immediately

    Java example code:

    Notification.Builder notificationBuilder = new Notification.Builder(this, CHANNEL_ID)
              .setContentTitle("Counting steps")
              .setContentText(message)
              .setSmallIcon(R.drawable.your_cool_icon)
              .setContentIntent(pendingIntent);
    
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) {
              notificationBuilder.setForegroundServiceBehavior(Notification.FOREGROUND_SERVICE_IMMEDIATE);
            }
    

    Kotlin example:

    val notificationBuilder: Notification.Builder =
                Notification.Builder(this, CHANNEL_ID)
                    .setContentTitle("Notification title")
                    .setContentText("Content title")
                    .setSmallIcon(R.drawable.your_cool_icon)
                    .setContentIntent(pendingIntent)
    
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
                notificationBuilder.setForegroundServiceBehavior(Notification.FOREGROUND_SERVICE_IMMEDIATE)
            }
    
    Arc answered 21/10, 2022 at 9:55 Comment(0)

    © 2022 - 2024 — McMap. All rights reserved.