Parasitic network usage? - java

My app will be running in the background and uploads data to the server from time to time. Everything works fine, except that I want my app NOT to upload data to the server on its own by turning on network services. Instead, I want it to only start the upload process when another app or service has started a connection. In some words, I wanna be parasitic to the other app.
I know for a fact that this can be done in Android, however I couldn't find anything in the documentation. Any idea?

I would try registering for the broadcast intent WifiManager.NETWORK_STATE_CHANGED_ACTION, via a BroadcastReceiver. See http://developer.android.com/reference/android/net/wifi/WifiManager.html#NETWORK_STATE_CHANGED_ACTION.
Hope that helps!

Related

Send notification when app is not running

First of all I don't know what type of code to put for my problem.
I'm developing an Android mobile game, with two ways to earn ruby, via mini transactions or via the OfferWall IronSource.
Regarding the OfferWall, those are tasks to complete to then receive Ruby in exchange.
Now my problem that I would very much like to solve
When a user completes a task and the app is not running, how can I send a notification?
zigg, my suggestion would be to call firebase notification while running background service or you can use push notification as Blundell suggested in background service.
The background service will run even if the app is not running. I use foreground services and call push notification in the service when the app is closed or in background.
Hope this helps.

Receive data from apache webserver

I have installed apache web server on a Raspberry Pi. I made also an android application that connects to the server and post some requests successfully. The app "speaks" with an index.php file placed in the web server.
I installed a motion sensor on the Raspberry and I want the server to send data to the application if the motion sensor is triggered or not. Is there any way to do this or request the web server from the android app if the variable (e.x. motionstate) is "active"? If I need to provide more informations or parts of the code feel free to ask.
The easiest way would be to have a a code snippet running in your server which periodically(Google for Event Handlers in PHP or Polling/Looping) checks if the value of the variable (ex motionstate) has turned to active and then inform the app using an API call(or the other way around ie Android App fires a call periodically to server running in device to get data).
Refer to this for some idea: http://www.robertprice.co.uk/robblog/using-motion-sensor-raspberry-pi-php/

Sending push Notifications from Java Web Application

I have a Java web application which needs to send manual and automatic notifications to the user.
This notification should go to the user's browser as well as the mobile devices (both iOS & Android).
I found out there is no way to send notifications to mobile devices directly if there is no native application running on the mobile device. So my only option seems to be web push notifications. I went through a few articles and I found it very confusing. I don't know where to start.
Can I send the notifications directly from Java Code? Or do I have to use FCM(Firebse)? If so can I call FCM directly from Java code, such as by calling it using Apache http client libraries?
How does the FCM, client's browser and my application connect?
I also found out that a service worker should run in the background to receive the notifications. How do I integrate it with the Java code?
You can send them directly without using FCM and there are several libraries available, including a Java one.
Unfortunately iOS currently has no support for Web Push. Subscriptions to your service are on a device level rather than by user, so if I sign up on my desktop you cannot send notifications to my mobile unless I sign up again in my mobile browser.
Notifications pushed to Android will be displayed if an instance of the browser is running, in the real world (for me anyway) Chrome always seems to be running in the background somewhere so I get notifications through in pretty much real time. The downside is web push notifications go straight into the notification shade, they do not pop up on screen first.
The rough workflow goes like this:
User visits your page, you load service worker and check for web push
capability, if satisfied you can request permission to send
notifications.
If user grants permission you pass your public key to your service
worker to create a subscription for that user, this returns an
endpoint and two keys which you need to push a notification to them.
Your webpush library runs as a server instance and takes care of all
the encryption and token handling, you configure it however you like
to dispatch messages, usually in response to HTTP POST requests but
it's up to you.
Within your service worker you define an event handler for receipt of
a push message. This is where you create and display the notification
to the user, again how you do this is up to you.
You can pass parameters in the payload of the notification and use them as variables within the notification you display or you can hard code values, you can specify different behaviours depending upon whether the user has your page in focus or not, you can add buttons and set different actions for them, trigger events upon dismissal, customise the vibrate pattern, replace or stack the notifications, access the data in existing notifications etc etc. All this is handled by your service worker, receiving the notification alone does nothing at all.
Your service worker is just a script written in javascript which you link from your page. It is loaded and installed by the browser the first time a user visits and then runs independently when invoked.
Service workers are very powerful. You can also use them to implement complex caching rules, serve content while offline, push data between different browser windows etc. A service worker can spawn more service workers and as they run outside of the main thread of your browser they are ideal for offloading cpu intensive tasks to without delaying the rendering of your page.
Final point to note, your site must be served over SSL to be able to deploy a service worker.
1) It depends a bit on where the Java code that sends a message runs.
If it runs in an Android device, then there is no way to securely send messages from that code. See my answer here for more details: How to send one to one message using Firebase Messaging
If the code runs in an environment you control (such as a server you control, or Cloud Functions), you can call the FCM HTTP end points documented here. The Java Admin SDK doesn't support sending messages yet, although it seems to be under consideration.

Show messages after killing the app

I am currently working on android chatting app using io.socket. When the app opened all the thing is working fine but when I kill the app I am not able show messages.
GCM is a bad idea for messaging because I heard it misses some messages.
service means it will kill the battery.
I want to show messages even if I kill the app(just like watsapp).
Do you have any suggestions, as to, how I can achieve this.
When you kill whatsup app its use Gcm and when you Open your Application its uses XMPP protocol to send and recieve Message. Whatsup app using this both feature in it. so It getting message after killing app too. Whats app app also use MessageService after killing app so It get message regarding.

Create a basic network app

I have a little question about an android app with a function "remote" use via internet.
So I have these ideas:
Create a webservice using php on the server, and refresh the client app all x time like 4-5 sec
Or make a java server (so I don't know how I can do that)
I just want make a remote connected via user/password to the server and the other app connect to the server and refresh his status.
Do you know the best way for do that?
Thanks
The question is not very precise, but as far as I understand it, both your ideas implement the 'pull' concept, that is the client app checking the status of the server every now and then.
If you want the app to be instantly notified of the server status change, I would suggest push notifications with GCM (Google Clound Messaging). You can find some basic descriptions and examples at: http://developer.android.com/google/gcm/index.html
Sending a server-to-client notification is simple regardless of the platform you use server side. E.g. for PHP integration with GCM, check out this thread: GCM with PHP (Google Cloud Messaging)

Categories

Resources