0

I'm just trying to have my app send a notification to the users when someone likes there event.

I have tried to do it this way:

    exports.likeAddedNotification = functions.database.ref('/eventLikes/{eventID}/{userWhoLikedID}').onWrite(event => { 
    const userWhoLikedID = event.params.userWhoLikedID;
    console.log("User has liked a post " + userWhoLikedID);

    admin.database().ref('events/' + event.params.eventID).once('value').then(function(snapshot) {
    var usersID = snapshot.val().userId;
    console.log("The person who made the post is " + usersID);

    //Send notification to that user
    var payload = {
      "group_id": "batch_push_sender",
      "push_time": "now",
      "message": {
        "body": usersID + " liked your event"
        },
      "recipients": {
        "custom_ids": [usersID]
        },
      "sandbox": true, // Only for iOS
    };

    notificationRequest.write(JSON.stringify(payload));
    notificationRequest.end();

    console.log("Sent a notification to " + usersID);

    });
});

(Sorry for including so much code, but I feel it may all be relevant) If I use this code and remove the

var payload = ...

and

notificationRequest.write(JSON.stringify(payload));
notificationRequest.end();

it works exactly how I'd expect. It will write to the console saying which post is liked and who liked that post.

I'm really not sure if the issue is coming from doing to much in a closure, or if I'm just doing something plain wrong, or if it's the Batch API that is causing errors.

If you know of a better site for push notifications that allows using a UDID as a custom identifier so that I don't have to manually deal with tokens for each device, please let me know!

5
  • 1
    A) make sure to return your Promises, that's the only way to guarantee that the worker will keep it going. Since notifications are sync, it's very likely your request is getting aborted. B) you should check out the Firebase Admin SDK's messaging functions. Commented Mar 22, 2017 at 18:16
  • 1
    We have an example on Github github.com/firebase/functions-samples/tree/master/… Commented Mar 22, 2017 at 18:16
  • 1
    Combined, those make a good answer James! :-) Commented Mar 23, 2017 at 13:50
  • Agreed! Make that an answer @JamesDaniels and I will accept it! Commented Mar 24, 2017 at 5:21
  • Alright, combined and gave it a bit less of a bullet point format. 👍 Commented Mar 24, 2017 at 8:07

1 Answer 1

1

First, make sure to return your Promises, that's the only way to guarantee that the Cloud Functions worker will keep things going. Since Firebase Cloud Notifications are sync, it's very likely your request is getting aborted.

You should also check out the Firebase Admin SDK's messaging components. We've built in some nice APIs for FCM. We have an example on how to use these in Cloud Functions for Firebase on GitHub https://github.com/firebase/functions-samples/tree/master/fcm-notifications

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks! So just to be clear, do you mean to add a return before admin.database().ref('events/' + event.params.eventID).once ? I don't use javascript very often so I apologize for how simple of a question this has to be.
Yes. And also return your notificationRequest inside the then, which would keep the promise going during that call too assuming that whatever request lib you're using supports promises.
If you're building it manually using request, I'd suggest you check out request-promise which will give you a nice wrapper.
Great. Thanks for all of your help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.