0

I am working on an application based on Push Notifications (Custom Notifications). I am using FCM and javascript (index.js). I am able to trigger push notification perfectly but the problem is sound. I want Push Notification with Sound in one condition (if) and without Sound in other (else). What is happening is, in both conditions I am getting sound. How can I trigger a push notification without sound?

String remoteMessageSound = remoteMessageData.get(REMOTE_SOUND);
            if (remoteMessageSound.equals("default")) {
                Notification notificationDefault = new NotificationCompat.Builder(this, getString(R.string.default_notification_channel_id))
                        .setSmallIcon(R.drawable.namma_apartment_notification)
                        .setAutoCancel(true)
                        .setContentTitle(getString(R.string.app_name))
                        .setContentText(message)
                        .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                        .setPriority(PRIORITY_DEFAULT)
                        .build();

                Objects.requireNonNull(notificationManager).notify(mNotificationID, notificationDefault);
            } else {
                Notification notificationNoSound = new NotificationCompat.Builder(this, getString(R.string.default_notification_channel_id))
                        .setSmallIcon(R.drawable.namma_apartment_notification)
                        .setAutoCancel(true)
                        .setContentTitle(getString(R.string.app_name))
                        .setContentText(message)
                        .setSound(null)
                        .setPriority(PRIORITY_LOW)
                        .build();

                Objects.requireNonNull(notificationManager).notify(mNotificationID, notificationNoSound);
            }

And this is my index.js:

if (cabNotificationSound) {
            payload = {
                data: {
                    message: "Your Cab has " + status + " your society.",
                    "sound": "default",
                    type: "Cab_Notification"
                }
            };
        }
        else {
            payload = {
                data: {
                    message: "Your Cab has " + status + " your society.",
                    "sound": "",
                    type: "Cab_Notification"
                }
            };
        }


        return admin.messaging().sendToDevice(tokenId, payload).then(result => {
            return console.log("Notification sent");
        });     
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
Ashish Jha
  • 173
  • 1
  • 3
  • 18

1 Answers1

0

Try something like this to set the sound directly in the channel before sending the notification (for api >= 26):

NotificationManager manager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationChannel channel = manager.getNotificationChannel(getString(R.string.default_notification_channel_id));
channel.setSound(null, null);
Javi Mollá
  • 786
  • 7
  • 18