【问题标题】:notification custom sound not working with some devices通知自定义声音不适用于某些设备
【发布时间】:2021-06-01 02:51:55
【问题描述】:

我的应用中有一个服务,用于在连接充电器时提醒用户,但问题是自定义声音不适用于某些设备,即使它们都是 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 一起工作。为什么会这样?

【问题讨论】:

  • 请分享您设备的型号或品牌。
  • @sinaakbary 这是三星

标签: java android android-studio notifications android-notifications


【解决方案1】:

确保你正在做这两件事:

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

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

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

【讨论】:

  • 所有代码都是正确的。就像我说的声音在某些设备上工作并且重新安装应用程序并没有改变任何事情。感谢回答
  • 确保同时添加 channel.setSound 和 Notification.Builder.SetSound 因为在某些设备中 channel.set 声音不起作用
【解决方案2】:

正如您在Create and Manage Notification 中看到的: 如果您以 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" />

【讨论】:

    猜你喜欢
    • 2021-11-15
    • 1970-01-01
    • 2021-07-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-08
    • 2018-02-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多