下面列出了android.app.NotificationChannel#setSound ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@TargetApi(26)
private static @NonNull NotificationChannel copyChannel(@NonNull NotificationChannel original, @NonNull String id) {
NotificationChannel copy = new NotificationChannel(id, original.getName(), original.getImportance());
copy.setGroup(original.getGroup());
copy.setSound(original.getSound(), original.getAudioAttributes());
copy.setBypassDnd(original.canBypassDnd());
copy.enableVibration(original.shouldVibrate());
copy.setVibrationPattern(original.getVibrationPattern());
copy.setLockscreenVisibility(original.getLockscreenVisibility());
copy.setShowBadge(original.canShowBadge());
copy.setLightColor(original.getLightColor());
copy.enableLights(original.shouldShowLights());
return copy;
}
@TargetApi(Build.VERSION_CODES.O)
private void createNotificationChannels() {
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
NotificationChannel foreground = new NotificationChannel("foreground", getString(R.string.channel_foreground), NotificationManager.IMPORTANCE_MIN);
foreground.setSound(null, Notification.AUDIO_ATTRIBUTES_DEFAULT);
nm.createNotificationChannel(foreground);
NotificationChannel notify = new NotificationChannel("notify", getString(R.string.channel_notify), NotificationManager.IMPORTANCE_DEFAULT);
notify.setSound(null, Notification.AUDIO_ATTRIBUTES_DEFAULT);
nm.createNotificationChannel(notify);
NotificationChannel access = new NotificationChannel("access", getString(R.string.channel_access), NotificationManager.IMPORTANCE_DEFAULT);
access.setSound(null, Notification.AUDIO_ATTRIBUTES_DEFAULT);
nm.createNotificationChannel(access);
}
private NotificationManager initNotificationManager() {
NotificationManager notificationManager;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID, "Playback", android.app.NotificationManager.IMPORTANCE_DEFAULT);
notificationChannel.setSound(null, null);
notificationChannel.setShowBadge(false);
notificationManager = (android.app.NotificationManager) this.context
.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.createNotificationChannel(notificationChannel);
} else {
notificationManager = (android.app.NotificationManager) this.context
.getSystemService(Context.NOTIFICATION_SERVICE);
}
return notificationManager;
}
@TargetApi(Build.VERSION_CODES.O)
private void createNotificationChannel() {
//第一个参数:channel_id
//第二个参数:channel_name
//第三个参数:设置通知重要性级别
//注意:该级别必须要在 NotificationChannel 的构造函数中指定,总共要五个级别;
//范围是从 NotificationManager.IMPORTANCE_NONE(0) ~ NotificationManager.IMPORTANCE_HIGH(4)
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME,
NotificationManager.IMPORTANCE_LOW);
channel.canBypassDnd();//是否绕过请勿打扰模式
channel.enableLights(true);//是否在桌面icon右上角展示小红点
channel.setLockscreenVisibility(VISIBILITY_SECRET);//锁屏显示通知
channel.setLightColor(Color.RED);//闪关灯的灯光颜色
channel.canShowBadge();//桌面launcher的消息角标
//channel.enableVibration(false);//是否允许震动
channel.getAudioAttributes();//获取系统通知响铃声音的配置
channel.getGroup();//获取通知取到组
channel.setBypassDnd(true);//设置可绕过 请勿打扰模式
channel.setSound(null, null);
//channel.setVibrationPattern(new long[]{100, 100, 200});//设置震动模式
channel.shouldShowLights();//是否会有灯光
channel.setShowBadge(true); //是否在久按桌面图标时显示此渠道的通知
getManager().createNotificationChannel(channel);
}
@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;
}
@Override
public void onCreate() {
super.onCreate();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID,
getString(R.string.apply_on_boot), NotificationManager.IMPORTANCE_DEFAULT);
notificationChannel.setSound(null, null);
notificationManager.createNotificationChannel(notificationChannel);
Notification.Builder builder = new Notification.Builder(
this, CHANNEL_ID);
builder.setContentTitle(getString(R.string.apply_on_boot))
.setSmallIcon(R.mipmap.ic_launcher);
startForeground(NotificationId.APPLY_ON_BOOT, builder.build());
}
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
//创建NotificationChannel
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
NotificationChannel channel = new NotificationChannel("com.xyoye.dandanplay.smbservice.playchannel", "SMB服务", NotificationManager.IMPORTANCE_LOW);
channel.enableVibration(false);
channel.setVibrationPattern(new long[]{0});
channel.enableLights(false);
channel.setSound(null, null);
if (notificationManager != null) {
notificationManager.createNotificationChannel(channel);
}
}
startForeground(NOTIFICATION_ID, buildNotification());
return super.onStartCommand(intent, flags, startId);
}
private void listing11_22(NotificationChannel channel, NotificationCompat.Builder builder) {
// Listing 11-22: Customizing a Notification's alerts
// For Android 8.0+ higher devices:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
channel.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION), null);
channel.setVibrationPattern(new long[]{1000, 1000, 1000, 1000, 1000});
channel.setLightColor(Color.RED);
} else {
// For Android 7.1 or lower devices:
builder.setPriority(NotificationCompat.PRIORITY_HIGH)
.setSound(
RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000})
.setLights(Color.RED, 0, 1);
}
}
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 initData() {
//初始化广播
wifiReceiver = new WifiReceiver(isConnected -> {
if (TorrentConfig.getInstance().isDownloadOnlyWifi() && !isConnected) {
ToastUtils.showShort("wifi连接断开,已暂停所有下载任务");
TorrentEngine.getInstance().pauseAll();
}
});
registerReceiver(wifiReceiver, new IntentFilter(WifiManager.NETWORK_STATE_CHANGED_ACTION));
//初始化刷新
syncNotifyHandler = new Handler();
syncNotifyRunnable = new Runnable() {
@Override
public void run() {
isRefreshing.set(true);
updateUIData();
startForeground(NOTIFICATION_ID, buildNotification());
syncNotifyHandler.postDelayed(this, NOTIFY_SYNC_TIME);
}
};
//初始化通知
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel("com.xyoye.dandanplay.TorrentService.DownloadChannel", "TorrentService", NotificationManager.IMPORTANCE_LOW);
channel.enableVibration(false);
channel.setVibrationPattern(new long[]{0});
channel.enableLights(false);
channel.setSound(null, null);
if (notificationManager != null) {
notificationManager.createNotificationChannel(channel);
}
}
startForeground(NOTIFICATION_ID, buildNotification());
}
@RequiresApi(Build.VERSION_CODES.O)
private void ensureEvolvedNotificationChannel() {
final NotificationChannel screenshotChannel = new NotificationChannel(
CHANNEL_ID_SCREENSHOT,
getString(R.string.noti_channel_screenshot),
NotificationManager.IMPORTANCE_HIGH
);
screenshotChannel.setSound(Uri.EMPTY, new AudioAttributes.Builder().build());
screenshotChannel.enableLights(false);
final NotificationChannel otherChannel = new NotificationChannel(
CHANNEL_ID_OTHER,
getString(R.string.noti_channel_other),
NotificationManager.IMPORTANCE_DEFAULT
);
screenshotChannel.setSound(Uri.EMPTY, new AudioAttributes.Builder().build());
screenshotChannel.enableLights(true);
final NotificationChannel previewedChannel = new NotificationChannel(
CHANNEL_ID_PREVIEWED_SCREENSHOT,
getString(R.string.noti_channel_screenshot_preview),
NotificationManager.IMPORTANCE_MIN
);
previewedChannel.setSound(Uri.EMPTY, new AudioAttributes.Builder().build());
previewedChannel.enableLights(false);
createNotificationChannels(
TARGET_PACKAGE, Arrays.asList(screenshotChannel, otherChannel, previewedChannel));
}
@TargetApi(26)
private void createNotificationChannel() {
NotificationChannel channel = new NotificationChannel("ongoing",
getString(R.string.notificationChannelName), NotificationManager.IMPORTANCE_LOW);
channel.setShowBadge(false);
channel.setSound(null, null);
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if (notificationManager != null)
notificationManager.createNotificationChannel(channel);
}
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);
}
}
@RequiresApi(api = Build.VERSION_CODES.O)
private synchronized void createAppNotificationChannel() throws ApplozicException {
CharSequence name = MobiComKitConstants.APP_NOTIFICATION_NAME;
int importance = NotificationManager.IMPORTANCE_HIGH;
if (mNotificationManager != null && mNotificationManager.getNotificationChannel(MobiComKitConstants.AL_APP_NOTIFICATION) == null) {
NotificationChannel mChannel = new NotificationChannel(MobiComKitConstants.AL_APP_NOTIFICATION, name, importance);
mChannel.enableLights(true);
mChannel.setLightColor(Color.GREEN);
mChannel.setShowBadge(ApplozicClient.getInstance(context).isUnreadCountBadgeEnabled());
if (ApplozicClient.getInstance(context).getVibrationOnNotification()) {
mChannel.enableVibration(true);
mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
}
AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_NOTIFICATION).build();
if (TextUtils.isEmpty(soundFilePath)) {
throw new ApplozicException("Custom sound path is required to create App notification channel. " +
"Please set a sound path using Applozic.getInstance(context).setCustomNotificationSound(your-sound-file-path)");
}
mChannel.setSound(Uri.parse(soundFilePath), audioAttributes);
mNotificationManager.createNotificationChannel(mChannel);
Utils.printLog(context, TAG, "Created app notification channel");
}
}
private void createStaticNotificationChannel() {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(staticNotificationId, "Static notification widget", NotificationManager.IMPORTANCE_DEFAULT);
channel.setSound(null, null);
channel.enableVibration(false);
notificationManagerCompat.createNotificationChannel(channel);
}
}
public static void createDownloadChannel(Context c, @NonNull NotificationManager manager) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(
CHANNEL_ID_DOWNLOAD,
getNotificationChannelName(c, CHANNEL_ID_DOWNLOAD),
NotificationManager.IMPORTANCE_LOW);
channel.setShowBadge(true);
channel.setSound(null, null);
channel.setLightColor(ContextCompat.getColor(c, R.color.colorNotification));
manager.createNotificationChannel(channel);
}
}
private void showNotification() {
// Sets an ID for the notification, so it can be updated.
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationManager mNotificationManager =
(NotificationManager) getContext().getSystemService(Context.NOTIFICATION_SERVICE);
NotificationChannel mChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, NOTIFICATION_CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
mChannel.setSound(null,null);
// Create a notification and set the notification channel.
Notification notification = new Notification.Builder(getContext(), NOTIFICATION_CHANNEL_NAME)
.setSmallIcon(R.drawable.ic_notification_icon)
.setContentTitle(getString(R.string.fetching_subscription_videos))
.setContentText(String.format(getContext().getString(R.string.fetched_videos_from_channels), numVideosFetched, numChannelsFetched, numChannelsSubscribed))
.setChannelId(NOTIFICATION_CHANNEL_ID)
.build();
mNotificationManager.createNotificationChannel(mChannel);
// Issue the notification.
mNotificationManager.notify(NOTIFICATION_ID , notification);
} else {
final Intent emptyIntent = new Intent();
PendingIntent pendingIntent = PendingIntent.getActivity(getContext(), 1, emptyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(getContext(), "")
.setSmallIcon(R.drawable.ic_notification_icon)
.setContentTitle(getString(R.string.fetching_subscription_videos))
.setContentText(String.format(getContext().getString(R.string.fetched_videos_from_channels), numVideosFetched, numChannelsFetched, numChannelsSubscribed))
.setPriority(NotificationCompat.FLAG_ONGOING_EVENT)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getContext().getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_ID, builder.build());
}
}
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);
}
}
@TargetApi(26)
public static NotificationChannel getChan(NotificationCompat.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(wip.mNotification.vibrate);
template.setSound(wip.mNotification.sound, generic_audio);
template.setLightColor(wip.mNotification.ledARGB);
if ((wip.mNotification.ledOnMS != 0) && (wip.mNotification.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(wip.mNotification.ledARGB);
if ((wip.mNotification.ledOnMS != 0) && (wip.mNotification.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;
}
@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;
}