Android:通知声音禁用

1cklez4t  于 2022-12-02  发布在  Android
关注(0)|答案(6)|浏览(211)

我收到了一个带有此代码的通知:

Notification notifica = new Notification();
notifica.flags |= Notification.FLAG_AUTO_CANCEL;
notifica.icon = R.drawable.serie_notification;
notifica.when = System.currentTimeMillis();

使用**通知.默认值=通知.默认值|通知.DEFAULT_SOUND;**我启用了默认声音,但如果我想禁用声音,该怎么办??

093gszye

093gszye1#

好吧,我这么做很管用:

myNotification.defaults = 0;

试试看吧=)

gab6jxml

gab6jxml2#

在pre 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());
s4n0splo

s4n0splo3#

基本上可以这样做,只需启用除声音(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已过时

3yhwsihp

3yhwsihp4#

在较新版本的Android中,你必须将通知通道的优先级设置为低:

var channel = new NotificationChannel(notificationChannelName, "channel", NotificationManager.IMPORTANCE_LOW);
agyaoht7

agyaoht75#

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);
}
llmtgqce

llmtgqce6#

您可以使用此

NotificationChannel channel = new NotificationChannel(CHANNEL_ID, "Channel name", NotificationManager.IMPORTANCE_LOW);
channel.setSound(null, null);

只需将通道声音设置为空

相关问题