ANDROID API

The ANDROID client API allows mobile applications to send and receive real-time messages via the PushFYI server. The ANDROID API uses WebSocket interface to connect to PushFYI and exchange real-time messages.

The ANDROID API has 3 main functions in addition to 2 helper functions. In this documentation we will examine the API usage and some of the best practices.

init()

The init method allows a mobile client to connect to PushFYI WebSocket gateway to send and receive real-time messages. It is important for a client application to first connect to PushFYI prior to sending or receiving any real-time messages. Hence, this must be the first method that your client application will call in order to use PushFYI real-time transport.

This PushFYI Android init method is invoked after creating pushfyi object constructor ,Which takes listener as an argument to get pushfyi connection states.

Parameter List:

SN parameters note
1 appsecret This parameter identifies your App when it connects to PushFYI. Since PushFYI can serve real-time messages to many apps simultaneously. This field allows PushFYI to distinguish between Apps, so that real-time messages from one App do not mix up with another App. The value of appsecret parameter is in UUID format. e.g. 123e4567-e89b-12d3-a456-426655440000
2 address URL of your pushfyi instance. e.g. 10.0.0.101
3 secure Boolean variable for secure/plain text connection.

Code Sample:


		/**
         * create Pushfyi instance
         */
        Pushfyi pushfyi= new Pushfyi("<your appsecret key>",
          "<your pushfyi ip address>","<boolean variable for secure/plain text connection>"); /** * initialise Pushfyi */ pushfyi.init(new ConnectionListener() { @Override public void onConnected() { Log.i("tag", "connected"); } @Override public void onConnectionFail(final PushfyiException pushfyiError) { mConnectionState.setText("connection failed"); } } );

subscribe()

The subscribe method allows a mobile client to register their interest in a given data channel and returns a Channel.Channel is bound with listener to get subscribed data. PushFYI data channels are logical names to which real-time messages are sent and received. Once the connection is established using the init method, your application can make a call to subscribe method to show interest in a specified data channel.

Parameter List:

SN parameters note
1 channel The logical name of the data channel associated with an event.
2 callback A callback function to call when the API receives an event with the given data channel.

Code Sample:


                                                      
		/**
         * create Pushfyi instance
         */
        Pushfyi pushfyi= new Pushfyi("<your appsecret key>",
          "<your pushfyi ip address>","<boolean variable for secure/plain text connection>"); /** * initialise Pushfyi */ pushfyi.init(new ConnectionListener() { @Override public void onConnected() { Log.i("tag", "connected"); } @Override public void onConnectionFail(final PushfyiException pushfyiError) { mConnectionState.setText("connection failed"); } } ); Channel channel=pushfyi.subscribe("my_data_channel"); channel.bind(new SubsciptionEventListener() { @Override public void onSubscriptionEvent(String channelName, String data) { Log.i("tag","subscribed data "+data+" from channel "+channelName); } });

publish()

The publish method allows a mobile client to publish a real-time message on a given data channel. PushFYI data channels are logical names to which real-time messages are sent and received. Once the connection is established using the init method, your application can make a call to publish method to send a real-time message on the specified data channel.

Parameter List:

SN parameters note
1 channel The logical name of the data channel associated with an event.
2 params Data to send in the real-time message. Data is in string format.

Code Sample:


		/**
         * create Pushfyi instance
         */
        Pushfyi pushfyi= new Pushfyi("<your appsecret key>",
          "<your pushfyi ip address>","<boolean variable for secure/plain text connection>"); /** * initialise Pushfyi */ pushfyi.init(new ConnectionListener() { @Override public void onConnected() { Log.i("tag", "connected"); } @Override public void onConnectionFail(final PushfyiException pushfyiError) { mConnectionState.setText("connection failed"); } } ); Channel channel=pushfyi.subscribe("my_data_channel"); channel.bind(new SubsciptionEventListener() { @Override public void onSubscriptionEvent(String channelName, String data) { Log.i("tag","subscribed data "+data+" from channel "+channelName); } }); JSONObject jsonObject = new JSONObject(); try { jsonObject.put("name", "Henry"); jsonObject.put("message","How are you doing?"); //publish data on given channel mPushfyi.publish(channelName, jsonObject.toString()); } catch (JSONException e) { e.printStackTrace(); }