8

After several hours searching the web for answers on how to enable the default sound on Firebase cloud messaging notifications, for both android and iOS, I finally figured it out by myself. I could not really find any answers to this problem anywhere on the web, so I thought I should post the answer here.

Hope this helps :)

Magnus
  • 895
  • 3
  • 9
  • 20

1 Answers1

24

This particular snippet is written in node.js, but except from the 3 first lines, the syntax is the same in Typescript.

    const functions = require('firebase-functions');
    const admin = require('firebase-admin');
    admin.initializeApp(functions.config().firebase);

    const payload = {
        notification: {
            title: "X",
            body: "XXX",
        },
        android: {
            notification: {
                sound: 'default'
            },
        },
        apns: {
            payload: {
                aps: {
                    sound: 'default'
                },
            },
        },
        topic: 'X'
    };
    return admin.messaging().send(payload).then(response => {
            console.log("Successfully sent message:", response);
        })
        .catch(function (error) {
            console.error("Error sending message:", error);
        });
Magnus
  • 895
  • 3
  • 9
  • 20
  • Right.. Thanks, there is a documentation here: https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages#resource:-message but the one you have is straightforward. – harlandgomez Jan 13 '21 at 09:21
  • I'm getting - Messaging payload contains an invalid \"android\" property. Valid properties are \"data\" and \"notification\.... :\ – genericUser May 09 '21 at 16:35
  • Make sure you are using `messaging().send` and not `messaging().sendToDevice` – genericUser May 09 '21 at 16:41