I am developing an app in which users can send private messages to eachother.
I want a notification to be sent to a user when he recieves a new message.
Everywhere I looked it sayed using a cloud service such as FCM is needed.
I am storing each user's recieved messages in the Firebase database, I'm wondering why the following method wouldn't work:
Upon user login, Set a firebase reference on the user's Recieved Messages database node.
start an AsyncTask with an infinite loop:
2.1 set OnDataChange event to listen to any changes in the user's Recieved Messages node
2.2 Upon any changes alerted by OnDataChange, send a notification to the logged user that he recieved a new message.
would this work?
In general what you are suggesting will work. (as #int j said there is no need for an infinite loop or AsyncTask though)
HOWEVER! The user's battery will not last very long with what you are proposing. It is not good practice to have listeners listening to the database all the time. While the app is in the foreground is fine, but when the app is in the background you should un-attach database listeners. This is why you would see many suggesting that you use FCM for notifying a user that a message is available.
Related
I am using Firebase with OneSignal within an hybrid application (Android + JS with cordova app). In some cases the user may become offline and online while he is still using the application.
Note that you can't retrieve a push notification if you don't have connectivity.
So my question is, is it possible to retrieve a push notification if the another user sends a push notification while the first user is offline, and later on this first user retrieves the connectivity (As a delayed push notification)?
Thanks!
Internally, OneSignal uses Firebase Messaging Service, so the constraints should be looked for there.
Firebase has 2 types of pushes: notification messages and data messages. That matters if you want to show a notification straight when a push comes, or you'd like to do some additional processing beforehand.
Then, you can configure Firebase to store and resend every message up to 28 days. Of course, losing a network connection for some time does not prevent a message to arrive.
There is another limitation though: up to 100 messages can be stored per client. So, if there are more than a hundred, it's better to re-request the diff.
And then, when the device finally comes back to the network, you should decide if you'd like the notification to come immediately even if the app is already minimized or the device is sleeping. Here is a part about push priorities.
Finally, to be able to work with Firebase on this lower level, you may need to configure OneSignal accordingly. Here is an instruction telling how to work with the background notifications, if you need them.
I've thinking this for a while that how do android app really notify its users?
Like Messenger, Messenger usually notify a user by making a floating circular profile picture of the sender.
and like other games like Subway Surfers or Zombie Catchers,
in the case of subway Surfers and other apps it notify users, when they are updated or an event is going on,
but in the case of Zombie Catchers, it notifies the users when their slushies are ready to be sold. Or the drones found new zombies at the alloted time.
So my concern is that, how can i notify my users when they have a unread messages or there is an event coming? without using Firebase?
If you are not using Firebase then I guess you must be using SQLite in android. If you are using SQLite then in order to notify its users you must add a TRIGGER on the table so that whenever some value is inserted or updated you can create a Notification Builder or any sort of notification like a TOAST.
E.g:
A user wants to know unread messages, there would be a table consisting of the number of message that are not opened. Whenever the app is opened it checks whether there are any unread messages and if there are/is, you can notify the user.
Link on how to use Trigger in SQLite.
How to use TRIGGER in Android SQLite
Link to notification builder.
https://developer.android.com/reference/android/app/Notification.Builder.html
However if you want the notification to appear when your App is closed, then you must also add a Sticky Service in the manifest file which would check the database after some interval and you should be good.
But I would recommend using Firebase since it has Value event listener and child event listeners making your task much easier.
I'm developing an app in which I wanted to implement some notifications features for the user. Every user notification is saved in a back end sql database. What I've done so far is to create a Android service in which after a certain delay, an HTTP request is made to check if database has new notifications for the user by processing its json reply and notify the user if something is available.
After some research I've done, I have come to conclusion that this in not battery-friendly and I "have" to use Push Notifications using some service like gcm or fcm.
So my problem is now this:
How to properly make a request to the database every for example 1
sec. and check if any notification is available and THEN make a
push notification so that the device is triggered to fetch the
notifications themselves from the database?
I am using Firebase Cloud Messaging to send data messages to my Android app. It is possible that the devices goes offline and that there are multiple data messages queued on the server to be send to a specific device.
When a message is received I have to execute some specific code based on the data inside the message.
Is there some way to be sure that when you receive a message in onMessageReceived() that there are no other messages in the queue to be delivered to my device?
I am asking this because when receiving multiple messages, the data in all messages need to be combined before starting my processing.
Thanks!
I think using FCM alone isn't the best for this use case. In general, FCM's expected behavior is to send the message(s) as soon as feasible.
One thing to remind also is that FCM is a push notification service. So a use-case where you need data to be synced is not a good match.
The way I can think of that you can use FCM here is to simply send a push notification, informing the user that there needs data to be synced. Upon tapping the notification, you can use a DownloadManager (not really sure what data you need synced, but for downloads, it usually this) to download the data you need, then process the data.
I am using vk sdk for my android app. The app can send and receive messages to another users. In order to get the list of messages I used /method/messages.getHistory but how to listen new messages event when another user send me a message? For example in firebase - since it is realtime db we can do it. But how get to know when new message was sent using vk sdk like in realtime? I have an idea to start Service which will check new messages every 5 sec, but I think it is bad solution since it will drain battery etc.