在Notification.Builder中不推荐使用setDefaults

Rad*_*esh 1 notifications android android-8.0-oreo

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

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

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

pau*_*dam 6

setDefaults 应该用以下代替

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

资源

setSound 应该用代替

  • NotificationChannel.setSound(Uri,AudioAttributes)

资源