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.
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'm working with Firebase Cloud Messaging. I have a couple of question I was not able to understand from the documentation:
Android: Lets suppose the app is closed (not backgrounded: closed). If I send a notification with also the data payload, this data payload is passed to the activity through the Intent Extra
Messages with both notification and data payload, both background and
foreground. In this case, the notification is delivered to the
device’s system tray, and the data payload is delivered in the extras
of the intent of your launcher Activity.
What if the user does not tap on the notification? Is the data payload lost? Is there a way to retrieve it?
iOS/Android. Lets suppose the user disabled the notification and I sent a notification to the client: is there a way to retrieve (pull) the notification at the application start?
Thanks very much
If the app is closed (not in background) the onMessageReceived method is called when the notification is received and there you can retrieve the data payload with remoteMessage.getData().get("key_for_parameter"); where key_for_parameter is the name of the parameter that you send in the notification. This method is called even if the application is closed. However, take into account that the "onMessageReceived" is called only if you omit the "notification" param in the notification (look this post)
So once you have the params in the onMessageReceived you can look for an strategy to use them in your application like storing in the DB and you are not going to lose the data if the user does not click on the notification.
if your app in background, Firebase will not trigger onMessageReceived(). Why.....? I have no idea. In this situation, I do not see any point in implementing FirebaseMessagingService.
According to docs, if you want to process background message arrival, you have to send 'click_action' with your message. But it is not possible if you send message from Firebase console, only via Firebase API. It means you will have to build your own "console" in order to enable marketing people to use it. So, this makes Firebase console also quite useless!
There is really good, promising, idea behind this new tool, but executed badly.
I suppose we will have to wait for new versions and improvements/fixes
While reading some article about push notifications, I got to know that push notifications will not be delivered 100% to the device.
In this case how can handle my application functionality in a better way ,
why I am asking is because in the push notification. I will be sending the last updated record id based on that I will be updating data /information in my device.
If I am not receiving any push notification then I have to manage this situation based on some time slot I have to keep on checking the server to fetch the record if i am doing in this way then it will affect for battery because keep on checking method whether some data as update or not
Which we will be the good way to handle this situation, I need a logic for better way of doing.
First of all - On contrary to documentation, Push Notification is working consistently for us.
Secondly, the way I would design this by keeping track of receipt of push notification. For instance:
Server Sent the Push payload to APNS and note down the time.
Client received the Push Notification and make a server call & inform the receipt of notification and pulls the record details.
If server did not receive a notification of push receipt within X period then trigger another push notification. OR
For any further service call, Server can also inform client of missed notification confirmation. Based on this client can trigger the server pull for that record. So, its not a timer based pull but a more informative decision based on data.
PS: From your description I believe you are sending push payload not exceeding 256 bytes.
I am using a custom php webserver for an im program to send push messages, for now i can send push notifications to mobile phone but i am not sure about how to use it properly.
By this i mean should i send all messages as push notifications, is it possible to do that? Is there any limitations about push notification's size? Or should i start a timer to check new messages after first notification and finish the timer when the activity is closed?
So what would be the most proper way to get instant messages from the server?
For Android the limitation on the push notification size is 4k. If your messages are smaller than that, you can use push notifications for sending all the content.
However, delivery of push notifications is not guaranteed, so it's a better practice to use if as a means for the server to notify the app that new data is available, especially when it's not running in the foreground. The app then calls the server to retrieve the data.
When the app is in the foreground, you can poll the server periodically for new data.
I'm using C2DM to send notification for my android App and it is working fine. But i am able to get only one notification at a time. so that while receiving the notification, it replaces previously received notification.
So how to receive multiple C2DM notification, like receiving normal SMS?
Give the messages different collapse_keys. Here's an excerpt from the official docs:
An arbitrary string that is used to collapse a group of like messages
when the device is offline, so that only the last message gets sent to
the client. This is intended to avoid sending too many messages to the
phone when it comes back online. Note that since there is no guarantee
of the order in which messages get sent, the "last" message may not
actually be the last message sent by the application server. Required.
On a side note; you are not supposed to use the push messages too frequently:
Are you sending C2DM messages too frequently? If you need to
communicate with your application frequently over a short period of
time, C2DM is probably not the best solution. Instead, consider
implemeting XMPP or your own protocol to exchange messages, and use
C2DM only to send the initial notification.