Notification.Builder 中弃用了 setDefaults

setDefaults is deprecated in Notification.Builder

setDefaults in 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());`

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

来自docs

public Notification.Builder setDefaults (int defaults)

This method was deprecated in API level 26. use NotificationChannel.enableVibration(boolean) and NotificationChannel.enableLights(boolean) and NotificationChannel.setSound(Uri, AudioAttributes) instead.

public Notification.Builder setSound (Uri sound, AudioAttributes audioAttributes)

This method was deprecated in API level 26. use NotificationChannel.setSound(Uri, AudioAttributes) instead.

您需要更改方法的签名。

从Android O(API 26级)开始,所有通知都必须分配给一个频道。 可以使用以下专用方法在通知通道上设置这些配置:

  1. NotificationChannel.enableVibration(boolean)
  2. NotificationChannel.enableLights(boolean)
  3. NotificationChannel.setSound(Uri, AudioAttributes)

setDefaults 应替换为以下

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

source

setSound 应替换为

  • NotificationChannel.setSound(Uri, AudioAttributes)

source

我现在用这个功能,效果很好

private void CreateNotificationMessage(Context ctx,String Title,String Msg){
    int id=15;
    Intent intent= new Intent(ctx, MainActivity.class);
    PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0, intent, 0);
    Notification.Builder b = new Notification.Builder(ctx);

    NotificationChannel mChannel = null;
    b.setAutoCancel(true)
            .setSmallIcon(R.drawable.ic_luncher)
            .setContentTitle(Title)
            .setTicker(Title)
            .setContentText(Msg)
            .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
            .setContentIntent(contentIntent);



    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        mChannel = new NotificationChannel("cid", "name",NotificationManager.IMPORTANCE_HIGH);
        b.setChannelId("cid");
        mChannel.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION), new AudioAttributes.Builder()
                .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                .setUsage(AudioAttributes.USAGE_NOTIFICATION_RINGTONE)
                .build());
    }

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

}