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");
});