Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

230
Vistas
Can't sent notification with custom sound from cloud functions

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);
});
about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda