0

I am trying to generate FCM notifications with sound. I get the notification etc without issues, but there is no sound at all. I am OK with the default sound of notifications. Please check the below code. It is for API 26 and above.

NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

                // The id of the channel.
                String id = "xxx";

                // The user-visible name of the channel.
                CharSequence name = "xxx";

                // The user-visible description of the channel.
                String description = "xxx";

                int importance = NotificationManager.IMPORTANCE_DEFAULT;

                NotificationChannel mChannel = new NotificationChannel(id, name, importance);

                // Configure the notification channel.
                mChannel.setDescription(description);

                mChannel.enableLights(true);
                // Sets the notification light color for notifications posted to this
                // channel, if the device supports this feature.
                mChannel.setLightColor(Color.RED);

                mChannel.enableVibration(true);
                mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});


                mNotificationManager.createNotificationChannel(mChannel);

                mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);



                // The id of the channel.
                String CHANNEL_ID = "xxx";

                // Create a notification and set the notification channel.
                Notification notification = new Notification.Builder(this,"xxx")
                        .setSmallIcon(R.drawable.volusha_notifications)
                        .setContentText(text)
                        .setChannelId(CHANNEL_ID)
                        .setContentIntent(pendingIntent)
                        .setContentTitle(title)
                        .setAutoCancel(true)
                        .build();

                // Issue the notification.
                mNotificationManager.notify(new Random().nextInt(), notification);

Why is this happening and how to get the default sound?

KENdi
  • 7,576
  • 2
  • 16
  • 31
PeakGen
  • 21,894
  • 86
  • 261
  • 463

1 Answers1

0

Change this part :

// Create a notification and set the notification channel.
                Notification notification = new Notification.Builder(this,"xxx")
                        .setSmallIcon(R.drawable.volusha_notifications)
                        .setContentText(text)
                        .setChannelId(CHANNEL_ID)
                        .setContentIntent(pendingIntent)
                        .setContentTitle(title)
                        .setAutoCancel(true)
                        .build();

to

// Create a notification and set the notification channel.
                Notification notification = new Notification.Builder(this,"xxx")
                        .setSmallIcon(R.drawable.volusha_notifications)
                        .setContentText(text)
                        .setChannelId(CHANNEL_ID)
                        .setContentIntent(pendingIntent)
                        .setContentTitle(title)
                        .setSound(RingtoneManager
                              .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                        .setAutoCancel(true)
                        .build();
Koustuv Ganguly
  • 897
  • 7
  • 21