Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase cloud messaging does not work after successfully sendToDevice

I use Firebase Cloud Functions for sending notification via Cloud Messaging to iOS device. sendToDevice method works great and returns success promise without any error or warning in Firebase Functions logs, but on iOS, notification is not showed. If I send notification from Firebase Notifications dashboard, it works great and notification is showed on the device. I don't know where I can have bug. It's possible that bug is on iOS app side, even when it works like I said?

Firebase configuration:

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

Part of cloud function:

APP.services.firebase.admin.database().ref('tokens/' + userId).once('value', function(snapshot) {

    var userDevicesTokens = [];

    snapshot.forEach(function(childSnapshot) {
        userDevicesTokens.push(childSnapshot.key)
    });

    if (userDevicesTokens.length === 0) {
        console.warn('No tokens for user');
        return;
    }

    var payload = {
        data: {
            title: options.title,
            text: options.text,
            type: options.type,
        }
    };
    APP.services.firebase.admin.messaging().sendToDevice(userDevicesTokens, payload)
        .then(function(response) {

            console.log("Successfully sent message:", response);

        })
        .catch(function(error) {
            console.log("Error sending message:", error);
        });

})

Firebase Cloud Functions log:

11: 52: 43.952 AM info newChatMessageTrigger
Successfully sent message: {
    results: [{
        messageId: '0:15016....'
    }],
    canonicalRegistrationTokenCount: 0,
    failureCount: 0,
    successCount: 1,
    multicastId: 589....
}

11: 52: 22.760 AM outlined_flag newChatMessageTrigger
Function execution took 604 ms, finished with status: 'ok'

11: 52: 22.252 AM outlined_flag newChatMessageTrigger
Billing account not configured.External network is not accessible and quotas are severely limited.Configure billing account to remove these restrictions

11: 52: 22.252 AM outlined_flag newChatMessageTrigger
Function execution started
like image 917
Damian Avatar asked Jul 13 '26 10:07

Damian


1 Answers

I wrote to Firebase Support and problem was with message type. In Firebase we have two types of messages: data and notification. In this case we should use type notification (see documentation).

Payload should looks like this:

var payload = {
    notification: {
        title: options.title,
        body: options.text,
    },
    data: {
        type: options.type,
    }
};
like image 175
Damian Avatar answered Jul 16 '26 00:07

Damian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!