Mim*_*moG 15 notifications android
我收到了这段代码的通知:
Notification notifica = new Notification();
notifica.flags |= Notification.FLAG_AUTO_CANCEL;
notifica.icon = R.drawable.serie_notification;
notifica.when = System.currentTimeMillis();
Run Code Online (Sandbox Code Playgroud)
with notifica.defaults = notifica.defaults | Notification.DEFAULT_SOUND; 我启用默认声音,但如果我想禁用声音,我该怎么办?
Ted*_*Ted 18
嗯,通过这样做,它对我有用:
myNotification.defaults = 0;
Run Code Online (Sandbox Code Playgroud)
尝试一下=)
有可能这样做基本上只启用Notification.defaults除声音之外的所有其他声音(即Notification.DEFAULT_SOUND).
这是一个适合您的示例:
myNotification.defaults = 0;
myNotification.defaults |= Notification.DEFAULT_VIBRATE;
Run Code Online (Sandbox Code Playgroud)
以下是您可以选择的所有可用选项:
Notification.DEFAULT_LIGHTS
Notification.DEFAULT_VIBRATE
Notification.DEFAULT_SOUND
Notification.DEFAULT_ALL // This enables all above 3
Run Code Online (Sandbox Code Playgroud)
更新
通知.defaults已弃用
在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());
Run Code Online (Sandbox Code Playgroud)
在较新版本的 Android 中,您必须将通知通道的优先级设置为低:
var channel = new NotificationChannel(notificationChannelName, "channel", NotificationManager.IMPORTANCE_LOW);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
18757 次 |
| 最近记录: |