Damian
Damian

Reputation: 608

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

Upvotes: 3

Views: 5091

Answers (2)

Surjeet Singh
Surjeet Singh

Reputation: 11939

I think it is a issue with your payload.

Try this payload once with your firebase function, and let me know if you received notification on iOS.

var payload = {
        data: {
            title: "Welcome to My Group",
            message: "You have new messages"
        }
    };

Upvotes: 3

Damian
Damian

Reputation: 608

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

Upvotes: 6

Related Questions