3 Replies - 987 Views - Last Post: 14 August 2016 - 03:40 PM

#1 Student000   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 21-July 16

Is it possible to send data from server to android client?

Posted 11 August 2016 - 08:46 PM

I was wondering if it was possible to send data to the android client from the server such as EasyPHP. The android client in this case is not an actual device but an emulator. I have a scenario in which I want to send json data to specific client emulators but I'm not sure if there is an easy solution to this. Does anyone have ideas?
Is This A Good Question/Topic? 0
  • +

Replies To: Is it possible to send data from server to android client?

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15488
  • View blog
  • Posts: 62,032
  • Joined: 12-June 08

Re: Is it possible to send data from server to android client?

Posted 12 August 2016 - 06:54 AM

Yeah.. the ability for a device to hit a site to send data to and from is quite common.
Was This Post Helpful? 0
  • +
  • -

#3 Student000   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 21-July 16

Re: Is it possible to send data from server to android client?

Posted 14 August 2016 - 02:08 AM

I've figured out how to send from server to client through FCM. My only problem I'm facing as of now is I'm getting an error that reads:

    java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.google.firebase.messaging.RemoteMessage$Notification.getBody()' on a null object reference



According to this post, the error is usually caused by not including a notification object on the
$message
variable, but I'm not sure what that means (what does that look like in php?). Here is the PHP script

$api_key = "api key here";

$registrationID = array("registartion token here");

$message = array("From: " => "Server", "notification" => array("body: " => "Sent from server"));


$url = 'https://fcm.googleapis.com/fcm/send';


$fields = array(
				
				'registration_ids' => $registrationID,
                'data' => $message
				
				);
				
$header = array(
		'Authorization: key=' . $api_key,
		'Content-Type: application/json'
		);				

// Open Connection
$ch = curl_init();

// Set the URL, Number of Post vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
echo json_encode($fields);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));		

$result = curl_exec($ch);

curl_close($ch);

echo $result;




This gets printed on the php page signaling its a success,
{"multicast_id":6728039038208547531,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1471165085623147%9ddb9a9ff9fd7ecd"}]}



but the android client keeps crashing, what could be the problem?
Was This Post Helpful? 0
  • +
  • -

#4 Student000   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 21-July 16

Re: Is it possible to send data from server to android client?

Posted 14 August 2016 - 03:40 PM

The android client looks like this:

public class FirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService {

    private final String TAG = "MSG";

    public void onMessageReceived(RemoteMessage remoteMessage)
    {

        Log.d(TAG, "From: " + remoteMessage.getFrom());
        Log.d(TAG, "Message Body: " + remoteMessage.getNotification().getBody());

    }


}





public class FirebaseInstanceIDService extends FirebaseInstanceIdService {

    private final String TAG = "MSG";

    public void onTokenRefresh()
    {
        String token = FirebaseInstanceId.getInstance().getToken();
        Log.d(TAG, "Registered Token: " + token);
    }

}




   protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        FirebaseMessaging.getInstance().subscribeToTopic("test");
        FirebaseInstanceId.getInstance().getToken();

    }



Was This Post Helpful? 0
  • +
  • -

Page 1 of 1