即使我没有设置声音,Android Oreo通知也会继续发出声音.在旧版本,完美的工作

Ami*_*ari 24 android android-8.0-oreo

因此,我正在使我的应用程序与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);
Run Code Online (Sandbox Code Playgroud)

Lov*_*ove 38

查看通知频道设置(刷一下您的通知并按下它下方的设置图标,然后选择您的频道).这些设置是在您第一次创建频道时设置的,除非您在设备中手动执行此操作,否则不会进行修改(至少这是我从卸载和重新安装我的应用程序以查看默认情况下获得的设置的经验).

基本上,channel.setSound(null, null) 只有在全新安装时创建通道才会生效.这可能是他们试图在官方指南中解释的内容:

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

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


小智 8

^本杰明回答有效,但他缺少一些重要的细节!每次调整代码时都必须更改频道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());

    }
Run Code Online (Sandbox Code Playgroud)


Ben*_*min 6

用这个替换你的代码

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");
    }
Run Code Online (Sandbox Code Playgroud)