I'm creating an ecommerce app and I want to send promotional notifications to users from firebase cloud messaging. I want to show all the notifications in recycler view in notification screen using adapter can anyone tell how do I save all incoming notifications and display them in notification screen
If you want to show all notifications you need to save those in
local database when you receive and fetch all on required screen.
You can use Room database for the same.
You can ask the backend developer to save all notifications at
backend side and fetch those by an API.
Approach 1
See If you are sending a notification from the firebase console, Then you need to save that notification in the local DB While It receives in Mobile Firebase Messaging Service Class. (but It's not a good approach, Because user loss notification in case of clear data for app or reinstall app)
Approach 2
If you are sending notifications using some API or firebase function Then you can create one collection of Notifications and save notifications every time while sending And Fetch notifications from the same collection.
Related
The Firebase Console allows us to send notifications to single users, groups, users subscribed to topics or to the entire user base. Is there any code that allows us to directly send notifications in the same way, but programmatically?
For example, if I had a list of users (containing their Firebase UIDs), if I click on one of the users, could I send a notification to that user through Firebase the same way the console allows us to send a notification through the console?
This Firebase Blog uses the Google App Engine Flexible Environment to actually send the notifications, but it requires a free trial and costs money.
This Quickstart doesn't really show how to send user to user notifications. It focuses on the subscription-based notifications, but this isn't really what I need.
Is there any good way of doing this with Java/Kotlin and the Firebase API?
FCM doesn't support you sending message directly from Android app to Android app. However, this is a workaround to solve your problem:
Create a firebase cloud function. Listen a special path in firebase database
Android app 1 push a data (maybe Android app 2 ID) to this path
Firebase cloud function process data, determine who to send notification (Android app 2).
Firebase cloud function push notification to Android app 2.
ref: https://firebase.google.com/docs/functions/use-cases#notify_users_when_something_interesting_happens
In a basic concept: we write our server to get action from firebase database and decision to send notification to other user by FCM. This our server can write with NodeJS or using Firebase Cloud Function (above)
Guide how to using Nodejs is here: https://firebase.googleblog.com/2016/08/sending-notifications-between-android.html
Okay, I know the question may sound stupid. I’m building an app where there are pages that publish posts and users can subscribe/unsubscribe to those pages.
The goal here is to find a way to send notifications whenever a new post is published but only to the subscribed users. I though that I can do this by sending a push notification to all the devices on my app whenever the “Posts” reference is updated on Firebase database, and then choose whether to show this notification or not if the user is subscribed (on client side)
Is this a good idea? And if yes, how can I accomplish that?
Is this a good idea?
Yes, the idea is not bad. However, filtering notification to be shown in client side is done in many cases.
And if yes, how can I accomplish that?
I hope you already have a login or authentication system using Firebase authentication or any other server side authentication. When you have this, you might have already considered sending a push registration id to your server or firebase when a user signs up in your application and save it in your firebase database.
Now when its time to send a push notification, you are planning to send the push notification to all of your registered devices and you want to filter the notification will be shown or nor in the client side.
This can be achieved by keeping a flag in the client side, for example a SharedPreference having the id or tag of the last post. If you have an incremental id for each post, then it will be a lot easier to implement. When a user launches your application, it pulls the posts from your firebase database as I can think of. Just save the latest id of the post in your SharedPreference and when a push is received, match the id of the post that came along with the push notification with the latest id stored locally.
If the id received via push notification is greater than the id stored in your SharedPreference, you will show the notification in system tray and the notification will not be shown otherwise.
Hope that helps.
You can push notification to only subscribed users if you are maintaining a list of subscribed users FCM token in your database
Your approach is correct, what you would need to do is that maintain different database tables for all pages where users are inserted when they subscribe to that particular group/table.
So, let's suppose when a person subscribes to the page containing information about Sports, you add him to that group. Later, when you are sending update/notifications related to 'Sports' you would only send those push notifications to the user-tokens in 'Sports' table.
In this way, only relevant subscribers would receive those push notifications.
I see here is event triggers https://firebase.google.com/docs/functions/database-events for RTDB and I watch onCreate() method.
onCreate()`, which triggers when new data is created in the Realtime
Database.
When I create new insert into RTDB in this method I can notify user about new insert into database.
But my question is now I can make here notification and all mobile device in same time get notification?
Why I need to use FCM. With FCM I can choose which mobile device get notification and many more features about notification?
The Firebase Database client in your app is (normally) only active when the app is running and in the foreground. So it can only receive the new data, while the user is actively using your app.
The Firebase Cloud Messaging SDK includes a service that is also listening for messages when the app is not in the foreground. That means that with FCM you can also notify the users when they are not actively using your app.
It is quite common to use a combination of the Firebase Database and Cloud Messaging:
If one user changes the state, write that change to the database.
Then use FCM to notify the other users that "something" has changed.
Finally when the other users open the app again, the app gets the latest state from the database.
I have a cross platform application and i want to send notification to sign in users about their messages.
Now I confused about uses of push, local notifications.
What I think of Push notifications is that it is for sending Announcements to users which is not specifically related to their account only.
Can anyone help me out with what should I use? I already used Local notifications in one of my applications with such requirement.
It mainly depends on; is the data coming from local or remote?
You cannot control when your users open the app, and only when they open the app (with a few exceptions) you are able to fetch data. Then with that data you would be able to schedule a local notification. But in most cases that doesn't make much sense, because they have already loaded and probably seen the data. It only makes sense when you schedule an alarm clock for instance.
When you want the data to come from remote, like when they receive a message, you will have to use push notifications. The user is then alerted that new data is available without having to go look for it themselves. It is pushed to them.
However, for push notifications you will need infrastructure which you did not when using local notifications. You will need a server to handle the push notifications (Azure has some awesome functionalities for this) and some trigger to send push notifications. This can be an insert on a database, or a scheduled task. Also, the user has to enable push notifications and your app has to register itself to be able to receive them. It can be a pain to implement it the first time.
It depends on for what reason you're sending the notification.
A local notification is sent locally on the device, so it doesn't need an internet connection. Examples could be:
Send a birthday message when the user has birthday
In a harvesting game, send a local notification when the store is full
A Push Notification is sent from a server and it requires internet on your device to receive it. Examples:
You get a message in a chat while the app is not open (if I understand your question right, this is your case)
In a game: realtime events which are triggered by a server
So in your case, if guess you want to notify the user about new messages if he does not have the app opened. This notification comes from a server and is a Push Notification.
As you describe you want to send notification about sign in users about their messages. so it would be the real time notification about when there is message for user you need to notify the user. so apple having PushNotification is the best approach you need to apply for this. using that you can directly notify user about the new messages.
Why LocalNotification is not useful in this scenario?
I think messaging is the realtime stuff. local notification is not for that. its for only managing local notify stuff. like reminder OR to do added task.. and many more
I'm working in a friend request module for mobile app base on Firebase so I'm considering to choose the way that notification is pushed.
Assume that userA request to be friend with userB. There're 2 ideas now:
- userA send request to a simple server then it will call FCM to send notification to userB.
- Make a service that listen to data changed in Firebase realtime database then userA will make change on that db and notification will be shown on userB device.
I think both are possible to implement but what is better, and why?
Please give me some advice about this..
Thanks in advance.
Using either one should be fine.
However, a point to consider here is when keeping a listener active for the Real-time database, it also keeps an open socket on the user's device which adds to battery consumption.
While for FCM, it will only trigger once there is a notification is needed to be sent. If a friend request isn't really that app critical, I think using FCM is a way to go.
Have you also considered using both? If the user is currently online, it would be good to use the Real-time DB, but the childAdded won't be triggered if the user is offline (not using the app for instance). In that case, you can set it so that a notification will be sent to the user.
The important thing in your scenario is that the friend request should be saved first in your database or app server, so that it will trigger the corresponding action (FCM notification or Real-time DB update).