在 Notification.Builder 中,setDefaults 已经被废弃。

6

Notification.Builder中的setDefaults在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());`

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

4个回答

7

setDefaults应该被以下内容替换:

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

参考资料

setSound 应该被以下内容替换:

  • NotificationChannel.setSound(Uri, AudioAttributes)

参考资料


2

我现在使用这个函数,效果非常完美。

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

}

1

来自文档

public Notification.Builder setDefaults (int defaults)

此方法在API级别26中已弃用。请改用NotificationChannel.enableVibration(boolean)和NotificationChannel.enableLights(boolean)以及NotificationChannel.setSound(Uri,AudioAttributes)。

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

此方法在API级别26中已弃用。请改用NotificationChannel.setSound(Uri,AudioAttributes)。

您需要更改方法的签名。


1

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