Firebase cloud functions: sending multiple messages

420 views
Skip to first unread message

Ankit Jaiswal

unread,
Jan 10, 2018, 10:37:48 AM1/10/18
to Firebase Google Group
I'm trying to send multiple notifications from one cloud function. There are two use cases:

1. In a group chat, I've to create two separate payloads for android and iOS devices and then send notifications through the same cloud function for both the payloads.
2. There is a specific case, wherein, on an event on a collection, I need to send multiple messages to the same recipient. 

I tried with the below code:

var promises = new Array();

            if (androidMemberToken.length > 0) {
            var androidPromise = admin.messaging().sendToDevice(androidMemberToken, payload, options)
                .then(function(response) {
                    console.log("Message sent: ", response);
                })
                .catch(function(error) {
                    console.log("Error sending message: ", error, payload);
                });
            promises.push(androidPromise);
            }

            if (iosMemberToken.length > 0) {
            payload['notification'] = info;
            var iosPromise = admin.messaging().sendToDevice(iosMemberToken, payload, options)
                .then(function(response) {
                    console.log("Message sent: ", response);
                })
                .catch(function(error) {
                    console.log("Error sending message: ", error, payload);
                });
            promises.push(iosPromise);
            }
            
            return Promise.all(promises); 

This is for group chat. I'm not getting any error message, however the notifications are not sent. Cloud Function logs do not show any success/error messages. What should be the correct approach for the above two cases for sending multiple notifications?

Regards

Kato Richardson

unread,
Jan 10, 2018, 1:26:24 PM1/10/18
to Firebase Google Group
Hi Ankit,

Did you verify those code blocks get executed? I.e. are iosMemberToken.length and androidMemberToken.length actually greater than zero? Did you confirm the function is actually invoked?

☼, Kato

--
You received this message because you are subscribed to the Google Groups "Firebase Google Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firebase-talk+unsubscribe@googlegroups.com.
To post to this group, send email to fireba...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/firebase-talk/ad8c2ad6-e2b5-4736-9ae2-1b16cc323b73%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--

Kato Richardson | Developer Programs Eng | kato...@google.com | 775-235-8398

Ankit Jaiswal

unread,
Jan 11, 2018, 5:23:32 PM1/11/18
to fireba...@googlegroups.com
Thanks Kato, indeed there was some issue with the logic and token arrays were not populated correctly because of that. Code is working fine now.

Kato Richardson

unread,
Jan 12, 2018, 11:06:06 AM1/12/18
to Firebase Google Group
Nice! We like the simple problems : )

Great to hear from you. Hope to see you on the list again soon.

☼, Kato

On Thu, Jan 11, 2018 at 10:44 AM, Ankit Jaiswal <jaiswal....@gmail.com> wrote:
Thanks Kato, indeed there was some issue with the logic and token arrays were not populated correctly because of that. Code is working fine now.

For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages