下面列出了android.app.NotificationChannel#setLightColor ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private static void createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationManager mNotificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
// 通知渠道的id
String id = "1";
// 用户可以看到的通知渠道的名字.
CharSequence name = "notification channel";
// 用户可以看到的通知渠道的描述
String description = "notification description";
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel mChannel = new NotificationChannel(id, name, importance);
// 配置通知渠道的属性
mChannel.setDescription(description);
// 设置通知出现时的闪灯(如果 android 设备支持的话)
mChannel.enableLights(true);
mChannel.setLightColor(Color.RED);
// 设置通知出现时的震动(如果 android 设备支持的话)
mChannel.enableVibration(true);
mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
//最后在notificationmanager中创建该通知渠道
mNotificationManager.createNotificationChannel(mChannel);
}
}
@Override
public void onCreate() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(
GeometricWeather.NOTIFICATION_CHANNEL_ID_BACKGROUND,
GeometricWeather.getNotificationChannelName(
this, GeometricWeather.NOTIFICATION_CHANNEL_ID_BACKGROUND),
NotificationManager.IMPORTANCE_MIN
);
channel.setShowBadge(false);
channel.setLightColor(ContextCompat.getColor(this, R.color.colorPrimary));
NotificationManagerCompat.from(this).createNotificationChannel(channel);
}
// version O.
startForeground(
getForegroundNotificationId(),
getForegroundNotification(
1,
DatabaseHelper.getInstance(this).countLocation()
).build()
);
super.onCreate();
}
/**
* 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);
}
}
private void createchannel() {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
NotificationChannel mChannel = new NotificationChannel(id,
getString(R.string.channel_name), //name of the channel
NotificationManager.IMPORTANCE_DEFAULT); //importance level
//important level: default is is high on the phone. high is urgent on the phone. low is medium, so none is low?
// Configure the notification channel.
mChannel.setDescription(getString(R.string.channel_description));
mChannel.enableLights(true);
// Sets the notification light color for notifications posted to this channel, if the device supports this feature.
mChannel.setLightColor(Color.RED);
mChannel.enableVibration(true);
mChannel.setShowBadge(true);
mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
nm.createNotificationChannel(mChannel);
}
}
@TargetApi(O)
public void createVoidVpnNotificationChannel() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
return;
}
// Connection status change messages
CharSequence name = context.getString(R.string.channel_name_status);
NotificationChannel mChannel = new NotificationChannel(VoidVpnService.NOTIFICATION_CHANNEL_NEWSTATUS_ID,
name, NotificationManager.IMPORTANCE_DEFAULT);
mChannel.setDescription(context.getString(R.string.channel_description_status));
mChannel.enableLights(true);
mChannel.setLightColor(Color.BLUE);
mChannel.setSound(null, null);
notificationManager.createNotificationChannel(mChannel);
}
public static void createMessageChannel(Context context) {
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
// Create message notification channel
String id = context.getString(R.string.notification_channel_id);
String name = context.getString(R.string.content_title_notification);
String description = context.getString(R.string.content_title_notification);
NotificationChannel channel =
new NotificationChannel(id, name, NotificationManager.IMPORTANCE_HIGH);
channel.setDescription(description);
channel.setLightColor(context.getColor(R.color.notification_led_color));
channel.enableLights(true);
channel.enableVibration(true);
channel.setShowBadge(true);
notificationManager.createNotificationChannel(channel);
}
private void buildNotification() {
manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
builder =
createNotification(
this,
getString(R.string.update_app_model_prepare),
getString(R.string.update_app_model_prepare),
downloadNotificationFlag);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel notificationChannel =
new NotificationChannel(channelId, getApplicationName(), importance);
notificationChannel.enableLights(false);
notificationChannel.setLightColor(Color.GREEN);
notificationChannel.enableVibration(false);
notificationChannel.setSound(null, null);
manager.createNotificationChannel(notificationChannel);
}
manager.notify(notifyId, builder.build());
}
@TargetApi(26)
private void initChannels(Context context) {
if(BuildCompat.isAtLeastR()) {
return;
}
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
String channelId = NEW_VIDEOS_NOTIFICATION_CHANNEL;
CharSequence channelName = context.getString(R.string.notification_channel_feed_title);
int importance = NotificationManager.IMPORTANCE_LOW;
NotificationChannel notificationChannel = new NotificationChannel(channelId, channelName, importance);
notificationChannel.enableLights(true);
notificationChannel.setLightColor(ColorUtils.compositeColors(0xFFFF0000, 0xFFFF0000));
notificationChannel.enableVibration(false);
notificationManager.createNotificationChannel(notificationChannel);
}
private void createNotificationChannel() {
mNotifyManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel(PRIMARY_CHANNEL_ID, getResources().getString(R.string.channel_name), NotificationManager.IMPORTANCE_HIGH);
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.enableVibration(true);
notificationChannel.setDescription(getResources().getString(R.string.channel_desc));
mNotifyManager.createNotificationChannel(notificationChannel);
}
}
@RequiresApi(Build.VERSION_CODES.O)
private void initNotificationChannel(String channelId, String channelName) {
NotificationChannel channel = new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_NONE);
channel.setLightColor(Color.GRAY);
channel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (notificationManager != null) {
notificationManager.createNotificationChannel(channel);
}
}
@RequiresApi(api = Build.VERSION_CODES.O)
private String createNotificationChannel(String channelId, String channelName){
NotificationChannel chan = new NotificationChannel(channelId,
channelName, NotificationManager.IMPORTANCE_NONE);
chan.setLightColor(Color.BLUE);
chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
NotificationManager service = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
service.createNotificationChannel(chan);
return channelId;
}
@TargetApi(26)
private void createNotificationChannels() {
List<NotificationChannel> notificationChannels = new ArrayList<>();
NotificationChannel recordingNotificationChannel = new NotificationChannel(
Const.RECORDING_NOTIFICATION_CHANNEL_ID,
Const.RECORDING_NOTIFICATION_CHANNEL_NAME,
NotificationManager.IMPORTANCE_DEFAULT
);
recordingNotificationChannel.enableLights(true);
recordingNotificationChannel.setLightColor(Color.RED);
recordingNotificationChannel.setShowBadge(true);
recordingNotificationChannel.enableVibration(true);
recordingNotificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
notificationChannels.add(recordingNotificationChannel);
NotificationChannel shareNotificationChannel = new NotificationChannel(
Const.SHARE_NOTIFICATION_CHANNEL_ID,
Const.SHARE_NOTIFICATION_CHANNEL_NAME,
NotificationManager.IMPORTANCE_DEFAULT
);
shareNotificationChannel.enableLights(true);
shareNotificationChannel.setLightColor(Color.RED);
shareNotificationChannel.setShowBadge(true);
shareNotificationChannel.enableVibration(true);
shareNotificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
notificationChannels.add(shareNotificationChannel);
getManager().createNotificationChannels(notificationChannels);
}
@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 String buildChannel() {
String channelId = "";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
String channelName = getString(R.string.adamant_default_notification_channel);
AudioAttributes attributes = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.build();
NotificationChannel chan = new NotificationChannel(ADAMANT_DEFAULT_NOTIFICATION_CHANNEL_ID,
channelName, NotificationManager.IMPORTANCE_NONE);
chan.setLightColor(Color.RED);
chan.enableLights(true);
chan.enableVibration(true);
chan.setVibrationPattern(NotificationHelper.VIBRATE_PATTERN);
chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
chan.setSound(NotificationHelper.SOUND_URI, attributes);
NotificationManager service = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
if (service != null) {
service.createNotificationChannel(chan);
channelId = ADAMANT_DEFAULT_NOTIFICATION_CHANNEL_ID;
}
}
return channelId;
}
private void createNotificationChannel() {
mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mChannelID = getPackageName() + ".channel_01";
// The user-visible name of the channel.
CharSequence name = getString(R.string.channel_name);
// The user-visible description of the channel.
String description = getString(R.string.channel_description);
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel mChannel = new NotificationChannel(mChannelID, name, importance);
// Configure the notification channel.
mChannel.setDescription(description);
mChannel.enableLights(true);
// Sets the notification light color for notifications posted to this
// channel, if the device supports this feature.
mChannel.setLightColor(Color.RED);
mChannel.enableVibration(true);
mChannel.setVibrationPattern(mVibratePattern);
//Uri soundURI = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.alarm_rooster);
mChannel.setSound(soundURI, (new AudioAttributes.Builder()).setUsage(AudioAttributes.USAGE_NOTIFICATION).build());
mNotificationManager.createNotificationChannel(mChannel);
}
private void notify(CallInvite callInvite, int notificationId) {
String callSid = callInvite.getCallSid();
Notification notification = null;
Intent intent = getPackageManager().getLaunchIntentForPackage(getPackageName());
intent.setAction(TwilioVoicePlugin.ACTION_INCOMING_CALL);
intent.putExtra(TwilioVoicePlugin.INCOMING_CALL_NOTIFICATION_ID, notificationId);
intent.putExtra(TwilioVoicePlugin.INCOMING_CALL_INVITE, callInvite);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent =
PendingIntent.getActivity(this, notificationId, intent, PendingIntent.FLAG_CANCEL_CURRENT);
Bundle extras = new Bundle();
extras.putInt(NOTIFICATION_ID_KEY, notificationId);
extras.putString(CALL_SID_KEY, callSid);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel callInviteChannel = new NotificationChannel(VOICE_CHANNEL,
"Primary Voice Channel", NotificationManager.IMPORTANCE_DEFAULT);
callInviteChannel.setLightColor(Color.RED);
callInviteChannel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
notificationManager.createNotificationChannel(callInviteChannel);
notification = buildNotification(callInvite.getFrom() + " is calling", pendingIntent, extras);
notificationManager.notify(notificationId, notification);
} else {
int iconIdentifier = getResources().getIdentifier("icon", "mipmap", getPackageName());
int incomingCallAppNameId = (int) getResources().getIdentifier("incoming_call_app_name", "string", getPackageName());
String contentTitle = getString(incomingCallAppNameId);
if (contentTitle == null) {
contentTitle = "Incoming Call";
}
final String from = callInvite.getFrom() + " is calling";
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(iconIdentifier)
.setContentTitle(contentTitle)
.setContentText(from)
.setAutoCancel(true)
.setExtras(extras)
.setContentIntent(pendingIntent)
.setGroup("voice_app_notification")
.setColor(Color.rgb(225, 0, 0));
notificationManager.notify(notificationId, notificationBuilder.build());
}
}
@RequiresApi(api = Build.VERSION_CODES.O)
public static void generateNotificationChannel(Context context) {
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationChannel channelEvent = new NotificationChannel(
context.getString(R.string.notifications_events_unique_id),
context.getString(R.string.notifications_events_channel_name),
NotificationManager.IMPORTANCE_DEFAULT);
// Configure the notification channel.
channelEvent.setDescription(context.getString(R.string.notifications_events_channel_description));
channelEvent.enableLights(true);
// Sets the notification light color for notifications posted to this
// channel, if the device supports this feature.
channelEvent.setLightColor(Color.RED);
channelEvent.enableVibration(false);
mNotificationManager.createNotificationChannel(channelEvent);
NotificationChannel channelBookings = new NotificationChannel(
context.getString(R.string.notifications_bookings_unique_id),
context.getString(R.string.notifications_bookings_channel_name),
NotificationManager.IMPORTANCE_HIGH);
// Configure the notification channel.
channelBookings.setDescription(context.getString(R.string.notifications_bookings_channel_description));
channelBookings.enableLights(true);
// Sets the notification light color for notifications posted to this
// channel, if the device supports this feature.
channelBookings.setLightColor(Color.RED);
channelBookings.enableVibration(true);
mNotificationManager.createNotificationChannel(channelBookings);
NotificationChannel channelAnnouncements = new NotificationChannel(
context.getString(R.string.notifications_announcements_unique_id),
context.getString(R.string.notifications_announcements_channel_name),
NotificationManager.IMPORTANCE_DEFAULT);
// Configure the notification channel.
channelAnnouncements.setDescription(context.getString(R.string.notifications_announcements_channel_description));
channelAnnouncements.enableLights(true);
// Sets the notification light color for notifications posted to this
// channel, if the device supports this feature.
channelAnnouncements.setLightColor(Color.RED);
channelAnnouncements.enableVibration(true);
mNotificationManager.createNotificationChannel(channelAnnouncements);
}
@TargetApi(26)
public static NotificationChannel getChan(Notification.Builder wip) {
final Notification temp = wip.build();
if (temp.getChannelId() == null) return null;
// create generic audio attributes
final AudioAttributes generic_audio = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.setContentType(AudioAttributes.CONTENT_TYPE_UNKNOWN)
.build();
// create notification channel for hashing purposes from the existing notification builder
NotificationChannel template = new NotificationChannel(
temp.getChannelId(),
getString(temp.getChannelId()),
NotificationManager.IMPORTANCE_DEFAULT);
// mirror the notification parameters in the channel
template.setGroup(temp.getChannelId());
template.setVibrationPattern(temp.vibrate);
template.setSound(temp.sound, generic_audio);
template.setLightColor(temp.ledARGB);
if ((temp.ledOnMS != 0) && (temp.ledOffMS != 0))
template.enableLights(true); // weird how this doesn't work like vibration pattern
template.setDescription(temp.getChannelId() + " " + wip.hashCode());
// get a nice string to identify the hash
final String mhash = my_text_hash(template);
// create another notification channel using the hash because id is immutable
final NotificationChannel channel = new NotificationChannel(
template.getId() + mhash,
getString(temp.getChannelId()) + mhash,
NotificationManager.IMPORTANCE_DEFAULT);
// mirror the settings from the previous channel
channel.setSound(template.getSound(), generic_audio);
if (addChannelGroup()) {
channel.setGroup(template.getGroup());
} else {
channel.setGroup(channel.getId());
}
channel.setDescription(template.getDescription());
channel.setVibrationPattern(template.getVibrationPattern());
template.setLightColor(temp.ledARGB);
if ((temp.ledOnMS != 0) && (temp.ledOffMS != 0))
template.enableLights(true); // weird how this doesn't work like vibration pattern
template.setDescription(temp.getChannelId() + " " + wip.hashCode());
// create a group to hold this channel if one doesn't exist or update text
getNotifManager().createNotificationChannelGroup(new NotificationChannelGroup(channel.getGroup(), getString(channel.getGroup())));
// create this channel if it doesn't exist or update text
getNotifManager().createNotificationChannel(channel);
return channel;
}
public static void createChannel(Context context, NotificationRule rule) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O || rule.settings.noNotification)
return;
NotificationRuleManager.loadUserRuleSettings(context);
String id = rule.settings.notificationChannelId;
if (id == null) {
id = UUID.randomUUID().toString();
rule.settings.notificationChannelId = id;
NotificationRuleManager.saveUserRuleSettings(context);
}
String name = rule.getName();
if (name == null)
name = context.getString(rule.getNameId());
NotificationChannel channel = new NotificationChannel(id, name,
android.app.NotificationManager.IMPORTANCE_HIGH);
if (rule.getNameId() != -1)
channel.setGroup(NotificationManager.getDefaultNotificationChannelGroup(context));
else
channel.setGroup(NotificationManager.getUserNotificationChannelGroup(context));
if (rule.settings.soundEnabled) {
if (rule.settings.soundUri != null)
channel.setSound(Uri.parse(rule.settings.soundUri), new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_NOTIFICATION_COMMUNICATION_INSTANT)
.build());
} else {
channel.setSound(null, null);
}
if (rule.settings.vibrationEnabled) {
channel.enableVibration(true);
if (rule.settings.vibrationDuration != 0)
channel.setVibrationPattern(new long[]{0, rule.settings.vibrationDuration});
}
if (rule.settings.lightEnabled) {
channel.enableLights(true);
if (rule.settings.light != 0)
channel.setLightColor(rule.settings.light);
}
android.app.NotificationManager mgr = (android.app.NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
mgr.createNotificationChannel(channel);
}
/**
* Create push notification channel with provided id, name and importance of the channel.
* You can call this method also when you need to update the name or description of a channel, at
* this case you should use original channel id.
*
* @param context The application context.
* @param channelId The id of the channel.
* @param channelName The user-visible name of the channel.
* @param channelImportance The importance of the channel. Use value from 0 to 5. 3 is default.
* Read more https://developer.android.com/reference/android/app/NotificationManager.html#IMPORTANCE_DEFAULT
* Once you create a notification channel, only the system can modify its importance.
* @param channelDescription The user-visible description of the channel.
* @param groupId The id of push notification channel group.
* @param enableLights True if lights enable for this channel.
* @param lightColor Light color for notifications posted to this channel, if the device supports
* this feature.
* @param enableVibration True if vibration enable for this channel.
* @param vibrationPattern Vibration pattern for notifications posted to this channel.
* @param lockscreenVisibility How to be shown on the lockscreen.
* @param bypassDnd Whether should notification bypass DND.
* @param showBadge Whether should notification show badge.
*/
private static void createNotificationChannel(Context context, String channelId, String
channelName, int channelImportance, String channelDescription, String groupId, boolean
enableLights, int lightColor, boolean enableVibration, long[] vibrationPattern, int
lockscreenVisibility, boolean bypassDnd, boolean showBadge) {
if (context == null || TextUtils.isEmpty(channelId)) {
return;
}
if (BuildUtil.isNotificationChannelSupported(context)) {
try {
NotificationManager notificationManager =
context.getSystemService(NotificationManager.class);
if (notificationManager == null) {
Log.e("Notification manager is null");
return;
}
NotificationChannel notificationChannel = new NotificationChannel(channelId,
channelName, channelImportance);
if (!TextUtils.isEmpty(channelDescription)) {
notificationChannel.setDescription(channelDescription);
}
if (enableLights) {
notificationChannel.enableLights(true);
notificationChannel.setLightColor(lightColor);
}
if (enableVibration) {
notificationChannel.enableVibration(true);
// Workaround for https://issuetracker.google.com/issues/63427588
if (vibrationPattern != null && vibrationPattern.length != 0) {
notificationChannel.setVibrationPattern(vibrationPattern);
}
}
if (!TextUtils.isEmpty(groupId)) {
notificationChannel.setGroup(groupId);
}
notificationChannel.setLockscreenVisibility(lockscreenVisibility);
notificationChannel.setBypassDnd(bypassDnd);
notificationChannel.setShowBadge(showBadge);
notificationManager.createNotificationChannel(notificationChannel);
} catch (Throwable t) {
Util.handleException(t);
}
}
}