下面列出了android.app.NotificationChannel#setImportance ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private static void createNotificationChannel(Context context, Constants.NotificationChannel channel) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel(channelId(channel), context.getString(R.string.app_name), NotificationManager.IMPORTANCE_DEFAULT);
switch(channel) {
case BACKUP_FAILED:
notificationChannel.setName(context.getString(R.string.notification_channel_name_backup_failed));
notificationChannel.setDescription(context.getString(R.string.notification_channel_desc_backup_failed));
notificationChannel.setImportance(NotificationManager.IMPORTANCE_HIGH);
break;
case BACKUP_SUCCESS:
notificationChannel.setName(context.getString(R.string.notification_channel_name_backup_success));
notificationChannel.setDescription(context.getString(R.string.notification_channel_desc_backup_success));
notificationChannel.setImportance(NotificationManager.IMPORTANCE_LOW);
break;
default:
break;
}
NotificationManager notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
notificationManager.createNotificationChannel(notificationChannel);
}
}
@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;
}
@RequiresApi(VERSION_CODES.O)
private static void createDefaultNotificationChannel(Context context) {
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
String appName = context.getString(R.string.app_name);
NotificationChannel channel = new NotificationChannel(DEFAULT_CHANNEL_ID,
appName, NotificationManager.IMPORTANCE_DEFAULT);
channel.setImportance(NotificationManager.IMPORTANCE_LOW);
notificationManager.createNotificationChannel(channel);
}
static private NotificationChannel updateNotificationChannel( Context context, NotificationChannel notificationChannel, final AndroidPushTemplate template )
{
if( template.getShowBadge() != null )
notificationChannel.setShowBadge( template.getShowBadge() );
if( template.getPriority() != null && template.getPriority() > 0 && template.getPriority() < 6 )
notificationChannel.setImportance( template.getPriority() ); // NotificationManager.IMPORTANCE_DEFAULT
AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setUsage( AudioAttributes.USAGE_NOTIFICATION_RINGTONE )
.setContentType( AudioAttributes.CONTENT_TYPE_SONIFICATION )
.setFlags( AudioAttributes.FLAG_AUDIBILITY_ENFORCED )
.setLegacyStreamType( AudioManager.STREAM_NOTIFICATION )
.build();
notificationChannel.setSound( PushTemplateHelper.getSoundUri( context, template.getSound() ), audioAttributes );
if (template.getLightsColor() != null)
{
notificationChannel.enableLights( true );
notificationChannel.setLightColor( template.getLightsColor()|0xFF000000 );
}
if( template.getVibrate() != null && template.getVibrate().length > 0 )
{
long[] vibrate = new long[ template.getVibrate().length ];
int index = 0;
for( long l : template.getVibrate() )
vibrate[ index++ ] = l;
notificationChannel.enableVibration( true );
notificationChannel.setVibrationPattern( vibrate );
}
return notificationChannel;
}
public static Notification.Builder getNotificationBuilder(final Context context) {
NotificationManager manager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationChannel channel =
new NotificationChannel(NOTIFICATION_CHANNEL_ID, NOTIFICATION_CHANNEL_ID,
NotificationManager.IMPORTANCE_NONE);
channel.setImportance(NotificationManager.IMPORTANCE_MIN); // ignored by Android O ...
channel.enableLights(false);
channel.enableVibration(false);
channel.setBypassDnd(false);
channel.setSound(null, null);
manager.createNotificationChannel(channel);
Notification.Builder builder = new Notification.Builder(context, NOTIFICATION_CHANNEL_ID);
return builder;
}
@Override
public void createNotificationChannel(String pkg, int uid, NotificationChannel channel,
boolean fromTargetApp, boolean hasDndAccess) {
Preconditions.checkNotNull(pkg);
Preconditions.checkNotNull(channel);
Preconditions.checkNotNull(channel.getId());
Preconditions.checkArgument(!TextUtils.isEmpty(channel.getName()));
Record r = getOrCreateRecord(pkg, uid);
if (r == null) {
throw new IllegalArgumentException("Invalid package");
}
if (channel.getGroup() != null && !r.groups.containsKey(channel.getGroup())) {
throw new IllegalArgumentException("NotificationChannelGroup doesn't exist");
}
if (NotificationChannel.DEFAULT_CHANNEL_ID.equals(channel.getId())) {
throw new IllegalArgumentException("Reserved id");
}
NotificationChannel existing = r.channels.get(channel.getId());
// Keep most of the existing settings
if (existing != null && fromTargetApp) {
if (existing.isDeleted()) {
existing.setDeleted(false);
// log a resurrected channel as if it's new again
MetricsLogger.action(getChannelLog(channel, pkg).setType(
MetricsProto.MetricsEvent.TYPE_OPEN));
}
existing.setName(channel.getName().toString());
existing.setDescription(channel.getDescription());
existing.setBlockableSystem(channel.isBlockableSystem());
if (existing.getGroup() == null) {
existing.setGroup(channel.getGroup());
}
// Apps are allowed to downgrade channel importance if the user has not changed any
// fields on this channel yet.
if (existing.getUserLockedFields() == 0 &&
channel.getImportance() < existing.getImportance()) {
existing.setImportance(channel.getImportance());
}
// system apps and dnd access apps can bypass dnd if the user hasn't changed any
// fields on the channel yet
if (existing.getUserLockedFields() == 0 && hasDndAccess) {
boolean bypassDnd = channel.canBypassDnd();
existing.setBypassDnd(bypassDnd);
if (bypassDnd != mAreChannelsBypassingDnd) {
updateChannelsBypassingDnd();
}
}
updateConfig();
return;
}
if (channel.getImportance() < IMPORTANCE_NONE
|| channel.getImportance() > NotificationManager.IMPORTANCE_MAX) {
throw new IllegalArgumentException("Invalid importance level");
}
// Reset fields that apps aren't allowed to set.
if (fromTargetApp && !hasDndAccess) {
channel.setBypassDnd(r.priority == Notification.PRIORITY_MAX);
}
if (fromTargetApp) {
channel.setLockscreenVisibility(r.visibility);
}
clearLockedFields(channel);
if (channel.getLockscreenVisibility() == Notification.VISIBILITY_PUBLIC) {
channel.setLockscreenVisibility(Ranking.VISIBILITY_NO_OVERRIDE);
}
if (!r.showBadge) {
channel.setShowBadge(false);
}
r.channels.put(channel.getId(), channel);
if (channel.canBypassDnd() != mAreChannelsBypassingDnd) {
updateChannelsBypassingDnd();
}
MetricsLogger.action(getChannelLog(channel, pkg).setType(
MetricsProto.MetricsEvent.TYPE_OPEN));
}