即使我为这些 api 使用通道,我也有一个前台服务,它似乎无法在 api > 26 (oreo) 上发送不正在进行的通知。在下面的 api 上,通知显示正确。以下是我发送通知的代码:
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);
}
}
唯一正确显示的通知是向用户显示服务处于前台的持续通知。我这样称呼它:
@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);
}
你有一些线索吗?