我startForeground的id设置为0后,一开始是可以探出通知的,但是后来就没反应了。如果把0改成其他数字,startForeground是好用的,有人知道是什么原因吗?
createNotificationChanel("alarm");
Calendar calendar=Calendar.getInstance();
NotificationManager notificationManager=(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.cancelAll();
Notification notification=new NotificationCompat.Builder(this,"alarm")
.setSmallIcon(R.mipmap.ic_launcher_round)
.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher_round))
.setContentTitle("闹钟")
.setContentText("7:00")
.setSound(Uri.parse("android.resource://"+getApplicationContext().getPackageName()+
"/"+R.raw.bell))
.setPriority(NotificationCompat.PRIORITY_MAX)
.setWhen(calendar.getTimeInMillis())
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.build();
LogInfo.d("****startForeground ");
startForeground(0,notification);
createNotificationChanel的定义如下
private void createNotificationChanel( String chanelID){
LogInfo.d("createNotificationChanel start");
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
//创建通知渠道的代码只在第一次执行的时候才会创建,以后每次执行创建代码系统会检测到该通知渠道已经存在了,因此不会重复创建
if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){
String channelName="闹钟";
String description="允许闹钟响应";
int importance= NotificationManager.IMPORTANCE_HIGH;
NotificationChannel channel=new NotificationChannel(chanelID,channelName,importance);
channel.setDescription(description);
NotificationManager notificationManager=getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}