安卓 Oreo 通知即使我没有设置声音也会不停地发出声音。在旧版本上,一切正常。

25

我正在使我的应用程序与 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);

请贴上你所有与通知构建相关的代码。 - Benjamin
添加了我用于发布通知的完整代码。 - Amit Bhandari
4个回答

39
请查看通知频道设置(滑动您的通知并按下其下面的设置图标,然后选择您的频道)。 这些设置是在您第一次创建频道时设置的,除非您在设备中手动更改它们,否则不会修改(至少从我卸载和重新安装我的应用程序以查看默认设置的情况来看是这样的)。
基本上,channel.setSound(null, null) 只有在首次安装时创建频道才会有效。这可能是他们试图在官方指南中解释的内容:
  

尝试使用其原始值创建现有通知频道不执行任何操作

如果您尝试遵循该指南并设置NotificationManager.IMPORTANCE_HIGH而没有使用channel.setSound(null, null),则该频道将具有默认声音的紧急程度为Urgent Make sound and pop on screen

在新安装中设置频道对我有效。谢谢! - Amit Bhandari

12

^Benjamin的回答是正确的,但他缺少了一些重要的细节!每次调整代码时,您必须更改您的频道ID,否则Oreo将无法进行更改。不确定为什么。

下面是我的代码,您可以看到必须在此处进行更改 <-------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());

    }

在https://developer.android.com/training/notify-user/channels上,您可以阅读到:“创建通知渠道后,您无法更改通知行为-此时用户拥有完全控制权。尽管您仍然可以更改渠道的名称和描述。” - oml

8

用以下代码替换您的代码

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

3

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接