web-dev-qa-db-fra.com

Désactiver le son de notification sur Android O

J'essaie de me débarrasser de la son de notification dans la méthode ci-dessous.

J'ai pu le réduire pour ne s'éteindre qu'une seule fois mais cela devrait être complètement silencieux dans Android O et versions inférieures.

J'ai cherché longtemps sur stackoverflow et google mais jusqu'à présent rien ne fonctionne complètement.

Toute aide est appréciée.

public void showUpdateProgressNotification(int id, String appName, int progress, String status, long downloadStart) {

        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        String NOTIFICATION_CHANNEL_ID = "my_channel_id_01";

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel notificationChannel = new NotificationChannel
                    (NOTIFICATION_CHANNEL_ID, "Test Notifications", NotificationManager.IMPORTANCE_LOW);
            notificationChannel.setSound(null, null);

            // Configure the notification channel.
            notificationChannel.setDescription("Channel test");
            notificationManager.createNotificationChannel(notificationChannel);
        }

        Intent cancelIntent = new Intent(ACTION_FILE_CANCEL);
        cancelIntent.putExtra("id", id);
        PendingIntent cancel = PendingIntent.getBroadcast(this, ACTION_FILE_CANCEL.hashCode() + id,
                cancelIntent, PendingIntent.FLAG_CANCEL_CURRENT);

        NotificationCompat.Builder notification = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
                .setContentTitle(appName)
                .setContentText(status)
                .setSmallIcon(Android.R.drawable.stat_sys_download)
                .setDefaults(0)
                .setLargeIcon(BitmapFactory.decodeResource(MyApplication_.getInstance().getResources(), R.mipmap.ic_launcher))
                .setProgress(100, progress, progress == 0)
                .setWhen(downloadStart)
                .setContentIntent(cancel)
                .setGroup(GROUP_KEY)
                .addAction(R.drawable.ic_close_black_24dp, "Cancel", cancel)
                .setColor(MyApplication_.getInstance().getResources().getColor(R.color.apps_color))
                .setOnlyAlertOnce(true);

        NotificationCompat.InboxStyle inboxStyle =
                new NotificationCompat.InboxStyle();
        inboxStyle.addLine(status);
        notification.setStyle(inboxStyle);

        notificationManager.notify(id, notification.build());

        addNotification(id);
 }
11
Simon

Je l'ai compris après quelques recherches supplémentaires.

C'est la partie vitale:

//Configure the notification channel, NO SOUND
notificationChannel.setDescription("no sound");
notificationChannel.setSound(null,null); <---- ignore sound
notificationChannel.enableLights(false);
notificationChannel.setLightColor(Color.BLUE);
notificationChannel.enableVibration(false);

Au début, sur l'implémentation, cela ne fonctionnait toujours pas mais après avoir désinstallé mon application et l'avoir réinstallée tout allait bien.

Si quelqu'un en a besoin, voici le code correct:

public void showUpdateProgressNotification(int id, String appName, int progress, String status, long downloadStart) {

        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        String NOTIFICATION_CHANNEL_ID = "my_channel_id_01";

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel notificationChannel = new NotificationChannel(
                    NOTIFICATION_CHANNEL_ID, "My app no sound", NotificationManager.IMPORTANCE_LOW
            );

            //Configure the notification channel, NO SOUND
            notificationChannel.setDescription("no sound");
            notificationChannel.setSound(null,null);
            notificationChannel.enableLights(false);
            notificationChannel.setLightColor(Color.BLUE);
            notificationChannel.enableVibration(false);
            notificationManager.createNotificationChannel(notificationChannel);
        }

        Intent cancelIntent = new Intent(ACTION_FILE_CANCEL);
        cancelIntent.putExtra("id", id);
        PendingIntent cancel = PendingIntent.getBroadcast(this, ACTION_FILE_CANCEL.hashCode() + id,
                cancelIntent, PendingIntent.FLAG_CANCEL_CURRENT);

        NotificationCompat.Builder notification = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
                .setContentTitle(appName)
                .setContentText(status)
                .setSmallIcon(Android.R.drawable.stat_sys_download)
                .setDefaults(0)
                .setLargeIcon(BitmapFactory.decodeResource(MyApplication_.getInstance().getResources(), R.mipmap.ic_launcher))
                .setProgress(100, progress, progress == 0)
                .setWhen(downloadStart)
                .setContentIntent(cancel)
                .setGroup(GROUP_KEY)
                .addAction(R.drawable.ic_close_black_24dp, "Cancel", cancel)
                .setColor(MyApplication_.getInstance().getResources().getColor(R.color.apps_color));

        NotificationCompat.InboxStyle inboxStyle =
                new NotificationCompat.InboxStyle();
        inboxStyle.addLine(status);
        notification.setStyle(inboxStyle);

        notificationManager.notify(id, notification.build());

        addNotification(id);
    }
38
Simon