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

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.

Related

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.

Race issue with reading CallLog from provider

Just a bit of app background, this app allows users to click on a number from the application and redirects it to the phone app.
And once the call is done, it will redirect the user back to the app then it retrieves the call information of this particular number (such as timestamp, duration, etc)
However, I come across a race issue where I don't get the latest call log on some phones; meaning Android writes the call log after the app reads the CallLog provider (I can't be certain for how big this window gap).
To put this in simple technical flow:
User click the phone number on the app
Broadcast receiver is triggered
App start Phone activity
Call ends after some period
Broadcast receiver listen to the event
Get call details
With this in place, is there some steps that I missed? or do you have a better solution to handle this?
Thanks.

How to tell app was started by tapping push message notification

I have a Worklight app that receives push notifications from the server. A notification means there are new messages for the current user from other users. A user would read them by visiting a messages page within the app and then proceeding to a particular conversation page.
I would like to differentiate in the code between user intentions. The app would:
If the user started the app normally (not by tapping a new notification), present the user with the regular app home screen.
If the user started/resumed the app by tapping a notification, present the user with the messages page.
If received while the app is on foreground, only update the on-screen message count (regardless of which page is active).
The question is: is there a reliable means to differentiate between the above conditions?
In another thread, I saw a suggestion to remember the timestamp of a resume event and an onReadyToSubscribe event and assume we were asleep/inactive if notification arrives e.g. just a second after this. This would enable me to differentiate between 2 and 3.
However, 1 is not covered by this. I.e. if there have been new messages, but the user started the app normally, the app would have no means of knowing this and would think it was started by tapping a notification. This way, the user intended to see app home screen, but we transfer him to the messages page.
Is there a reliable way around this for both iOS and Android?
Worklight vesion 6.2.0.00-20140922-2259.
In a pure Native application, you can know "where from the user opened the app", meaning whether it was by tapping on a received notification or by tapping the application icon.
In a Worklight-based Hybrid application, and if using only the JavaScript API, this is not feasible because all paths arrive to the same destination which is the pushNotificationReceived function. So whether you tapped a notification or the app icon or brought the app to the foreground, the aforementioned function will be invoked.
The solution may be to base your implementation on the following Knowledge Center documentation topic: Using native and JavaScript push APIs in the same app.
This way, using a combination of native code and JS code, you could handle the scenario where a notification was received but the user decided to tap the application icon rather than the notification.
An end-to-end example would to somewhat involved to implement, but if you'll follow the code examples in the documentation topic, you should get there...

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

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?

Mobile Chat functionality using Push Notifications, when should something be pushed?

(The following question is for mobile platforms in general (iPhone, Android, Blackberry))
We are integrating chat functionality into our existing application. I designed the system with a .Net web service and Sql Database to keep track of chat messages. Everything's working fine so far, when the chat window is open.
I need a way to notify the user of a new message when he/she is not currently viewing the chat screen (meaning either the app is not in the foreground or they are in another section of the app (not the chat screen).
Obviously, push notifications would be perfect here, but I'm not sure when I should be sending pushes.
How would the client sending the message know whether or not the other guy is viewing the chat screen? Should I just be sending pushed with every single message and have the receiving device decide whether or not it needs to pop up a window or display something in a nofication bar?
It seems like overkill to be pushing every single time a message is sent. How is this normally done?
Any ideas at all would be greatly appreciated.
Thanks.
First and foremost, keep in mind that your server will act as the broker for all messages passed between different chat clients. Here's how this could work:
User A initiates a message for user B
Message gets sent to the server.
Server determines that this message is for user B.
Server initiates a push notification and delivers the message to user B.
Now why would you need to push every single chat message? Because the only other way for your users to get notified would be to poll your server for new messages. Constant polling (at whatever rate you determine) is extremely bad in the mobile realm due to limited resources (battery, networking, etc.)
In a push notification scenario, it is up to your application to handle the logic of whether the user is notified on receiving a new message. What this means is that when user B receives a new message from user A, it's up to you to decide if you wish to notify B (i.e. bring your app to the foreground) or not. In either scenario, you want to use push notifications instead of polling.
On a similar note, user B does not necessarily have to know that user A's application (your app) is in the background which is why you will need to handle that logic (within your app) appropriately.
Push notification normally is applied to the application if it is not in the foreground. It is a way to wake-up the application to handle a new transaction from the server. Basically, a push notification is to be done if the client is running in the background and a new message is ready to be fetched from the server. As long as the new message is not fetched, if other messages arrive on the server, a push notification should be sent to the application.
Push notification is not necessary if the application is still running even if the user is viewing other screens of the application. It is however important that a thread is handling the connection to the server which is "patiently" waiting for transactions from the server.
One question, what protocol are you using for the messaging?.. Is it OMA IMPS protocol?

Categories

Resources