Firebase Cloud Message with Rest API Android - android

I am trying to build an app which will send notifications to the users according to the data changes from Rest API.
i.e A user subscribes that he wants to be notified when Spain scores the goal against Portugal. So my question is, is it possible to connect that link which sends requests to Rest API which I'm using to retrieve data to FCM or should I look for other product which will solve my problem?

You could do it with Cloud Functions, which "run backend code in response to events triggered by Firebase features and HTTPS requests", see the general doc here.
You could regularly trigger an HTTPS Cloud Functions that would:
Call the REST API, e.g. with Request (https://github.com/request/request)
Based on the response of the REST API, send a notification with FCM.
To send a FCM notification via Cloud Function, have a look a this official Firebase sample https://github.com/firebase/functions-samples/tree/master/fcm-notifications
To call your HTTPS Cloud Function regurlarly, you have to trigger it via http through a cron-job.
Have a look at: https://firebase.google.com/docs/functions/http-events and
https://www.youtube.com/watch?v=CbE2PzvAMxA. You could also use another online CRON service to call the HTTPS Function, like https://www.easycron.com/
Note that you would need to be on Blaze Plan to be able to query an outside service.

Related

Cloud Functions with Real Time Notifications and Pub/Sub - What is endpoint url?

I'm trying to implement this example: create rtdn of developer notifications, however, I use firebase, so I'll be using Cloud Functions to give onPublish in my topic subscriptions.
My question is as follows, the recommended for pub / sub notifications for Real Time Notifications is to use push notifications, since the pull is based on the assumption of the back end to exit, which does not happen in Cloud Functions, however, when clicking push on the Console, it asks for an "endpoint url", where do I get this endpoint url from or how would I generate it? In fact, even though I researched, I did not understand the reason for this.
Screenshoot example:
You do not need to make a push subscription yourself through the Pub/Sub console. If you go to ‘Cloud Functions’, create one and set the trigger to be ‘Cloud Pub/Sub’ and then choose a topic, it will create a push subscription on your behalf. After that, whenever a message is published on that topic, your cloud function will be triggered.
The URL endpoint is used when you own an HTTP address that you want Pub/Sub to push messages to.

database update notification in firebase for specific users

I am trying to create an android application and would like to use firebase database. As I read firebase allows instant messaging and also notify clients when database is updated. I need to create a database and when any database changes happen, related users needs to be notified only. Is that possible with firebase or should I use backend server for this purpose?
You can do it using the Firebase only.
What you needs are
Firebase Database (Realtime database/Firestore)
Firebase Cloud Functions
Firebase Cloud Messaging (FCM)
You should write two side codes:
Client side (Android)
Server side (Cloud Functions)
In Client side (Android)
You should register token. (To send a notification using the FCM, Token is needed.) You can do this easily (https://firebase.google.com/docs/cloud-messaging/android/client)
I recommend that you can store the token into the database/firestore too.
for example:
class User {
long id;
String email;
String token;
}
You may write the code to modify the database/firestore.
Implement receiving code (https://firebase.google.com/docs/cloud-messaging/android/receive)
In Server side (Cloud Functions)
Add some trigger to the database/firestore.
Get the token from the database/firestore
just send FCM message (https://firebase.google.com/docs/cloud-messaging/admin/send-messages)
You can get the sample code for Server side (Cloud Functions)
https://github.com/firebase/functions-samples/tree/master/fcm-notifications
Yes you can achieve this using Firebase Cloud Functions which:
lets you automatically run backend code in response to events triggered by Firebase features and HTTPS requests. Your code is stored in Google's cloud and runs in a managed environment.
And with Firebase Cloud Messaging which:
Is a cross-platform messaging solution that lets you reliably deliver messages at no cost. Using FCM, you can notify a client app that new email or other data is available to sync. You can send notification messages to drive user re-engagement and retention.
So you can write a function in Firebase Cloud Functions which will trigger a notification every time something is changing in your database. Both documetations are very well explained.

Android - Get data from server in every time

I wrote an item selling system using PHP and android.php app for sellers and android app for buyers.with android app buyers can order items from php server app.I used a slim rest API to do that.that part is ok.server app users can accept or deny buyer requests.
Now I need to get that response to android app.i used volley to send requests to server.how can I do that.
Need a code sample
You should use something like Firebase Cloud Messaging instead of regular polling to a REST API.
So when you receive message from Firebase, you can update the data accordingly.
You also may want to check Firebase Real-time database. Search for it in Google.
If you don't want to use Firebase, when response of API call comes to the app, wait for some time and make API call again.
Make a method to initiate an API call, and when you receive the response, update the UI and call that method again after some time using Handler#postDelayed

Push Notification to Specific Logged In User using Firebase/Batch

I'm building an app that allows the user to create events and add people to those events. It stores the event and certain user data (email and display name) in a Firebase database.
What I'm trying to do is send a notification to a specific user when another user adds them to an event. I've tried looking at the documentation for both Firebase Notifications/Cloud Messaging and Batch, but I can't find a way to send the notification to a single user, rather than an entire group.
How do I go about doing this? I'm hoping to avoid storing the notification in the Firebase DB and setting up a servlet backend that queries the DB repeatedly.
Edit: If I do have to make a Java Servlet backend for this, can someone tell me how I would code it?
I think you can do that by specifying the device token when sending the notification using console or using post request to FCM server. But unfortunately you have to handle every single device token manually by yourself (get it from the device when it register to FCM, and save it to your own DB somewhere).
If you use console, you can put the token on "FCM registration token" field under target -> single device.
If you use post request, you can make a request like this
{
"data": {"my_custom_key" : "my_custom_value"},
"registration_ids": ["your-device-token","your-device2-token"]
}
Batch.com provides a simpler API named Transactional API to send a notification to one or more users identified by their User ID, Firebase UID etc. as you probably don't want to deal with the complexity of APNS push tokens or GCM registration ids yourself. Contrary to FCM, you will get a complete analytics view (eg: open-rate) on those notifications.
You can trigger push notifications directly from your app code by using our Swift client for the Transactional API or by coding your own HTTP client (it's a standard POST request with a JSON body). We even have a client for Node.js if you decide to trigger the notifications from a server.
And if you are using Firebase, we have a specific documentation to plug Firebase with Batch.

Send firebase cloud message from client without exposing API secret

I'm developing a new chat application that currently works with firebase realtime database and cordova.
I was looking for a backend-less solution since my currently working app doesn't need any server at all apart from a tiny server that its only function is to provide with temporal authorization tokens for the clients.
This tokens allow the client to work directly with firebase without the need for a more expensive and loaded server, and still have a central control for the usage of the app.
By reading the new firebase documentation I believe that the notifications and the firebase cloud messages app can't be used by the client side to post messages, only to listen notifications since all the send message examples expose the server API key, which obviously can't be on the client side.
Is there a way to issue temporal tokens from a central server that can be used by the clients to send messages instead of having to send all the messages to the server and then back to the other devices?
Thanks
Sending downstream messages to devices with Firebase Cloud Messaging requires access to the authorization key. For that reason it should run in a trusted process, such as on hardware you control.
Cloud Functions for Firebase was launched today, which would solve your problem! You can initialize the firebase-admin SDK within your functions code (which runs on Google's servers, not client side), and use it to access FCM. That way you can send messages in response to new database items, or in response to HTTPS requests.
Here's an intro to Cloud Functions for Firebase: https://firebase.google.com/docs/functions/
Here's how you can use firebase-admin to send FCM messages:
https://firebase.google.com/docs/cloud-messaging/admin/send-messages

Categories

Resources