Oreo - Starting A Service In The Foreground
I have created a service that tracks the device's location as it moves. The service is started in by an Activity that binds to it, and in this activity there is a 'Start Tracking'
Solution 1:
Try to change your startInForeground() method with this code:
privatevoidstartInForeground() {
IntentnotificationIntent=newIntent(this, WorkoutActivity.class);
PendingIntent pendingIntent=PendingIntent.getActivity(this,0,notificationIntent,0);
NotificationCompat.Builderbuilder=newNotificationCompat.Builder(this,NOTIFICATION_CHANNEL_ID)
.setSmallIcon(R.drawable.shsl_notification)
.setContentTitle("TEST")
.setContentText("HELLO")
.setTicker("TICKER")
.setContentIntent(pendingIntent);
Notification notification=builder.build();
if(Build.VERSION.SDK_INT>=26) {
NotificationChannelchannel=newNotificationChannel(NOTIFICATION_CHANNEL_ID, NOTIFICATION_CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT);
channel.setDescription(NOTIFICATION_CHANNEL_DESC);
NotificationManagernotificationManager= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.createNotificationChannel(channel);
}
startForeground(NOTIFICATION_ID, notification);
}
So, when your Android version is Oreo (or higher) notification channel will be created, otherwise not.
Solution 2:
Before show notification you have to create notification channel:
privatevoidcreateNotificationChannel() {
if (Build.VERSION_CODES.O <= Build.VERSION.SDK_INT) {
NotificationChannel notificationChannel =
new NotificationChannel(PRIMARY_CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT);
notificationManager.createNotificationChannel(notificationChannel);
}
}
and then change create notification builder
new Notification.Builder(context, PRIMARY_CHANNEL_ID)
Post a Comment for "Oreo - Starting A Service In The Foreground"