[英]Android : Notification sound disable
我有一個帶有此代碼的通知:
Notification notifica = new Notification();
notifica.flags |= Notification.FLAG_AUTO_CANCEL;
notifica.icon = R.drawable.serie_notification;
notifica.when = System.currentTimeMillis();
使用notifica.defaults = notifica.defaults|Notification.DEFAULT_SOUND; 我啟用了默認聲音,但如果我想禁用聲音我該怎么辦?
好吧,它通過這樣做對我有用:
myNotification.defaults = 0;
試試看 =)
在 Oreo 之前的設備、Oreo 及更高版本的設備上顯示無聲通知
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent = new Intent(this, AlertDetails.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
String CHANNEL_ID = "channel_id";
// You must create the channel to show the notification on Android 8.0 and higher versions
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// Set importance to IMPORTANCE_LOW to mute notification sound on Android 8.0 and above
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, "name", NotificationManager.IMPORTANCE_LOW);
notificationManager.createNotificationChannel(channel);
}
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("My notification")
.setContentText("Hello World!")
// You must set the priority to support Android 7.1 and lower
.setPriority(NotificationCompat.PRIORITY_LOW) // Set priority to PRIORITY_LOW to mute notification sound
.setContentIntent(pendingIntent)
.setAutoCancel(true);
notificationManager.notify(
1001, // notification id
mBuilder.build());
可以這樣做基本上只啟用除聲音(即Notification.DEFAULT_SOUND
)之外的所有其他Notification.defaults
。
這是一個適合您的示例:
myNotification.defaults = 0;
myNotification.defaults |= Notification.DEFAULT_VIBRATE;
以下是您可以選擇的所有可用選項:
Notification.DEFAULT_LIGHTS
Notification.DEFAULT_VIBRATE
Notification.DEFAULT_SOUND
Notification.DEFAULT_ALL // This enables all above 3
更新
通知.defaults
已棄用
android "O" >= ,禁用或啟用通知的聲音您使用兩個 id 作為通知通道。 一個用於聲音通知,另一個用於當您要禁用時
@RequiresApi(api = Build.VERSION_CODES.O)
private void createNotificationChannel(Context context, NotificationManager mNotificationManager, boolean playSound) {
// The user-visible name of the channel.
CharSequence name = context.getString(R.string.channel_name);
// The user-visible description of the channel.
String description = context.getString(R.string.channel_description);
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel mChannel = new NotificationChannel(playSound ? channel_id_sound : channel_id_no_sound, 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(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
if (!playSound)
mChannel.setSound(null, null);
mNotificationManager.createNotificationChannel(mChannel);
}
在較新版本的 Android 中,您必須將通知通道的優先級設置為低:
var channel = new NotificationChannel(notificationChannelName, "channel", NotificationManager.IMPORTANCE_LOW);
您可以使用此禁用聲音
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, "Channel name", NotificationManager.IMPORTANCE_LOW);
channel.setSound(null, null);
只需將頻道的聲音設置為 null
NotificationCompat.Builder 方法
setSilent(true)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.