代码之家  ›  专栏  ›  技术社区  ›  Radesh

在notification.builder中不推荐使用setdefaults

  •  1
  • Radesh  · 技术社区  · 7 年前

    setDefaults 在通知中,android o及更高版本中不推荐使用builder(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 回复  |  直到 6 年前
        1
  •  5
  •   pau1adam    7 年前

    setDefaults 应替换为使用以下内容

    • NotificationChannel.enableVibration(布尔值)
    • 通知通道
    • 设置声音(URI,音频属性)

    source

    setSound 应替换为使用

    • notificationchannel.setSound(uri,音频属性)

    source

        2
  •  1
  •   2Dee    7 年前

    docs 以下内容:

    public notification.builder设置默认值(int默认值)

    此方法在API级别26中已弃用。使用 NotificationChannel.enableVibration(布尔值)和 通知通道(布尔)和 NoTimeChansChanv.StValm(URI,AudioAtvices)代替。

    public notification.builder setsound(uri声音,audioattributes音频属性)

    此方法在API级别26中已弃用。使用 NoTimeChansChanv.StValm(URI,AudioAtvices)代替。

    您需要更改方法的签名。

        3
  •  1
  •   Sagar    7 年前

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

    1. NotificationChannel.enableVibration(boolean)
    2. NotificationChannel.enableLights(boolean)
    3. NotificationChannel.setSound(Uri, AudioAttributes)
        4
  •  0
  •   Radesh    7 年前

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

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