Even if I'm using channels for those apis, I have a foreground service which seems not to be able to send not ongoing notifications on api > 26 (oreo). On api below, the notification shows correctly. Below is my code which send notifications :
private void notification(String content) {
Random random = new Random();
int id = random.nextInt(1000)+1;
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O) {
NotificationChannel chan = new NotificationChannel(STANDARD_NOTIFICATION_CHANNEL_ID, standardChannelName, 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(this, STANDARD_NOTIFICATION_CHANNEL_ID);
Notification notification = notificationBuilder.setOngoing(false)
.setSmallIcon(android.R.drawable.btn_dropdown)
.setContentTitle(content)
.setPriority(NotificationManager.IMPORTANCE_DEFAULT)
.setCategory(Notification.CATEGORY_SERVICE)
.build();
// TODO we need to see how to send not ongoing notifications in our case with API > 26
manager.notify(id,notification);
}
else {
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, STANDARD_NOTIFICATION_CHANNEL_ID);
Notification notification = notificationBuilder.setOngoing(false)
.setSmallIcon(R.drawable.ic_launcher_fury)
.setContentTitle(content)
.build();
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(id,notification);
}
}
The only notification which is correctly displayed is the ongoing notification which shows to the user that the service is in foreground. I call it like this :
@Override
public void onCreate() {
super.onCreate();
Random random = new Random();
int id = random.nextInt(1000);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startMyOwnForeground();
}
else {
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, FOREGROUND_NOTIFICATION_CHANNEL_ID);
Notification notification = notificationBuilder.setOngoing(true)
.setSmallIcon(R.drawable.ic_launcher_fury)
.setContentTitle(getResources().getString(R.string.app_is_in_background))
.build();
startForeground(id, notification);
}
}
@RequiresApi(Build.VERSION_CODES.O)
private void startMyOwnForeground()
{
Random random = new Random();
int id = random.nextInt(1000);
NotificationChannel chan = new NotificationChannel(FOREGROUND_NOTIFICATION_CHANNEL_ID, foregroundChannelName, 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(this, FOREGROUND_NOTIFICATION_CHANNEL_ID);
Notification notification = notificationBuilder.setOngoing(true)
.setSmallIcon(android.R.drawable.btn_dropdown)
.setContentTitle(getResources().getString(R.string.app_is_in_background))
.setPriority(NotificationManager.IMPORTANCE_MIN)
.setCategory(Notification.CATEGORY_SERVICE)
.build();
startForeground(id, notification);
}
Do you have some clues ?
I guess your problem is here:
NotificationChannel chan = new NotificationChannel(FOREGROUND_NOTIFICATION_CHANNEL_ID, foregroundChannelName, NotificationManager.IMPORTANCE_NONE);
According to the JavaDoc of NotificationManager.IMPORTANCE_NONE
/** * A notification with no importance: does not show in the shade. */
So, try to use a different importance level such as: NotificationManager.IMPORTANCE_DEFAULT
NotificationChannel chan = new NotificationChannel(FOREGROUND_NOTIFICATION_CHANNEL_ID, foregroundChannelName, NotificationManager.IMPORTANCE_DEFAULT);