Android Oreo notification keep making Sound even if I do not set sound. On Older version, works perfectly

允我心安 提交于 2019-11-27 15:39:55

问题


So I am making my app compatible with Oreo and facing issue with notification.

I added notification channel according to documentation and everything is working smooth except notification keep making sound on every posting, tried setting defaults to 0 as well.

I am testing my app in emulator, any help is highly appreciated.

Used this code for creating channel

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

回答1:


Take a look at the notification channel settings (swipe your notification and press the settings icon under it and then select your channel). Those settings are set the first time you create the channel and then not modified unless you do it manually in the device (at least that is my experience from uninstalling and reinstalling my app to see what settings I get by default).

Basically, channel.setSound(null, null) will only have effect when you create the channel on a fresh installation. That might be what they try to explain in the official guide:

Attempting to create an existing notification channel with its original values performs no operation

If you tried to follow that guide and set NotificationManager.IMPORTANCE_HIGH and didn't use channel.setSound(null, null), the channel would get importance level Urgent Make sound and pop on screen with the default sound.




回答2:


^Benjamin answer works but he is missing some important detail! You must change your channel ID each time you adjust your code or Oreo will not make the changes. Not sure why.

My code below and you can see where the chnage must be made with this <-------here

 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());

    }



回答3:


Replace your code with this

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



回答4:


My scene is the first time there is a sound, and the update notification does not require a sound.

I use this setOnlyAlertOnce() method

Reference: https://developer.android.com/training/notify-user/build-notification#Updating

Test pass version 26



来源:https://stackoverflow.com/questions/46234254/android-oreo-notification-keep-making-sound-even-if-i-do-not-set-sound-on-older

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!