firebase / functions-samples

Collection of sample apps showcasing popular use cases using Cloud Functions for Firebase
https://firebase.google.com/docs/functions
Apache License 2.0
12k stars 3.84k forks source link

Calling "sendFollowerNotification" from Android #432

Open TeamRainless opened 5 years ago

TeamRainless commented 5 years ago

Regrettably, Google Groups says that they are NOT for questions about code, and I received absolutely no help from Stackoverflow. I've read all of the docs and scoured the internet, but I have not found a single answer... or even the question:

How would you call the "sendFollowerNotification" function from an Android app?

There is LITERALLY nowhere else on the face of the Earth I can ask this question... unfortunately. And I have thoroughly exhausted myself for the better part of two months looking.

I've reached a point where my app automatically adds users and notification tokens to the realtime database... but I still have no idea how to trigger the function that will ultimately send those users a follower notification.

I'm aware that you can call functions like this:

FirebaseFunctions.getInstance()
    .getHttpsCallable("myCoolFunction")
    .call(optionalObject)
    .addOnFailureListener {
        Log.wtf("FF", it) 
    }
    .addOnSuccessListener {

    }

...but I'm unsure of what variable to send to something like sendFollowerNotification... or how to send it. If you could explain it here that would already be great... and even better if you added that information to the official README.

nicolasgarnier commented 5 years ago

@TeamRainless the snippet of code you are showing above is for calling a "callable function", it could work but that's not what I'm using in the FCM sample here. Instead we rely on a "database trigger". The function triggers automatically when a certain path is updated in the database. In particular in the sample when a user starts following another user he writes a boolean true to the path /followers/{followedUid}/{followerUid}. When this data is written the function server-side is triggered.

This way we have a server side function which is triggered as soon as a user starts following another user and when the data reflects that.

So to answer your question: to trigger the function from android you need to write data at the /followers/{followedUid}/{followerUid} path.

TeamRainless commented 5 years ago

That's a pretty damned fast reply... I should've written to you first! :)

Unfortunately I already figured it out about 20 seconds after I wrote to you. (That's what ALWAYS happens...)

The users that I create in my app don't have photos. This created an error in my Functions log for "notification.icon". That's when I realized that "sendFollowerNotification" was listening to the RealTime Database and would send a notification whenever it was updated.

I wrote the following reply to my own question on StackOverflow explaining the whole thing.

https://stackoverflow.com/questions/51235813/calling-sendfollowernotification-firebase-function-from-android-app/51261308#51261308

It might be worth adding a few more words to the original ReadMe on "sendFollowerNotification" so that it's a bit more clear what's happening.

(Or maybe it was just me taking a while to come around to how the whole thing works.)

Either way I really do appreciate you getting back to me so fast and I think you from the bottom of my heart.

Have a wonderful week!