gpt4 book ai didi

java - 通知自定义声音不适用于某些设备

转载 作者:行者123 更新时间:2023-12-05 00:03:29 26 4
gpt4 key购买 nike

我的应用程序中有一项服务用于在连接充电器时提醒用户,但问题是自定义声音不适用于某些设备,即使它们都是 Android 8 版本。我的代码:`

    public void notification(Intent intent) {
int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS,-1);

boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING ;

Intent enterApp_intent = new Intent(getApplicationContext(),MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(),0,enterApp_intent,0);

Bitmap largeIcon = BitmapFactory.decodeResource(getApplicationContext().getResources(),R.drawable.main_icon);

NotificationCompat.Builder charging_builder = new

NotificationCompat.Builder(getApplicationContext());
charging_builder.setContentTitle("Title");
charging_builder.setContentText("Content Text");
charging_builder.setContentIntent(pendingIntent);
charging_builder.setLargeIcon(largeIcon);
charging_builder.setSmallIcon(R.drawable.null_shape);
charging_builder.setDefaults(NotificationManager.IMPORTANCE_MAX);
charging_builder.setColor(Color.parseColor("#1fd699"));
charging_builder.setOngoing(false);

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

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){

AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.build();

NotificationChannel charging_notificationChanel = new NotificationChannel("charging_battery","charging battery",
NotificationManager.IMPORTANCE_DEFAULT);
charging_notificationChanel.setDescription("Charging notification");
charging_notificationChanel.setSound(Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE+"://"+getBaseContext().getPackageName()+"/"+
R.raw.charging_notification),audioAttributes);

charging_notificationChanel.enableVibration(false);
charging_notificationChanel.enableLights(true);

notificationManager.createNotificationChannel(charging_notificationChanel);
charging_builder.setChannelId("charging_battery");
}

if(isCharging){ // charging
notificationManager.notify(19,charging_builder.build());
}
}`

通知显示在所有设备但是一些SAMSUNG设备不存在声音?这一切都适用于 API 26。这是为什么?

最佳答案

正如您在 Create and Manage Notification 中看到的那样:如果您以 Android 8.0(API 级别 26)为目标并在未指定通知 channel 的情况下发布通知,则该通知不会出现并且系统会记录错误。

以防万一你可以尝试这个可能你需要将你的 channel ID 添加到 list 中,如下所示:

<meta-data android:name="com.google.firebase.messaging.default_notification_channel_id" android:value="@string/default_notification_channel_id" />

关于java - 通知自定义声音不适用于某些设备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66441472/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com