Sending alerts / notifications from server side to android application without internet access - android

I need to send a push notificiation from a webapp to an android app.
Basically I just want to inform the android client that new data is available on the server. So it only has to be a string + vibration/sound of the phone.
The (big) problem here is that I am inside a corporate network without access to the internet. This is why I cannot use GCM.
So far I found the following options to accomplish the task without GCM:
Use XMPP
WebSockets
Ajax Polling
Is it possible to include WebSockets or AjaxPolling into a native Android app to trigger events like vibrate?
Is there an easier solution, without that much overhead as with xmpp, since I just need to send a short notification? So far I understand that I need somethink like SMACK XMPP for Android + e.g. Openfire and XMPPHP on the server side for this scenario.

Since nobody replied, I want to provide you with an approach I now try to pursue.
I use Laravel to write the API. It comes with an out-of-the-box support for Redis, which is awesome to Broadcasts Events -> https://laravel.com/docs/5.1/events
The following is just a quick example:
Redis::publish('test', json_encode($data));
Those events are received from a Socket.IO Server-Instance running on the same machine.
var Redis = require('ioredis');
var redis = new Redis();
redis.subscribe('test');
....
server.listen(3000);
Socket.IO has an android client implementation, which allows me to connect to the socket.io server in a native android app.
Here is an example: http://socket.io/blog/native-socket-io-and-android/
private Socket mSocket;
{
try {
mSocket = IO.socket("http://localhost:3000");
} catch (URISyntaxException e) {}
}

Related

Android - XMPP over websockets

