notification custom sound not working with some devices

Viewed 153

I have a services in my app Which is used to alert the user when the charger is connected, but the problem is that the custom sound does not work with some devices even though they are all Android 8 version. My code : `

    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());
    }
}`

The notification is show at all devices but some SAMSUNG devices don't exists a sound? and it's all working with API 26.why that?

2 Answers

As you can see in Create and Manage Notification: If you target Android 8.0 (API level 26) and post a notification without specifying a notification channel, the notification does not appear and the system logs an error.

Just in case you can try this one that Probably you need to add your channel id to the manifest something like this:

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

Make sure you are doing these two things:

  1. Correct path is like this channel.setSound(Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + getPackageName() + "/raw/ice"), att);

  2. Whenever you do some changes in notification channel config, either re-install app or clear data.

  3. Make sure you are adding both Channel.setSound and NotificationCompat.Builder.setSound both together

Related