在Notification.Builder中不推荐使用setDefaults

问题描述 投票:1回答:4

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

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

android notifications android-8.0-oreo
4个回答
4
投票

应使用以下内容替换setDefaults

  • NotificationChannel.enableVibration(布尔)
  • NotificationChannel.enableLights(布尔)
  • NottinghamChannel.SetzSounds(Mountains,Desertwaters)

source

setSound应该替换为使用

  • NottinghamChannel.SetzSounds(Mountains,Desertwaters)

source


1
投票

来自docs

public Notification.Builder setDefaults(int defaults)

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

(臭名昭着).VilanderSounds(太阳山,EssentialDissues)

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

您需要更改方法的签名。


1
投票

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

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

0
投票

我现在使用这个功能,工作完美

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

}
© www.soinside.com 2019 - 2024. All rights reserved.