When to resync not-notification-worthy data after receiving GCM tickle? - android

I was wondering, when do you requery your server after receiveing GCM message, that is not "notification-worthy"? Should I even use it for this?
Lets take Facebook-like application and the event of somebody posting a status update.
My naive implementation of this would be as follows.
The server method that inserts the status into database, and then generates a gcm message to everyone who is friends with the person who made the post.
Now, in the GCMIntentService.onMessage() you receive the tickle. What do you do now?
1) Requery the server for new data right there? This means it would download stuff even tho the app is not running. (battery?)
2) Save some flag (indicating there is new data to be synced) in the preferences and next time the app is started,read that flag in onCreate() and requery server if neccessary?
3) Save the flag, do a broadcast to the activity. The activity, if running, receives the broadcast and shows some toast (crouton) that theres new data and allows user to click refresh (like Facebook does). If its not running, just save the flag and load new stuff on apps next start, if flag is raised.
4) I shouldnt use GCM for not notification-worthy stuff
Ideas?

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.

Send data to the server when remove app from recent apps programmatically

I have an application that works with the database.
When I exit the application when I click the "exit" button, it sends the data to the server.
There is a bug: when I remove an app from the recent apps, data is not sent to the server.
How can I fix this?
I want the data to be sent to the server when the application is removed from the recent apps.
You could spawn a Service during onDestroy, which lets you know when the activity is finished, and send the data from there.
https://developer.android.com/guide/components/activities/activity-lifecycle.html

Android - Sending a notification upon recieving a new private message with Firebase

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.

Ensuring triggered sync finishes before showing notification

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.

GCM Chat app - How to know when one user disconnected?

The app
So I'm developing a chat app using GCM. The app works as follow: In a list of users, I can choose with which one I want to talk. Then a request is sent to this user and he has to accept it in order to start the chat. It's like the first user opens a chat room and wait for the other o join in. Im doing all this communication using special flags through GCM messages. Note that a user only exchanges messages inside a "chat room". There's no notification for him if he is outside a chat room.
The problem
When a user leaves the chat room I make him send (through onDestroy()) a message through GCM to tell the other user that he is disconecting and therefore the other user won't be able to send him messages anymore. But what if this first user leaves the chat room without calling onDestroy()? (Like closing the app, the app crashes, internet goes down, etc...)
Solution so far
When the user sends or receives a message I update his last_seen attribute on my database so I can know more or less if he is still online. So I have a cron job on my server checking from x to x seconds if the users of an active conversation are online and closing it if one of them are not. Note that the proccess of updating the user last_seen attribute is really heavy since I have to make an HttpRequest everytime I receive a GCM message (when sending I already have to make an HTTPRequest, so its not a big problem) and that's why I don't like this solution...
Question
Any ideas on how to know if the user is not there anymore?
Thanks in advance, any ideas are welcome
You could perfectly use the onStop() method of your chat room activity:
Called when the activity is no longer visible to the user, because another activity has been resumed and is covering this one. This may happen either because a new activity is being started, an existing one is being brought in front of this one, or this one is being destroyed.
Followed by either onRestart() if this activity is coming back to interact with the user, or onDestroy() if this activity is going away.
Set the status as online as long as that event doesn't trigger, if it does, send the last_seen parameter and assume he is no longer in the chat room.

Categories

Resources