Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

232
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda