Update views on a SQLiteChange - java

I'm writing a simple XMPP Client in android, I've extracted all the XMPP Calls to a nice helper module. That module writes any incoming XMPP Messages to a SQLite database.
When the app opens a view of a user, it grabs a copy of the latest messages from the SQLite db (easy enough).
However if a XMPP Message was to come in whilst the user is viewing that "ListView" is there anyway bubble a message down a tree to tell a view to update?
The XMPP Client is run in the Application context if that helps at all.

Use ContentProvider and ContentObserver, its a good implementation of the observer pattern, which is exactly what you need. The Adapter providing the content to the ListView even handles practically all of the observation side, you'll just need to implement your own ContentProvider

Use listview.notifyDataSetChanged() and the listview will handle the rest :)

Related

Are multiple Firebase Database Child/Value Listeners efficient for mobile app?

I've been reading about Firebase Realtime Database for a while now I'm just wondering about the whole idea of event-driven data fetching from database. If data is downloaded from the database only when there was a change made to it, then it should be much less demanding on the device resources like battery or Internet connection. I thought I could remap the whole database fragment I need in my Android activity with my model objects that reflect the structure of my database and update its values with Child/Value listeners. Then I would have the whole data I need in a flexible form of POJOs and it would update every time there was a change in the database. So it is actually a real-time solution. But I wonder if such number of listeners would slow down or overload user's device in any terms like battery life or data transfer.
So, would It be a good solution to reflect database state in my Java model objects or should I go another way?
Could anyone resolve my doubts?
Thanks!
The listeners are triggered only when something is changed in the database. So you get data only when needed. It is expected that your app will lose their socket connections when there is no visible activity. That's the way the Firebase SDK works. Also, Android will aggressively pause background applications so they don't consume data and battery when the user isn't actively using it.
When you restart an activity for example, the Firebase SDK attempts to restore the websocket that it uses to communicate with the server. This websocket is fully managed by the SDK.
Hope it helps.
Firebase.addvalueeventlistener() only work when there is any change or update in database you are requesting. And it is a good way to move with JAVA objects or you can also use HashMaps<>.
As far as there is concern for device battery and performance then instead of requesting whole database you try to get only that part whose change event you wanna track.
And if you also want to have grabbing data on background as Whatsapp you can you Broadcast Receiver for internet and startservice() for where Firebase.addvalueeventListener() used.

Firing events on Android and Desktop Java when a database changes

I'm currently trying to work out what technology I can use to fire events remotely when a database changes, a realtime database of sorts. There is both an android application and a desktop JavaFx app that will communicate via the same database. When the data is changed from the desktop side, I'd like Android to update its data, and vice versa when changes occur on Android. Is there any method to achieve this without polling the DB for changes regularly?
I looked into Firebase and it seemed perfect, but lacked a desktop java library. Similarly, I have experimented with Amazon AWS Lambda and DynamoDB, and I can get a Lambda function to fire when the DynamoDB table changes. I can't however find a way for the Lambda function to update the Android/Desktop application that the data has changed though.
The JavaFx desktop application is a requirement of the project.
Apologies if this isn't possible, or if I'm overlooking a well known platform for this issue. Any help would be greatly appreciated.
No you not need the process of polling to achieve your results. To answer the first question, first the most flexible and plausible approach would to built a middle ware to intercept any changes.
Create a script to fire events whenever the data base changes. Wether by a time interval.
Second create server, a real-time server to fire such events to any client.
Third, the java fx client can use native observable for such a task. However i proposed going with a common listener, a perfect choice would be socket.io, there web-socket implementation available for android and vanilla java. For android client, whenever a data is inserted , transformed etc , use a broadcast receiver(local) to fire events in the to notify the server. Or use the socket.io connection to send events. Thats pretty much it.
Firebase + Cloud Functions for Firebase sound perfect for your use case. If the matter is pure Java support for Firebase, rather than Android, you might want to check out this question Get Firebase to work with java, not Android

