下面列出了android.app.NotificationChannel#setLockscreenVisibility ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static void checkAndCreateNotificationChannel(Context context) {
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.O) {
return;
}
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationChannel notificationChannel = notificationManager.getNotificationChannel("yourLocalWeather");
boolean createNotification = notificationChannel == null;
if (!createNotification &&
((notificationChannel.getImportance() == NotificationManager.IMPORTANCE_LOW) ||
(AppPreference.isVibrateEnabled(context) && (notificationChannel.getVibrationPattern() == null)))) {
notificationManager.deleteNotificationChannel("yourLocalWeather");
createNotification = true;
}
if (createNotification) {
NotificationChannel channel = new NotificationChannel("yourLocalWeather",
context.getString(R.string.notification_channel_name),
NotificationManager.IMPORTANCE_DEFAULT);
channel.setDescription(context.getString(R.string.notification_channel_description));
channel.setVibrationPattern(isVibrateEnabled(context));
channel.enableVibration(AppPreference.isVibrateEnabled(context));
channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
channel.setSound(null, null);
notificationManager.createNotificationChannel(channel);
}
}
/**
* Sets up notification channel.
*/
public void setupNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
final NotificationChannel notificationChannel = new NotificationChannel(
NOTIFICATION_CHANNEL_ID,
mContext.getText(R.string.notificationChannelName),
NotificationManager.IMPORTANCE_DEFAULT);
notificationChannel.setDescription(mContext.getString(R.string.notificationChannelDescription));
notificationChannel.setLightColor(Color.YELLOW);
notificationChannel.enableLights(true);
notificationChannel.enableVibration(false);
notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
final NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(mContext.NOTIFICATION_SERVICE);
notificationManager.createNotificationChannel(notificationChannel);
}
}
@RequiresApi(Build.VERSION_CODES.O)
private String createNotificationChannel()
{
String channelId = NOTIFICATION_CHANNEL_ID;
String channelName = getString(R.string.notificationChannelName);
NotificationChannel chan = new NotificationChannel(channelId,
channelName, NotificationManager.IMPORTANCE_LOW);
chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
NotificationManager service = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
if(service != null)
{
service.createNotificationChannel(chan);
}
else
{
Log.w(TAG, "Could not get NotificationManager");
}
return channelId;
}
@RequiresApi(Build.VERSION_CODES.O)
private String createNotificationChannel()
{
String channelId = NOTIFICATION_CHANNEL_ID;
String channelName = getString(R.string.notificationChannelName);
NotificationChannel chan = new NotificationChannel(channelId,
channelName, NotificationManager.IMPORTANCE_LOW);
chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
NotificationManager service = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
if(service != null)
{
service.createNotificationChannel(chan);
}
else
{
Log.w(TAG, "Could not get NotificationManager");
}
return channelId;
}
/**
* Init notification channels for android 8.0 and higher
*/
private String createNotificationChannel(Context context) {
// 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) {
int importance = NotificationManager.IMPORTANCE_LOW;
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, importance);
channel.setDescription(CHANNEL_DESCRIPTION);
channel.enableLights(true);
channel.setLightColor(Color.RED);
channel.setShowBadge(true);
channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
return CHANNEL_ID;
}
@RequiresApi(Build.VERSION_CODES.O)
public static String createSilentNotificationChannel(String channelId, String channelName, Context context){
NotificationChannel chan = new NotificationChannel(channelId,
channelName, NotificationManager.IMPORTANCE_NONE);
chan.setDescription("Silent channel");
chan.setSound(null,null);
chan.enableLights(false);
chan.setLightColor(Color.BLUE);
chan.enableVibration(false);
chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
NotificationManager service = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (service != null) {
service.createNotificationChannel(chan);
}
return channelId;
}
private void createNotifyChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
if (mNotificationManager != null) {
mNotificationManager.createNotificationChannelGroup(
new NotificationChannelGroup(mGroupId, mGroupName));
NotificationChannel channelMsg =
new NotificationChannel(mChannelMsgId, mChannelMsgName,
NotificationManager.IMPORTANCE_DEFAULT);
channelMsg.setDescription(mChannelMsgDes);
channelMsg.enableLights(true);
channelMsg.setLightColor(Color.BLUE);
channelMsg.enableVibration(false);
channelMsg.setVibrationPattern(new long[] { 100, 200, 300, 400 });
channelMsg.setShowBadge(true);
channelMsg.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
channelMsg.setGroup(mGroupId);
mNotificationManager.createNotificationChannel(channelMsg);
NotificationChannel channelService =
new NotificationChannel(mChannelServiceId, mChannelServiceName,
NotificationManager.IMPORTANCE_LOW);
channelService.setDescription(mChannelServiceDes);
channelService.enableLights(false);
channelService.enableVibration(false);
channelService.setShowBadge(false);
channelService.setSound(null, null);
channelService.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
channelService.setGroup(mGroupId);
mNotificationManager.createNotificationChannel(channelService);
}
}
}
private void showNotification() {
String channelId = "meshenger_service";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel chan = new NotificationChannel(channelId, "Meshenger Background Service", NotificationManager.IMPORTANCE_DEFAULT);
chan.setLightColor(Color.RED);
chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
NotificationManager service = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
service.createNotificationChannel(chan);
}
// start MainActivity
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent pendingNotificationIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
Context mActivity = getApplicationContext();
Notification notification = new NotificationCompat.Builder(mActivity, channelId)
.setOngoing(true)
.setSmallIcon(R.drawable.ic_logo)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.logo_small))
.setPriority(PRIORITY_MIN)
.setCategory(Notification.CATEGORY_SERVICE)
.setContentText(getResources().getText(R.string.listen_for_incoming_calls))
.setContentIntent(pendingNotificationIntent)
.build();
startForeground(NOTIFICATION, notification);
}
/**
* Method returns the Notification builder
* For Oreo devices we can have channels with HIGH and LOW importance.
* If highImportance is true builder will be created with HIGH priority
* For pre Oreo devices builder without channel will be returned
* @param highImportance true if we need HIGH channel, false if we need LOW
* @return
*/
private NotificationCompat.Builder getNotificationBuilder(boolean highImportance){
NotificationCompat.Builder builder;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
if (highImportance){
NotificationChannel channel = new NotificationChannel(PRIMARY_CHANNEL_ID, PRIMARY_CHANNEL, NotificationManager.IMPORTANCE_HIGH);
channel.setLightColor(Color.GREEN);
channel.enableLights(true);
channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
channel.enableVibration(true);
channel.setVibrationPattern(notificationVibrationPattern);
((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).createNotificationChannel(channel);
builder = new NotificationCompat.Builder(RCDevice.this, PRIMARY_CHANNEL_ID);
} else {
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
NotificationChannel notificationChannel = new NotificationChannel(DEFAULT_FOREGROUND_CHANNEL_ID, DEFAULT_FOREGROUND_CHANNEL, NotificationManager.IMPORTANCE_LOW);
notificationManager.createNotificationChannel(notificationChannel);
builder = new NotificationCompat.Builder(RCDevice.this, DEFAULT_FOREGROUND_CHANNEL_ID);
}
} else {
builder = new NotificationCompat.Builder(RCDevice.this);
}
return builder;
}
private String createNotificationChannel(
Context context, MockDatabase.MessagingStyleCommsAppData mockNotificationData) {
// NotificationChannels are required for Notifications on O (API 26) and above.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// The id of the channel.
String channelId = mockNotificationData.getChannelId();
// The user-visible name of the channel.
CharSequence channelName = mockNotificationData.getChannelName();
// The user-visible description of the channel.
String channelDescription = mockNotificationData.getChannelDescription();
int channelImportance = mockNotificationData.getChannelImportance();
boolean channelEnableVibrate = mockNotificationData.isChannelEnableVibrate();
int channelLockscreenVisibility =
mockNotificationData.getChannelLockscreenVisibility();
// Initializes NotificationChannel.
NotificationChannel notificationChannel =
new NotificationChannel(channelId, channelName, channelImportance);
notificationChannel.setDescription(channelDescription);
notificationChannel.enableVibration(channelEnableVibrate);
notificationChannel.setLockscreenVisibility(channelLockscreenVisibility);
// Adds NotificationChannel to system. Attempting to create an existing notification
// channel with its original values performs no operation, so it's safe to perform
// the below sequence.
NotificationManager notificationManager =
(NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.createNotificationChannel(notificationChannel);
return channelId;
} else {
// Returns null for pre-O (26) devices.
return null;
}
}
/**
* Creates Notification Channel used for Notifications on O (26) and higher.
*
* @param context
* @param mockNotificationData
* @return
*/
public static String createNotificationChannel(
Context context,
MockDatabase.MessagingStyleCommsAppData mockNotificationData) {
// NotificationChannels are required for Notifications on O (API 26) and above.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// The id of the channel.
String channelId = mockNotificationData.getChannelId();
// The user-visible name of the channel.
CharSequence channelName = mockNotificationData.getChannelName();
// The user-visible description of the channel.
String channelDescription = mockNotificationData.getChannelDescription();
int channelImportance = mockNotificationData.getChannelImportance();
boolean channelEnableVibrate = mockNotificationData.isChannelEnableVibrate();
int channelLockscreenVisibility =
mockNotificationData.getChannelLockscreenVisibility();
// Initializes NotificationChannel.
NotificationChannel notificationChannel =
new NotificationChannel(channelId, channelName, channelImportance);
notificationChannel.setDescription(channelDescription);
notificationChannel.enableVibration(channelEnableVibrate);
notificationChannel.setLockscreenVisibility(channelLockscreenVisibility);
// Adds NotificationChannel to system. Attempting to create an existing notification
// channel with its original values performs no operation, so it's safe to perform the
// below sequence.
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.createNotificationChannel(notificationChannel);
return channelId;
} else {
// Returns null for pre-O (26) devices.
return null;
}
}
@RequiresApi(api = Build.VERSION_CODES.O)
private static Notification buildForegroundNotification(Context context) {
NotificationChannel channel = new NotificationChannel("foreground-service", "Foreground Service", NotificationManager.IMPORTANCE_NONE);
channel.setLockscreenVisibility(Notification.VISIBILITY_SECRET);
channel.setShowBadge(false);
channel.setVibrationPattern(new long[0]);
context.getSystemService(NotificationManager.class).createNotificationChannel(channel);
return new Notification.Builder(context, channel.getId())
.setOngoing(true)
.setContentTitle("Running in background")
.setSmallIcon(R.drawable.gcm_bell)
.build();
}
@RequiresApi(Build.VERSION_CODES.O)
@NonNull
private String getNotificationChannelId() {
NotificationChannel channel = new NotificationChannel(TAG, getString(R.string.playback),
android.app.NotificationManager.IMPORTANCE_DEFAULT);
channel.enableLights(true);
channel.setLightColor(Color.BLUE);
channel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
android.app.NotificationManager notificationManager =
(android.app.NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Objects.requireNonNull(notificationManager).createNotificationChannel(channel);
return TAG;
}
private void createNotificationChannel(Options options) {
// 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) {
int importance = NotificationManager.IMPORTANCE_DEFAULT;
if(options.doesHeadsUp()) {
importance = NotificationManager.IMPORTANCE_HIGH;
} else if(!options.doesSound()) {
importance = NotificationManager.IMPORTANCE_LOW;
}
NotificationChannel channel = new NotificationChannel(options.getChannelId(), options.getChannelName(), importance);
if(options.doesHeadsUp()) {
channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
}
channel.setDescription(options.getChannelDescription());
if(options.doesSound() && options.getSoundUri() != null) {
AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setLegacyStreamType(android.media.AudioManager.STREAM_NOTIFICATION)
.build();
channel.setSound(options.getSoundUri(), audioAttributes);
}
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
NotificationManager notificationManager = this.context.getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}
/**
* Create notification channel for Android O
*/
@RequiresApi(Build.VERSION_CODES.O)
private void createChannel() {
final CharSequence name = getString(R.string.channel_name);
final int importance = NotificationManager.IMPORTANCE_LOW;
final NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
channel.setShowBadge(false);
channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
channel.enableVibration(false);
channel.enableLights(false);
channel.setBypassDnd(false);
final NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
nm.createNotificationChannel(channel);
}
/**
* Creates notification channel for Android API 26+
*/
private void createNotificationChannel(Context context) {
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(CHANNEL_ID,
context.getString(R.string.channel_name), NotificationManager.IMPORTANCE_HIGH);
channel.enableLights(true);
channel.enableVibration(true);
channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
if (manager != null) {
manager.createNotificationChannel(channel);
}
}
}
@RequiresApi(Build.VERSION_CODES.O)
private String createNotificationChannel(){
String channelId = "VBrowserNotification";
String channelName = "前台下载通知";
NotificationChannel chan = new NotificationChannel(channelId,
channelName, NotificationManager.IMPORTANCE_HIGH);
chan.setLightColor(Color.BLUE);
chan.setImportance(NotificationManager.IMPORTANCE_NONE);
chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
NotificationManager service = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
service.createNotificationChannel(chan);
return channelId;
}
private void createServiceChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
int priority = IMPORTANCE_DEFAULT;
if (getBooleanPreferences(getApplicationContext(), PREF_KCA_SET_PRIORITY)) {
priority = IMPORTANCE_HIGH;
}
NotificationChannel channel = new NotificationChannel(getServiceChannelId(),
SERVICE_CHANNEL_NAME, priority);
channel.enableVibration(false);
channel.setSound(null, null);
channel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
notifiManager.deleteNotificationChannel(SERVICE_CHANNEL_ID_OLD);
notifiManager.createNotificationChannel(channel);
}
}
@Override
public void updateNotificationChannel(String pkg, int uid, NotificationChannel updatedChannel,
boolean fromUser) {
Preconditions.checkNotNull(updatedChannel);
Preconditions.checkNotNull(updatedChannel.getId());
Record r = getOrCreateRecord(pkg, uid);
if (r == null) {
throw new IllegalArgumentException("Invalid package");
}
NotificationChannel channel = r.channels.get(updatedChannel.getId());
if (channel == null || channel.isDeleted()) {
throw new IllegalArgumentException("Channel does not exist");
}
if (updatedChannel.getLockscreenVisibility() == Notification.VISIBILITY_PUBLIC) {
updatedChannel.setLockscreenVisibility(Ranking.VISIBILITY_NO_OVERRIDE);
}
if (!fromUser) {
updatedChannel.unlockFields(updatedChannel.getUserLockedFields());
}
if (fromUser) {
updatedChannel.lockFields(channel.getUserLockedFields());
lockFieldsForUpdate(channel, updatedChannel);
}
r.channels.put(updatedChannel.getId(), updatedChannel);
if (NotificationChannel.DEFAULT_CHANNEL_ID.equals(updatedChannel.getId())) {
// copy settings to app level so they are inherited by new channels
// when the app migrates
r.importance = updatedChannel.getImportance();
r.priority = updatedChannel.canBypassDnd()
? Notification.PRIORITY_MAX : Notification.PRIORITY_DEFAULT;
r.visibility = updatedChannel.getLockscreenVisibility();
r.showBadge = updatedChannel.canShowBadge();
}
if (!channel.equals(updatedChannel)) {
// only log if there are real changes
MetricsLogger.action(getChannelLog(updatedChannel, pkg));
}
if (updatedChannel.canBypassDnd() != mAreChannelsBypassingDnd) {
updateChannelsBypassingDnd();
}
updateConfig();
}
/**
* Creation of notification on operations completed
*/
public NotificationBuilder createNotification(String title, String ringtone, boolean vibrate, PendingIntent notifyIntent) {
NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
mNotificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID,
title, NotificationManager.IMPORTANCE_HIGH);
// Configure the notification channel.
mNotificationChannel.setDescription("beacon location notification channel");
mNotificationChannel.enableLights(true);
if (vibrate) {
mNotificationChannel.setVibrationPattern(mVibrate_pattern);
mNotificationChannel.enableVibration(true);
} else {
mNotificationChannel.enableVibration(false);
}
mNotificationChannel.setLightColor(Color.YELLOW);
mNotificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
mNotificationChannel.setSound(null, null);
/* if (ringtone != null) {
AudioAttributes att = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.build();
mNotificationChannel.setSound(Uri.parse(ringtone), att);
} else {
mNotificationChannel.setSound(null, null);
}
*/
notificationManager.createNotificationChannel(mNotificationChannel);
}
mBuilder = new NotificationCompat.Builder(mContext, NOTIFICATION_CHANNEL_ID);
mBuilder.setAutoCancel(true)
.setDefaults(vibrate?(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE):Notification.DEFAULT_LIGHTS)
.setSmallIcon(getNotificationSmallIcon())
.setContentTitle(title)
.setColor(ContextCompat.getColor(mContext, R.color.hn_orange));
setRingtone(ringtone);
if (notifyIntent != null) {
mBuilder.setContentIntent(notifyIntent);
}
setLargeIcon(getNotificationLargeIcon());
return this;
}