0

Be informed we are trying to send push notification to a device with custom sound. The custom mp3 file (120 seconds) is stored in res/raw file. The notification is sent successfully, but the sound never plays out even after many tries. The code is as given below:

Update:Based on suggestion given in the comments, created a notificationchannel and set the sound in it, but still its not working. Worse, unable to build apk at all.

Uri soundUri= Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + BuildConfig.APPLICATION_ID + "/" + R.raw.adhan);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, MainActivity.asw_fcm_channel)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle(title)
                .setContentText(message)
                .setAutoCancel(true)
                .setSound(soundUri)
                .setContentIntent(pendingIntent);
        Notification noti = notificationBuilder.build();
        noti.flags = Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE | Notification.FLAG_AUTO_CANCEL;

    

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

            NotificationChannel mChannel = new NotificationChannel("345",
                    "new",
                    NotificationManager.IMPORTANCE_HIGH);

            AudioAttributes attributes = new AudioAttributes.Builder()
                    .setUsage(AudioAttributes.USAGE_NOTIFICATION)
                    .build();

            // Configure the notification channel.
            mChannel.setDescription(message);
            mChannel.enableLights(true);
            mChannel.enableVibration(true);
            mChannel.setSound(soundUri, attributes); // This is IMPORTANT


            if (NotificationManager != null)
                NotificationManager.createNotificationChannel(mChannel);
        }

    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(notification_id, notificationBuilder.build());

We are at loss as where we are going wrong. Please guide us as how we solve this issue.

Ahamed
  • 163
  • 1
  • 10

0 Answers0