private Notification buildNotification() {
String channelId;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
channelId = "GaoDeLocationService";
String channelName = "GaoDeLocationService";
createNotificationChannel(channelId, channelName);
} else {
channelId = "";
}
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, channelId);
NotificationCompat.Builder builder =
notificationBuilder.setOngoing(true)
.setSmallIcon(R.drawable.icon_vas_logo)
.setVibrate(null)
.setSound(null)
.setContentTitle("大虾师傅")
.setContentText("正在后台运行")
.setVisibility(NotificationCompat.VISIBILITY_SECRET)
.setPriority(NotificationCompat.PRIORITY_MAX);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
builder.setCategory(Notification.CATEGORY_SERVICE);
}
return builder.build();
}
@RequiresApi(Build.VERSION_CODES.O)
private void createNotificationChannel(String channelId, String channelName) {
NotificationManager service = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (service == null || service.getNotificationChannel(channelId) != null) {
return;
}
NotificationChannel chan = new NotificationChannel(channelId,
channelName, NotificationManager.IMPORTANCE_NONE);
chan.setLightColor(Color.BLUE);
chan.enableVibration(false);
chan.setVibrationPattern(new long[]{0});
chan.setSound(null, null);
chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
service.createNotificationChannel(chan);
}
网友评论