首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >通知自定义声音不适用于某些设备

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

Stack Overflow用户
提问于 2021-03-02 14:39:09
回答 2查看 166关注 0票数 3

我的应用程序中有一项服务,用于在充电器连接时提醒用户,但问题是,一些设备即使都是Android 8版本,定制声音也不能工作。我的代码:‘

代码语言:javascript
复制
    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一起工作,为什么呢?

EN

回答 2

Stack Overflow用户

发布于 2021-03-07 13:53:55

正如您在创建和管理通知中所看到的,如果您针对Android8.0( appear 26)并在不指定通知通道的情况下发布通知,则通知不会出现,并且系统会记录错误。

万一您可以尝试这种方法,您可能需要将您的通道id添加到清单中,如下所示:

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

票数 0
EN

Stack Overflow用户

发布于 2021-03-11 12:49:39

确保你在做这两件事:

  1. 正确的路径类似于这个channel.setSound(Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://“+ getPackageName() + "/raw/ice"),att);
  2. 每当您在通知通道配置中做一些更改时,要么重新安装应用程序,要么清除数据。
  3. 确保将Channel.setSound和NotificationCompat.Builder.setSound同时添加到一起
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66441472

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档