I created a service. And I started this service foreground, but when I lunch my application, An application is crashed. I added startServiceOreoCondition I read about this but it does not help me
@Override
public void onCreate() {
mdb = ManagerDB.getInstance(getApplicationContext(), "name");
context = getApplicationContext();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
nm.createNotificationChannel(new NotificationChannel(NOTIFICATION_CHANNEL_ID_SERVICE, "*****", NotificationManager.IMPORTANCE_DEFAULT));
nm.createNotificationChannel(new NotificationChannel(NOTIFICATION_CHANNEL_ID_INFO, "********", NotificationManager.IMPORTANCE_DEFAULT));
}
startServiceOreoCondition();
sp = getSharedPreferences("pfref", Activity.MODE_PRIVATE);
editor = sp.edit();
executeSomething();
timer = new Timer();
}
private void startServiceOreoCondition() {
if (Build.VERSION.SDK_INT >= 26) {
String NOTIFICATION_CHANNEL_ID = "pl.***.***";
String channelName = "Communication Service";
NotificationChannel chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_NONE);
chan.setLightColor(Color.BLUE);
chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
assert manager != null;
manager.createNotificationChannel(chan);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context);
Notification notification = notificationBuilder.setOngoing(true)
.setContentTitle(context.getString(R.string.app_name))
.setPriority(NotificationManager.IMPORTANCE_MIN)
.setCategory(Notification.CATEGORY_SERVICE)
.build();
startForeground(4, notification);
}else{
startForeground(3, new Notification());
}
}
But When I lounch this application on andorid 8.1 i see this error :
android.app.RemoteServiceException: Bad notification for startForeground: java.lang.RuntimeException: invalid channel for service notification: Notification(channel=null pri=1 contentView=null vibrate=null sound=null defaults=0x0 flags=0x42 color=0x00000000 category=service vis=PRIVATE)
And also I added this permission :
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
NotificationChannel chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_NONE);
chan.setDescription(<Add channel Description>);
You have just created Notification Channel object But you need to create channel using channel object.
nm.createNotificationChannel(new NotificationChannel(NOTIFICATION_CHANNEL_ID, "********", NotificationManager.IMPORTANCE_DEFAULT));
And while creating Notification Builder pass the created channel ID.
NotificationCompat.Builder notificationBuilder = new
NotificationCompat.Builder(context,NOTIFICATION_CHANNEL_ID);
Notification notification = notificationBuilder.setOngoing(true)
.setContentTitle(context.getString(R.string.app_name))
.setPriority(NotificationManager.IMPORTANCE_MIN)
.setCategory(Notification.CATEGORY_SERVICE)
.build();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With