Мне нужно запустить службу приложений на переднем плане.Мой код отлично работает с API уровня 26 и новее, но не со старым уровнем API.В более старой версии служба отображается в запущенной службе, но не отправляет начальное уведомление.Что мне нужно изменить в своем коде, почему не работает?
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());
}
}
Запуск службы
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));
}