简体   繁体   中英

I am trying to start Foreground service on older API version. Work on APIs 26+

I need to start my app service foreground. My code work fine with API level 26 and newer, but not with older APIs level. On older version Service is shown in running service but don't send the starting notification. What I need to change to my code, why isn't working?

public void onCreate() {
        super.onCreate();
        messageIntent.setAction(getString(R.string.receiver_receive));



        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID_DEFAULT)
                .setOngoing(false).setSmallIcon(R.drawable.ic_launcher).setPriority(Notification.PRIORITY_MIN);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID_DEFAULT,
                    NOTIFICATION_CHANNEL_ID_DEFAULT, NotificationManager.IMPORTANCE_LOW);
            notificationChannel.setDescription(NOTIFICATION_CHANNEL_ID_DEFAULT);
            notificationChannel.setSound(null, null);
            notificationManager.createNotificationChannel(notificationChannel);
            startForeground(1, builder.build());
        }

    }

Starting the Service

protected void onStart() {
        super.onStart();
        // Bind to LocalService
        Intent intent = new Intent(this, SocketService.class);
        bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
            ContextCompat.startForegroundService(this, new Intent(this, SocketService.class));
        else
            this.startService(new Intent(this, SocketService.class));
    }

start service for below apis in else statement.

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
    // only for newer versions
      Intent pushIntent = new Intent(this, ClassName.class);
      startForegroundService(pushIntent);
} else {
      Intent pushIntent = new Intent(this, ClassName.class);
      startService(pushIntent);
}

Method ContextCompat.startForegroundService(...) already contain if block:

public static void startForegroundService(@NonNull Context context, @NonNull Intent intent) {
    if (Build.VERSION.SDK_INT >= 26) {
        context.startForegroundService(intent);
    } else {
        // Pre-O behavior.
        context.startService(intent);
    }
}

Just call ContextCompat.startForegroundService(this, pushIntent) .

And you need to move startForeground outside if block:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    ...        
}
startForeground(1, builder.build());

Also, don't forget to call notificationManagerCompat.notify(NOTIFICATION_ID, notification) on older APIs (if notification doesn't show automatically). NOTIFICATION_ID must be NOT ZERO!!!

Write code for devices with api lower than 26 same as written in if statement excluding the part of notification channel because notification channel is not supported in devices with version lower than 26

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        // you had written code for device version greater than O
    } else {
       //You need to write code for device with lower version here
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM