Android background service: CPU and network usage doubts - android

I am developing a custom chat application.
Sent messages are stored on my server using a simple POST.
A service that runs in background is responsible to check using another POST every 5 seconds if there are new messages for the current user and send a notification to the him/her.
I am a bit worried about network and CPU usages because i am aware that internet connection is used all the day.
I used that approach because i need to manipulate some data on my server code before delivery a notification to receiver.
An example is that i have some users assigned as admin. The users username is their email. When a message is sent from one of the admins i overwrite their username (so their email) with the application name before delivery the notification to the receiver.
Is this the best approach to create a custom chat application?

Dont know exactly why you used this use case, but really simple solution provides Firebase.
You can read something about that here. Basically you have to:
Create some layouts
Add listener on "send message" click, that will add message to firebase realtime database
Create FirebaseListAdapter, who will display messages to user
From database docs
all of your clients share one Realtime Database instance and
automatically receive updates with the newest data.
From my understading you dont have to check every 5 seconds if there is some message waiting. Firebase handles it for you.

Related

I want to be notified when data is added in Firebase Realtime Database

I have an application with two different package names. There are separate databases for these two applications. If a user adds new data, I want to receive a message. (For example: Added new data to app A or new data added to app B) I don't want to constantly check. How can I do that?
I can receive notifications as Mail, or I can make a different application and an application that controls the content of these two applications. But I don't know how to make notifications.
To display notification to a user about a change in the data while they are not actively using the app, you'll typically use Firebase Cloud Message or some other out-of-band mechanism to deliver the message.
To detect the relevant data change and send that message, you'll want to run code in a trusted environment that is guaranteed to continue to run all the time. An example of such an environment is Cloud Functions, which also has an example of notifying the user when something interesting happens.
You can use cloud functions to trigger events when there are document changes.
Docs are here : https://firebase.google.com/docs/functions/firestore-events.
Then you can tie those triggers with FCM to get notified when doc changes.

How to send notification to user when a record is added in the DB

I'm developing an Android application , i have to develop an activity that contains a simple communications board .
The information that i want show are contained in a database , When the user started the activity the informations are downloaded , parsed and Showed to users.
Now i would implement a function That notify to the user When a new information is inserted in the db.
Now what that i don't understand is:
I need to implement a service that runs in background and polls the database and notify to the user when a information is inserted or can i use only the GMC (now FCM) ?
If i don't want use GCM there are another solution to do this ?
I have read some thread in stack overflow and there are different solution but i don't understand what is the best solution for my problem.
Speaking at a high-level, here's what I would do.
If I didn't want to use GCM then I would write my own service to make an http request to my backend web server to check if any new information is available. If any new info is received save it to my local Android database and refresh the app. I'd use Android's AlarmManager to start this service every so often (hourly, daily, whatever frequency I want).

Push notification for XMPP Chat App for offline users in Group

Everyone....!!
Greetings..!!
I am working on XMPP based Chat app . I have used XMPPFramework by Robbie Hanson https://github.com/robbiehanson/XMPPFramework
i have used OpenFire as XMPP Server
I am facing problem to get Push Notification for Group Chat when user is offline.
Currently I am able to get Push Notification for One to One Chat when user is offline . (For this I have created a PHP WebService which fetches the information from ofOffline Table every 60 sec)
But As there are no Offline messages Tables for Group Chat.
Can anyone suggest me How can I get push notification to group when user is offline in group
I need to manage this from Both Android n iOS Chat App
for Android i am using
https://github.com/siacs/Conversations
Look into ejabberd_mod_offline_post
First config the Room must be a Member-Only room, and add all users as members right after you created it, so that be able to get a total.
Add above model into ejabberd models.
Implement a Callback Service to handle the callback post.
The idea is when User go offline
In one-to-one case, offline_message_hook will be raised
In MUC case, muc_filter_message will be raised, and any one not Presence-Available is offline.
The best example would be AMP implementation in the Tigase. It is based on a MessageAmp plugin and AMP component.
The MessageAmp plugin intercepts messages. If it detects that the user is not logged in, it forwards the message to AMP component to store it in an offline storage. In your case, you could have your own Message plugin which, if it detects that the user is offline, could forward the message to your Push component (iOS push or Android push, SMS push or something else).
And all the logic responsible for actual pushing notification to the device should be implemented in that component.
Try checking this MUC - Light and use the mod_zeropush module to modify a bit to handle single and group push from the muc-light.
Its more like whats app , although you may not achieve 100% results as you expect but less complex than the tradition XEP-0045.

What is the best way to change website contents without the user giving any trigger

For example, whenever someone "likes" your post in Facebook, you get a notification. You don't have to refresh your page to get the notification. Or whenever someone sends you a message in Whatsapp you immediately get it.
How do they implement that? Does it use Ajax to listen for database changes every second or so?
I want to build an Android application which should do something whenever the value of a remote database changes. Should I use a service to check for the database value every second and compare it to the previous value? Is this the best way to do it?
You could either setup a gcm service or use the native android service to hit your database at a fixed interval of time . GCM would be more efficient and would be recommended , if your app is an extensive real time sort of an app like a messenger or something
You should look into Google Cloud Messaging. But you will need a server to implement it.
https://developers.google.com/cloud-messaging/

how to segregate Chat history messages in android without using local storage? I am using service to get history

How to load chat history message in android without using local storage? My doubt is, I am getting chat history messages from a service and I am loading it to a list. But if any other person chat at that time I am getting that message in that list itself, how to segregate that message from other person chat? Like how they have done in Google talk.
It is hard to give you an appropriate answer without further details:
1) How is the service passing data to your UI? (Cursor, Object Oriented Domain Model)
2) From where are those Chat Messages coming? If it is a online service, is offline caching really no option?
Code snippets are also helping to clarify the situation.
Its some complex, see
when you opening chat screen, just save userId(id of that friend with which you are chating) in prefrences, so when you receive new message in onMessage() method of GCMIntentService class then compare userId(coming with message) with userId(you saved in preferences), if id matched you do not need to call generate notification method, just simply show message in your chat list, if id does not matched, then call to generate notification method(as currently happening), and does not put message in chat list.

Categories

Resources