Ensuring triggered sync finishes before showing notification - android

I am developing an app that synchronizes data with a server using a Sync Adapter. When data changes on the server, I send notifications via GCM to android devices of the respective users. I also use these notifications to trigger a sync.
If a user clicks on a notification, I want my app to show details of the corresponding data. For this to work, I need to have the triggered sync completed before the notification is clicked, so that the data is available locally.
How do I ensure this? Can I delay showing a notification until its triggered sync is completed? Or is there another way of solving this problem?
I thought about using either a ContentObserver or SyncStatusObserver for this. The former requires me to specify a content URI, which is not possible if I want to wait until everything is synchronized, while the latter can only be used to determine if a sync is pending or active, but not if one has succeeded.

Related

Start a service in background which does gets destroyed as in Whatsapp

I am building a chat app using firebase . I want to show notification for new messages when the app is in background/foreground/terminated . I tried to show it using background service but it gets killed everytime app is destroyed. I know it can be done using FCM but i don't want to use it . It can also be done using foreground service but i don't want that strict notification .
I explored various blogs and i came to know this happens because it runs in same process and to create another process it get complex from Oreo and higher versions.
I also come to know JobService but it minimum periodic time is 15 minutes and if implemented will lose the real time notification nature.
How it can be implemented as in WhatsApp ?
If you see the setting of WhatsApp you will see that 1 Process and 1 Service message keeps showing indicating that MessageService is still running which means that it can be done using background service
I come to know that WhatsApp is also using FCM kind of thing with high priority to show notification.
It simply syncs the changes in database when data message is recieved using FCM.
This data message can be send using either firebase GUI or trusted environment like admin-sdk ..
I used the later one to send data message using admin-sdk from my server.
I have used APIs with device token of user as data message to send a POST request to my server which sends notification as data message to receiver device to trigger syncing .
Once Syncing is finished , i have send notification accordingly.

Retrieve push notification after coming back to online status. Firebase/Onesignal

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.

BroadcastReceiver active Observable vs single check up every 10min

I'm trying to set up a notification service for my application that shows upcoming shifts for an employee in a workplace - when a new shift is added for an employee by his/her boss I want notification to pop up.
Now the question I have is what is the correct way to implement it. In my BroadcastReceiver should I just make an Observable that listens to the changes of a node in firestore constantly or I should not use any listeners and use jobscheduler and check every e.g 10min if any changes occured?
The appropriate way to implement this would be using PUSH firebase service. Then register for PUSH on application startup, and save your token to your backend that stores the shift information.
You will likely have an API that changes, adds, or removes shifts, that API should then use the appropriate PUSH token for all employees affected by the shift change and send a payload to the appropriate devices to do a refresh or show a notification icon to open, or whatever action you want to occur from this.
Polling is not a good option, that should be a last resort and a darn good reason to do it should be supplied as it is wasteful on battery and resources.

How would android apps notify users even though the app is not opened yet?

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.

How to get notifications of changes in contacts even when the application is closed

My app "syncs" all the contacts on a phone with the server. However, part of the requirement is that if any contact is inserted, updated, or deleted then the sync must happen again to maintain data integrity. I know about registering a ContentObserver on ContactsContract.Contacts.CONTENT_URI, but any such observer would have to be unregistered on the Activity's onPause(), and thus would not get notifications if the app wasn't running at that moment.
I also do not want to implement a content observer in a service, since services aren't permanent either. Plus, I don't like the idea of running something in the background 24x7.
So, is there any way by which I can detect that the contacts have changed since a sync? I do not need a real-time notification, so it's ok if this detection only happens when the app is run next.

Categories

Resources