Android 即使我没有设置声音,奥利奥通知也会继续发出声音。在旧版本上,完美运行

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

所以我正在使我的应用程序与 Oreo 兼容,但遇到通知问题。

我根据文档添加了通知通道,一切正常,除了通知在每次发布时都发出声音,我也尝试将默认值设置为 0。

我正在模拟器中测试我的应用程序,非常感谢任何帮助。

使用此代码创建频道

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

用这个替换你的代码

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

查看通知频道设置(滑动您的通知并按其下方的设置图标,然后 select 您的频道)。这些设置是在您第一次创建频道时设置的,除非您在设备中手动进行,否则不会修改(至少这是我卸载并重新安装我的应用程序以查看默认设置的经验)。

基本上,channel.setSound(null, null) 仅当您在全新安装时创建频道时才有效。这可能是他们试图在 official guide:

中解释的内容

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

如果您尝试遵循该指南并设置 NotificationManager.IMPORTANCE_HIGH 而未使用 channel.setSound(null, null),则频道将获得重要级别 Urgent Make sound and pop on screen 并使用默认声音。

我的场景是第一次有声音,更新通知不需要声音

我用这个setOnlyAlertOnce()方法

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

测试通过版本 26

^Benjamin 的回答有效,但他遗漏了一些重要的细节!每次调整代码时都必须更改频道 ID,否则 Oreo 不会进行更改。不知道为什么。

下面是我的代码,您可以在 <-------- 这里看到必须在哪里进行更改

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

    }