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.
Related
I want to create social networking application whose features will be:
Authentication
Send Messages on newsfeed activity(visible to all the authenticated people)
when somebody comment/likes your message send notification
Followers
Followings
when somebody follows you send notification
Is firebase realtime database enough for this or I have to use firebase cloud function? or AWS dynamoDB or what would be the good combination to achieve desired functionality?
Thanks
You need to use Firebase Cloud Functions to send notification automatically on follow/comment/like.
All what you need is Firebase.
AWS is pretty much harder to init than Firebase.
You can see here some samples of using Firebase Cloud Functions.
This sample will help you to send notifications on follow/comment/like.
I need to retrieve data from firebase database while app is in background or closed. For this, I have been thinking of using database reference in a service.
Instead of this, people recommend using Firebase Cloud Messaging. But what exactly is that?
How does is interact with the database? How do I use it for listening and retrieving data? There are very little explanation on these and firebase docs are too broad. Or maybe I just cant find it
But what exactly is that?
Firebase Cloud Messaging (FCM) is the latest version of Google Cloud Messaging (GCM), which is Google's Push Notification Service.
It allows developers to send data from their App Server towards the Client App. For your case, you may be able to send over the desired data towards your Client App, by integrating your Database with FCM.
How does is interact with the database? How do I use it for listening and retrieving data?
If you are using Firebase Realtime Database, you may choose to integrate with FCM in such a way (referring to #FrankvanPuffelen's answer):
Sending messages to devices based on inserts into the Firebase Database will require you to run a trusted process, typically on an app server that you control. This trusted process listens to the database changes and then calls Firebase Cloud Messaging to send the messages.
For an example of how to send messaging from a node.js script, see my answer here: How to send Firebase Cloud Messaging from a node server?
In summary, you don't use it to listen for data. You can however use it as the medium to send the data from your Database towards your Client App.
Since you are implementing Firebase for the first time, I'd recommend you to go through this and get a basic idea about it.
Broadly, FCM (previously GCM) is a Cloud Messaging service for your app. Most importantly, the Analytics and Realtime Database of Firebase are used.
Here is a link to their docs. Go through it for better understanding of Firebase and its implementation.
I want something similar to Facebook messenger and
I don't have much knowledge about web servers,can i use them?
It should be like
I accept on String from user and then upload it to my server and program the server to send it to other device and the other device gets notified by a background service
To create something similar to Facebook messenger, you could do a lot of things.
Just to start, in my opinion, a Serverless service. Firebase could be your solution or AWS (or any other service). It has a Push notifications and database where to store that Strings.
Firebase Cloud Messaging
Firebase Cloud Messaging (FCM) 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 reengagement and retention. For use cases such as instant messaging, a message can transfer a payload of up to 4KB to a client app.
Firebase Realtime Database
The Firebase Realtime Database is a cloud-hosted database. Data is stored as JSON and synchronized in realtime to every connected client. When you build cross-platform apps with our iOS, Android, and JavaScript SDKs, all of your clients share one Realtime Database instance and automatically receive updates with the newest data.
Other way is open a socket in your Android app and handle all the events. I recommend you Socket.io to do that.
You could use node.js in your server to create a socket server and use the Android client.
http://socket.io/blog/native-socket-io-and-android/
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.
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