Magnus
Magnus

Reputation: 1085

Enable default sound on FCM notifications for both android and iOS

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 :)

Upvotes: 14

Views: 15051

Answers (1)

Magnus
Magnus

Reputation: 1085

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

Upvotes: 35

Related Questions