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));
}
from I am trying to start Foreground service on older API version. Work on APIs 26+
No comments:
Post a Comment