notificationcompat.builder设置振动和设置声音

bis0qfac  于 2021-07-12  发布在  Java
关注(0)|答案(0)|浏览(297)

在我的项目中,除了setvibrate和setsound.as sound,通知都可以工作。我想设置一些默认值。设备是小米。有什么特别的吗?比如小米不允许应用在后台运行,也不允许改变报警管理器的行为。
这是我的密码

public class Broadcast extends BroadcastReceiver {
    private String CHANNEL_ID="15";

    @Override
    public void onReceive(Context context, Intent intent) {

        NotificationCompat.Builder builder
                =new NotificationCompat.Builder(context,CHANNEL_ID)
                .setSmallIcon(android.R.drawable.sym_def_app_icon)
                .setContentTitle("It's time to train")
                .setContentText(intent.getStringExtra("muscle"))
                .setPriority(4)
                .setVibrate(new long[] {1000, 1000,1000})
                .setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
                .setLights(Color.GREEN,1,1)
                .setAutoCancel(true);

        Intent actionIntent = new Intent(context, DetailActivity.class);
        actionIntent.putExtra("muscle",intent.getStringExtra("muscle"));
        actionIntent.putExtra("exercise",intent.getStringExtra("exercise"));
        actionIntent.putExtra("count",intent.getStringExtra("count"));
        PendingIntent actionPendingIntent = PendingIntent.getActivity(
                context,
                0,
                actionIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);
        builder.setContentIntent(actionPendingIntent);

        NotificationManager notificationManager =
                (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel =
                    new NotificationChannel(CHANNEL_ID, "Notification Channel",
                            NotificationManager.IMPORTANCE_DEFAULT);
            AudioAttributes att = new AudioAttributes.Builder()
                    .setUsage(AudioAttributes.USAGE_NOTIFICATION)
                    .setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)
                    .build();

            channel.setSound(Settings.System.DEFAULT_NOTIFICATION_URI,att);
            notificationManager.createNotificationChannel(channel);
        }
        notificationManager.notify(intent.getIntExtra("not_id",0), builder.build());

    }
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题