Уведомление не молчит, хотя я использовал messagesChannel.setSound (null, null); - PullRequest
0 голосов
/ 08 сентября 2018

почему-то мое уведомление все еще издает звук. Что я сделал не так?

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        // For foreground service
        Intent notificationIntent = new Intent(this, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

        // Creating channel for notification
        String id = BackroundService.class.getSimpleName();
        String name = BackroundService.class.getSimpleName();
        NotificationChannel notificationChannel = new NotificationChannel(id,
                name, NotificationManager.IMPORTANCE_DEFAULT);
        NotificationManager service = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        service.createNotificationChannel(notificationChannel);
        notificationChannel.setSound(null, null);

        // Foreground notification
        Notification notification = new Notification.Builder(this, id)
                .setContentTitle(getText(R.string.app_name))
                .setContentText("KSL is protecting you!")
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentIntent(pendingIntent)
                .setTicker("Ticker text")
                .build();

        startForeground(9, notification);
    }

Я использовал notificationChannel.setSound(null, null); метод, но он не работает.

1 Ответ

0 голосов
/ 08 сентября 2018

Укажите ваш канал в уведомлении.

Notification notification = new Notification.Builder(this, id)
            .setContentTitle(getText(R.string.app_name))
            .setContentText("KSL is protecting you!")
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentIntent(pendingIntent)
            .setTicker("Ticker text")
            .setChannelId(channelId)
            .build();

Редактировать

Убедитесь, что вы удалили и переустановили приложение, чтобы изменения вступили в силу.

Редактировать 2

NotificationChannel notificationChannel = new NotificationChannel(id,
            name, NotificationManager.IMPORTANCE_LOW);

Система Android устанавливает важность уведомления на NotificationManager.IMPORTANCE_HIGH (по умолчанию воспроизводится звук), даже если вы указываете значение NotificationManager.IMPORTANCE_DEFAULT (по умолчанию звук не воспроизводится) , Я не знаю, почему это так, но для решения вашей проблемы я рекомендую изменить значение на NotificationManager.IMPORTANCE_LOW .

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...