web-dev-qa-db-fra.com

Android Oreo notification continue de faire du son même si je ne règle pas le son. Sur la version plus ancienne, fonctionne parfaitement

Je rend donc mon application compatible avec Oreo et je rencontre un problème de notification.

J'ai ajouté un canal de notification selon la documentation et tout fonctionne bien, sauf que la notification continue de sonner sur chaque publication, j'ai également essayé de définir les valeurs par défaut à 0.

Je teste mon application dans l'émulateur, toute aide est très appréciée.

Utilisé ce code pour créer une chaîne

  NotificationCompat.Builder builder = new NotificationCompat.Builder(PlayerService.this, "channel_01")
                            .setAutoCancel(false)
                            .setContentIntent(pendingIntent)
                            .setContent(viewsSmall)
                            .setCustomBigContentView(viewsExpanded)
                            .setDeleteIntent(pSwipeToDismiss);

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Lollipop) {
                builder.setVisibility(Notification.VISIBILITY_PUBLIC);
            }

            if (Build.VERSION.SDK_INT > Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {
                builder.setPriority(Notification.PRIORITY_MAX);
            }


            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                            /* Create or update. */
                NotificationChannel channel = new NotificationChannel("channel_01",
                            "Playback Notification",
                            NotificationManager.IMPORTANCE_DEFAULT);
                    mNotificationManager.createNotificationChannel(channel);
                    mBuilder.setChannelId("channel_01");
                }
    final Notification notification = builder.build();

                    startForeground(Constants.NOTIFICATION_ID.FOREGROUND_SERVICE,notification);
24
Amit Bhandari

Jetez un œil aux paramètres du canal de notification (faites glisser votre notification et appuyez sur l'icône des paramètres en dessous, puis sélectionnez votre canal). Ces paramètres sont définis la première fois que vous créez la chaîne, puis ne sont pas modifiés, sauf si vous le faites manuellement sur l'appareil (du moins, c'est mon expérience de la désinstallation et de la réinstallation de mon application pour voir quels paramètres j'obtiens par défaut).

Fondamentalement, channel.setSound(null, null) n'aura d'effet que lorsque vous créerez le canal sur une nouvelle installation . C'est peut-être ce qu'ils essaient d'expliquer dans le guide officiel :

Tenter de créer un canal de notification existant avec ses valeurs d'origine n'effectue aucune opération

Si vous tentiez de suivre ce guide et de définir NotificationManager.IMPORTANCE_HIGH Sans utiliser channel.setSound(null, null), le canal obtiendrait le niveau d'importance Urgent Make sound and pop on screen Avec le son par défaut.

37
Love

^ Benjamin réponse fonctionne mais il manque un détail important! Vous devez changer votre identifiant de chaîne chaque fois que vous ajustez votre code ou Oreo ne fera pas les changements. Pas certain de pourquoi.

Mon code ci-dessous et vous pouvez voir où le chnage doit être fait avec ce <------- ici

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

        String channelID = "My Channel I";  <-------here
        String appName = mContext.getResources().getString(R.string.app_name);


        NotificationCompat.Builder notificationCompatBuilder = new NotificationCompat.Builder(mContext );
        notificationCompatBuilder
                .setOngoing(true)
                .setContentTitle(mContext.getResources().getString(R.string.app_name))
                .setContentText(mContext.getString(R.string.clocked_in))
                .setSmallIcon(R.drawable.ic_action_name)
                .setChannelId(channelID)
                .setSound(null);

        NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);

        NotificationChannel notificationChannel = new NotificationChannel(channelID, appName, NotificationManager.IMPORTANCE_LOW);
        notificationChannel.setSound(null, null);
        notificationManager.createNotificationChannel(notificationChannel);
        notificationManager.notify(ONGOINGNOTIFICATION_ID, notificationCompatBuilder.build());

    }
8
cagney

Remplacez votre code par ce

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                /* Create or update. */
        NotificationChannel channel = new NotificationChannel("channel_01",
                "Playback Notification",
                NotificationManager.IMPORTANCE_LOW);
        channel.setSound(null, null);
        mNotificationManager.createNotificationChannel(channel);
        mBuilder.setChannelId("channel_01");
    }
6
Benjamin

Ma scène est la première fois qu'il y a un son et la notification de mise à jour ne nécessite pas de son.

J'utilise cette méthode setOnlyAlertOnce ()

Référence: https://developer.Android.com/training/notify-user/build-notification#Updating

Test pass version 26

3
user5218504