I'm trying to set a custom sound to my notification from Firebase cloud messaging but I can't figure out why isn't working. I thing I'm setting correctly the notification channel properties. But I'm beginner on cloud functions and on android notifications.
My notification code:
AudioAttributes attributes = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.build();
Uri sound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" +
getApplicationContext().getPackageName() + "/" + R.raw.notification_sound);
final String CHANNEL_ID = "MAIN";
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, "Main", NotificationManager.IMPORTANCE_HIGH);
getSystemService(NotificationManager.class).createNotificationChannel(channel);
channel.enableVibration(true);
channel.setImportance(NotificationManager.IMPORTANCE_HIGH);
channel.setSound(sound, attributes);
Notification.Builder notification = new Notification.Builder(this, CHANNEL_ID)
.setContentTitle(notificationTitle)
.setContentText(notificationBody)
.setSmallIcon(R.drawable.app_logo)
.setAutoCancel(false);
notification.setContentIntent(pendingIntent);
NotificationManagerCompat.from(this).notify(1, notification.build());
My cloud functions code:
//import firebase functions modules
const functions = require('firebase-functions');
//import admin module
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
const db = admin.firestore();
exports.notifications = functions.firestore.document("notifications/{userId}").onCreate((snap, context) => {
const values = snap.data();
//db.collection("notifications").add({name: "notification " + values.token});
const token = values.token;
const tokens = [token];
// c4FREruMT42IZpuSBF47X_:APA91bGJvT1ny46JeE5rp9R6M5usSWwUAlIGi88xWknPm4Uh73ybUrOvcaCiHbN3MUfKOzJCKEmvc_qoqpueQyE_7igeG_hVdem2wvX53cPI0X6GPEiQOjyhAIeS8QlgZNNaJstsHl42
// Create a notification
const payload = {
notification: {
channel_id: "Main",
sound: "notification_sound.wav",
priority: "high",
title: values.title,
body: values.message,
}
};
//Create an options object that contains the time to live for the notification and the priority
const options = {
priority: "high",
timeToLive: 60 * 60 * 24
};
//return admin.messaging().sendToTopic("pushNotifications", payload, options);
return admin.messaging().sendToDevice(tokens, payload, options);
});