I am trying to implement a chat functionality in my android app using XMPP over WebSockets.
I was able to find a library (org.igniterealtime.smack), but the connection is established over TCP and not over WebSockets, which I don't want.
Does anybody had to implement the same functionality and can point me in the right direction?
Websocket support for Smack is tracked as SMACK-835. It is currently in Smack's main branch and scheduled to become the Smack 4.5 release.
You can use it right now by consuming either Smack pre-releases or the nightly artifacts from https://www.igniterealtime.org/archiva/repository/maven/
Not sure if it's OK and doesn't violate TOS and whatnot but first sentence implies general guidance for XMPP + WebSocket and not strictly asking for Smack answer so - you could use JaXMPP library (https://github.com/tigase/jaxmpp), which already supports WebSocket and and is compatible with Android (StorkIM, https://github.com/tigase/stork is based on it).
It's quite easy to establish WebSocket connection (even encrypted):
JID user = JID.jidInstance("user#example.com/resource");
String password = "password";
Jaxmpp contact = new Jaxmpp();
contact.getModulesManager().register(new MessageModule());
contact.getConnectionConfiguration().setConnectionType(ConnectionConfiguration.ConnectionType.websocket);
contact.getConnectionConfiguration().setBoshService("wss://xmpp.example.com:5291");
contact.getConnectionConfiguration().setUserJID(user.getBareJid());
contact.getConnectionConfiguration().setUserPassword(password);
try {
contact.login(true);
if (contact.isConnected()) {
contact.disconnect();
}
} catch (Exception e) {
e.printStackTrace();
}
(disclaimer: I'm from Tigase team, which created both JaXMPP and StorkIM)

Get data from Mysql database in real time Android Studio

I'm developing an app which users can send a notification to another one.
Moreover, I'm using php scripts to retrieve and sent data to my Mysql database.
Using Volley, a user can inserts a new row into table 'Notifications', but I don't know how to listen that insert from the app.
I mean, now the user who sent the notification in the database should call the method getNotificationsFromDB() to get notifications and see them on a RecyclerView (like Facebook Notifications). There's no other way. And only can know about Notifications if he is running the app.
For example, a Notification could be like this: 'John has sent you a notification'.
Also, this thread runs on the Notifications Fragment...
final Thread thread = new Thread(new Runnable() {
#Override
public void run() {
getNotificacionsFromDB();
try {
Thread.sleep(100000);
run();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
thread.start();
But it should consume a lot of resources from device. A friend told me it could be with Triggers running on 'the runtime' of android. But actually I didn't understand him.
How can I listen updates from my MySql DB to create a Push Notification?
Is there a way using Services like IntentService? or how SocialApps like Facebook or twitter manage the database updates?
Is GCM a good option?
Just like Bruno said it is required to have a server in order for your app to receive GCM messages and vice versa. The docs have an example using Java Smack library. But you don't necessarily have to use Java. Since the upstream message you send to the server from the Android devices MUST be xmpp protocol if you decide to use php then you might wanna look into xampp server which supports xmpp protocol. Good luck.
Your architecture is quite unorthodox. You can't have GCM messages without a server pushing those GCM messages. Are you sure you don't want to build a server in between your device and database? Your setup looks very vulnerable to attacks

Pusher VS Client request to server every second for changes

I am trying to implement a real-time pushing from my server to a customized android device. (A device which runs on android OS). I can't use GCN because there is no unique token.
Here is the deal.
Problem
I am posting a message from a web portal to the server
I would like the message to be pushed to the android device
Here are the proposed solution, which is the best?
Solution 1
Insert the message with a 'create_date' timestamp
Let the android device req the server every second to check for any new messages via the 'create_date'
Solution 2
Use Pusher with pusher-java-client library installed in the android device.
I am thinking of using solution 2, which is the most ideal, I think! Since I am not too sure how Pusher works, is the architecture of pusher the same as Solution 1?
Any kind souls would answer my question?
Thank you in advance.
The architecture is different in two ways:
The Pusher service will create a persistent connection between the mobile devices and the service so when new data is available it can be instantly pushed to the mobile device
Pusher offers you real-time infrastructure so instead of having to handle multiple polling requests you make a single HTTP request to Pusher and it will push the message you send to the mobile client. This is an easier architecture than building it yourself and you see significant benefits if you need to horizontally scale.
The Code
In the Android app:
Pusher pusher = new Pusher(YOUR_APP_KEY);
pusher.connect();
// Subscribe to a channel
Channel channel = pusher.subscribe("my-channel");
// Bind to listen for events called "my-event" sent to "my-channel"
channel.bind("my-event", new SubscriptionEventListener() {
#Override
public void onEvent(String channel, String event, String data) {
System.out.println("Received event with data: " + data);
}
});
On the Web Server (node):
var Pusher = require('pusher');
var pusher = new Pusher({appId: ID, key: KEY, secret: SECRET});
pusher.trigger('my-channel', 'my-event', {some: 'data'});
To ensure that channels can only be subscribed to by the intended recipient of the message you should use authenticated channels.

Android App using RabbitMQ of cloudfoundry

Hi i am trying to develop a chat application in Android using RabbitMQ.
The code is working fine with my locally installed rabbitMQ server.
Inordrer to send message to the queue my android APP needs the address of the robbitMQ server it is talking to.
Can I make use of RabbitMQ on cloudfoundry, replacing my local rabbitMQ in the application code ?
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("<RobbitMQ server>");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.exchangeDeclare(EXCHANGE_NAME, "fanout", true);
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
You'll face a couple of issues with this:
the address of the RabbitMQ service on cloudfoundry.com will be dynamically-assigned... you can look it up from the environment on cloudfoundry.com within a web app, but you can't easily get it in a remote native Android app.
right now, you can't make a remote AMQP connection into a service on cloudfoundry.com - only within the Cloud Foundry vcap environment. So even if you had the address, an AMQP connection via JMS directly from an Android app wouldn't work.
How about running RabbitMQ on an EC2 instance? or what about building a web app backend for your Android app, which talked to RabbitMQ running in a Cloud Foundry instance, with Android talking to the backend via SockJS?

Can I connect with WebSocket in an Android app?

I have an Android app and I want to send a text from the Android application to the webpage using HTML5 WebSocket.
Is this possible and if so how?
I'm aware of 2 libs for Android supporting WebSockets from native apps
http://code.google.com/p/weberknecht
https://github.com/tavendo/AutobahnAndroid
Autobahn supports RFC6455 (the final WS spec), integrates well with UI and service apps and support RPC and PubSub over WebSockets.
Disclaimer: I am the author of Autobahn.
A simple google search for 'android websockets' turned up this. He is referring to a GitHub project called websocket-android-phonegap.
Answer is Yes, it is possible to send a text from app to a web page.
WebSocket works on the very principle of server and client over TCP/IP. Just a wrapper created over the TCP/IP layer and built a new data format which is defined by IETF. Details of the data format is available at -
[https://www.rfc-editor.org/rfc/rfc6455][1]
Server accepts websocket connection if requested in correct format. Client here is the web application in which javascript objects are defined solely for this purpose in HTML5.
Easy to use APIs:
Client side programming of the websockets are very easy with new APIs and objects defined.
APIs(events) available for the developer: onopen, onclose, onmessage. All these functions should be defined by the developers java script file.
onopen: The function is called when the server accepts the connections succesfully
onclose: The function is called when the socket connection is closed
onmessage: The function is called when the data is received from the server.
send : the function is not event based but should be triggered when the client has the information to share with the server.

Categories

Resources