I am trying to build an IM using Smack Library. I did it correctly and its working fine in foreground and I could start a STICKY service which can look for the message in the background. My issue is that I don't want a persistent service in the background, because it will eat up the battery of the android device, instead I want some broadcast to be fired up when the XMPP message comes with some events.
Is there any way I could achieve this? I have tried looking for example with the search term and I found nothing so I did not achieve any sort in this particular context so does not have any relevant code.
I think that all depends on what you want to archive.
If you need fast direct message arrival when the 2 clients are online, I think that the persistent service is the only solution with smack xmpp . With solid code development it should not eat up much battery...
If you don't really care if the message arrives after 2 minutes for example, you should use an intentservice (connect/get messages/disconnect) & a timer (e.g. every 2 mins) as long as the app is running.
What you describe (some broadcast to be fired up when the message comes) is more similar to PUSH mechanisms like Google Cloud Messaging...
Related
for my android app i need to receive some notifications from a server. The notifications are by no mean time critical and it would be enough to check for new notifications when the user opens the app, so no background activity at all.
I noticed, that it is recommended to use a service like FCM (Firebase Cloud Messaging) for any kind of notifications. However, I feel that I would introduce quite a big library for a very simple task with FCM, potentially causing worse battery life than just using simple sockets to ask the server for new notifications.
So I wonder if its better to use a service like FCM or just do simple pulling for notifications in my case.
I feel that I would intodruce quite a big libary for a very simple task with fcm, potentally causing worse battery life than just using simple sockets to ask the server for new notifications.
FCM uses one shared long-lived socket for all apps. There is a fat chance some other app on your phone is already using FCM, so having your app use the socket connection as well would not increase the battery usage at all.
So I wonder if its better to use a service like fcm or just do simple pulling for notifications im my case.
Although your app does not require any background push messaging, I would still advise you to use FCM to inform the app of any events instead of asking the server every time you start the app. The general principle of polling versus eventing apply here. FCM is not difficult to implement.
Well as I know , FCM never force to run a background service to receive Push notification, you may simply adapt it in your project and its simple to use.
Even I saw in may sites that using FCM increases the users by sending them promotions and offers using push message.
You may read more at FCM
I have a website that sends and receives documents. I was thinking of building an Android app that notifies the user if a new document has been received, and displays document details if the notification is clicked. It doesn't have to be in real time, it could update in interval of five minutes or something.
What is the best way to update the Android app of changes in the website? I'm new to Android and I'm not quite sure where to start. I've heard of Services, BroadcastReceivers and Alarms, but I don't know if those are the right ideas.
Update: How do I update my Android db from my web db within an AsyncTask in my BroadcastReceiver? I'm worried I might have a "leak error" which sometimes comes up with my AsyncTask.
You can try to implement GCM or as the above-mentioned, work with an AlarmManager or the more efficient JobScheduler (requires API level 21!). Avoid doing heavy work on a BroadcastReceiver. Instead use the Broadcastreceiver to receive Alarms and start a Service in background. You may also have a look to WakefulBroadcastReceiver which holds a WakeLock for you. The Service could GET data from your webservice by using a REST architecture and update it's local database. Retrofit is a powerful open source library for a REST architecture. If there are new database records, you can inform the user by a Notification. Don't forget to check basic things like not starting the Service if the device hasn't got a network connection or to stop the Service after the work has been finished. I personally recommend to learn the basics first and then go to advanced topics. Good luck and pleasure.
To start of, i should mention that i'm a newbie in Android (Not that much experience in Java at all tbh), so be easy on me.
I am making an app that continuously pulls data from a server, and then returns data through a http post request. The question is, what is the best way to handle the actual pulling from the server? Should i be using AsyncTask or create another thread and let it run on that? Are there better methods for this purpose?
I will be pulling data every 5 minutes. (I am aware that this will drain the battery very fast, and i should definately be using Androids C2DM framework. But i have no experience in it before and i'm on a deadline, so this'll have to do until i have time to learn how to implement it.)
I'm grateful for any advice!
As an alternative to C2DM, you can do the persistent TCP connection between your device and the server. Then every 5 minutes your server can push a tickle to the device. Upon being tickled, the device can request the information via Http post.
Here is some sample code on how to do that.The connection stays open in a background thread even after the app has exited
Creating and Managing a persistent TCP socket: http://openmobster.googlecode.com/svn/trunk/cloud/android/connection/src/main/java/org/openmobster/core/mobileCloud/android/module/connection/NotificationListener.java
Full Disclosure: I am the Chief Engineer of OpenMobster and I wrote this code. Please feel free to use whatever you like or just get an idea if thats what you need
Thanks
Do you need to pull the data in background (even if your app is not "opened" and the android device is sleeping)? I suppose thats what you want because you mentioned C2DM. If so..the buzzwords are AlarmManager (with repeating time)/BroadCastReceiver and maybe NotificationManager to notify the user. With AlarmManager you schedule your events (every 5 minutes) and with BroadcastReceiver you receive those events and do what you want to do every 5 minutes :)
I'm using pubnub as a publish/subscribe channel between an android app and a server.
Currently I'm thinking of how I will implement this.
I'm using the provided library for android (https://github.com/pubnub/pubnub-api/tree/master/android) but I think there will be some problems with the application lifecycle if I use it like it is now. (Correct me if i'm wrong)
I was thinking of implementing it as a service
What I want
The service has to keep on running until an hour (negotiable) after the last app usage. That's because we want to have notifications when a message comes in, but the app is not the currently used app.
How do i stop the service after one hour of non-activity of the app? Probably Android will kill it, but I want some control.
The Service must be able to trigger the app to change it's interface when specific messages come in (I was thinking of sending intents from the service when we receive a pubnub message?), pubnub will send data to the service, so I need a way to pass this data to the application (probably save it in a Bundle in the intent?)
I need to listen to multiple pubnub channels (max 2 at the same time), I think I will have to do this in multiple instances of this service?
I think I will do it like this:
Create a service that's started when the app starts
Let the service listen to a pubnub channel
When a message comes in, send an intent and use the intent filters
implement broadcasthandlers to listen to these internal intents
Is this the right way to do this? any hints?
You have an excellent set of questions an detailed points that I will talk about in this answer. You are using Android and you are interested in the conventions and best practices for PubNub Publish/Subscribe scenarios.
Your use case is very common and the best ways to build apps always vary dependent on application needs. However you definitely have the right idea and have asked all the right questions. You just needed some sample code and a direction to get started on implementing the specifics of your application needs. To define your needs in a list:
Connect/Disconnect Ability.
Always-on Background Service that can Send/Receive data and notify other apps via Android Intents.
Connecting to Multiple PubNub Channels at the Same Time.
So to get started I will provide you direct links to some examples and methods:
Create a Service that is Started when when Android Boots: https://github.com/pubnub/pubnub-api/blob/0dfd8028b803332f5641adc909b1a26f87bd7ff1/android/PubnubAndroid/src/com/aimx/androidpubnub/BootReceiver.java
UnSubscribe/Disconnect Example Code when you want to stop listening on a PubNub Channel: https://github.com/pubnub/pubnub-api/blob/0dfd8028b803332f5641adc909b1a26f87bd7ff1/android/PubnubAndroid/src/com/aimx/androidpubnub/MainActivity.java - Listening to multiple channels is easy by placing the blocking pubnub.Subscribe() method inside a Thread.
Regarding your thoughts - This IS the right way to do it:
Create a service that's started when the app starts
Let the Service listen to a PubNub Channel.
When a message comes in, send an intent and use the intent filters.
Implement BroadcastHandlers to listen to these internal intents.
is there any way to listen a Server action using a service?i want to check server message for life time and do actions based on server message,please post a sample code
I think what you looking for is a long time running background service, check out this SO question.
Here i have example of using service, but it's doing some other thing. You can work it out..
http://maephv.blogspot.com/2011/08/android-notification-in-your.html
But remember, that when screen comes off, services freeze. I don't know yet how to fix it, but maybe "background process" is word to google