首页 > 解决方案 > 在 Notification.Builder 中不推荐使用 setDefaults

问题描述

setDefaults在 Notification.Builder 中已弃用 android O 及更高版本 (SDK >= 26)

setSound

这是我的代码

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        b.setAutoCancel(true)
                .setSmallIcon(R.drawable.ic_luncher_new)
                .setContentTitle(Title)
                .setTicker(Title)
                .setContentText(Msg)
                .setChannelId("cid")
                .setDefaults(Notification.DEFAULT_ALL)
                .setStyle(new Notification.BigTextStyle().bigText(Msg))
                .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                .setContentIntent(contentIntent);
    }
NotificationManager notificationManager = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        notificationManager.createNotificationChannel(mChannel);
    }
    notificationManager.notify(id, b.build());`

我应该更换什么?我没有找到任何有用的例子

标签: androidnotificationsandroid-8.0-oreo

解决方案


setDefaults应使用以下内容替换

  • NotificationChannel.enableVibration(boolean)
  • NotificationChannel.enableLights(布尔值)
  • NotificationChannel.setSound(Uri, AudioAttributes)

来源

setSound应替换为

  • NotificationChannel.setSound(Uri, AudioAttributes)

来源


推荐阅读