一些设备无法使用自定义通知音

4

我的应用程序中有一个服务,用于在连接充电器时向用户发出警报,但问题是即使所有设备都是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());
    }
}`

通知在所有设备上都能显示,但是一些三星设备不存在声音? 这一切都是在API 26上运作的。为什么会这样呢?

请分享您设备的型号或品牌。 - sina akbari
@sinaakbary 这是三星。 - a7d.24_
2个回答

0

正如您在创建和管理通知中所看到的: 如果您的目标是Android 8.0(API级别26),并且发布通知时没有指定通知渠道,则通知不会出现,系统将记录错误。

以防万一,您可以尝试这个方法,可能需要将您的通道ID添加到清单中,类似于以下内容:

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


0

请确保您正在执行以下两个操作:

  1. 正确的路径应该像这样 channel.setSound(Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + getPackageName() + "/raw/ice"), att);

  2. 每当您更改通知渠道配置时,要么重新安装应用程序,要么清除数据。

  3. 请确保同时添加Channel.setSound和NotificationCompat.Builder.setSound


所有代码都是正确的,就像我所说的,声音在某些设备上可以正常工作,重新安装应用程序并没有改变任何事情。感谢您的回答。 - a7d.24_
请确保同时添加 channel.setSound 和 Notification.Builder.SetSound,因为在某些设备上 channel.setSound 可能不起作用。 - shafaq ahsan

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