Dynamically created content on Android - retrieve friends newest posts

I'm trying to create this:
It goes through your friends, finds the ones with new posts and retrieves them. What's the logic that would go behind something like this? What should I be trying to do?
I know two popular way to deliver news to receivers.
First way
Receiver asking for news from friends with a certain frequency. In this case server works as mediator for senders and receivers. Client application asking for news from paticular friend application. For this architecture typically used Mediator pattern:
Second way
Source of news sends this to server. Server works as observer and sends news directly to receivers with online state. Otherwise, server collects news for particular user (client application) in queue. For this behaviour typically used Observer pattern:
For more information about architecture templates see source PDF

How to create immediate Notification System in website

I am implementing a small social networking website, and I am trying to implement notifications.
Notifications have the following requirements
All users will receive notifications whenever users they follow do
certain events (like a post, create a post,leave a comment, etc...
When a set of notifications for a user is unread, just like on facebook, the user will
continue to see a read notification icon on their navbar.
Problem : I am taking an example to describe my problem for better understanding for everyone.
Suppose there are two users A and B. A & B are friends and A post something, suddenly B like the post of A then A should have to receive the notification immediately.
So how can i achieve this immediately receiving of notification process?
Should i send a call to server on each millisecond to check whether there is any unread notification is available for A user ? In this case thousand millions of call creating for multiple user. It is feasible solution ?
(I think it create unnecessary load on my website. As i think facebook and stackoverflow do not use this way).
Provide me a suitable solution just like fb and other webiste using.
Technology Using in my project: Java and MYSQL
You should use JavaScript (client side) along with Java - MySql (Server side), and more specifically Ajax. You will need some time to understand the concept and the usage but it does exactly what you want.
To give more details, what you need is to create a partial view where the notifications will appear, and update this partial view async (with AJAX).
Dont reinvent the wheel.
Use ajax call to server at each interval and check your server and update nav acc.
1.So how can i achieve this immediately receiving of notification process?
2) Should i send a call to server on each millisecond to check whether there is any unread notification is available for A user ?
Your above both questions answers solve through GWTEventService implementation.
Through it you can write code that will avoid client to server round trip.
GWTEventService is an event-based client-server communication framework. It uses GWT-RPC and the Comet / server-push technique. The client side offers a high-level API with opportunities to register listeners to the server like to a GUI component. Events can be added to a context/domain on the server side and the listeners on the client side get informed about the incoming events. The server side is completely independent of the client implementation and is highly configurable. Domains can be defined to decide which events are important for the different contexts.
Refer link: https://code.google.com/p/gwteventservice/

Android - Code design of notification feature

I am working on an Android social app. It contains notification feature such as when A send a friend request to B, B should later get a notification icon shows 1 friend request on App bar (not a toast).
In order to know if the user has a friend request or not, the app will need to periodically send HTTP request to my backend API query for any notification. This should be performed in background. I am not sure what is a good way of implement such a feature. Should I use a Android Service? or should I spin a thread by myself? How to design and implement this to make sure it won't drain the data and battery? If I want to add a toast feature later, how should I design the whole client side notification system to make sure its easy to add the toast functionality?
Code pointers or suggestions will be greatly helpful. Thank you!
Periodically sending web-requests to check whether the user has any notifications or not is very expensive task for battery life.
Rather I suggest to use Push Notifications. There are third-party libraries available for this. Also recently Google Cloud has started Google Cloud Messaging for Android which is powerful framework to sync data between devices for specific app on certain events.
http://developer.android.com/google/gcm/index.html
Please follow the link above for more information to use Google Cloud Messaging for Android.
Also please have a look at following framework. It may of your interest.
https://parse.com/tutorials/android-push-notifications
sending web-requests periodically is even more expensive task (for battery) when the internet is not available in the device.

Categories

Resources