GCM how to implement the server side for upstream using Jaxl

152 views Asked by At

I successfully sent notification from my app server(localhost) to my android phone. I have been reading and trying different things to implement the server side code so i can implement upstream messages using google cloud messaging. I have looked at this Upstream message to server app then I used the code on my android phone i get "Sent message" but i don't understand how and if my server actually received the msg.

so my question is my server side implementation correct ? ( right now I'm using WAMP running localhost) and where can I find the log output generated by Jaxl?

I have been trying for a 2-3 days now(yes i read the google cloud docs and the Jaxl getting started doc, but I'm still unclear). p.s I don't have enough reputation points to comment on the link I provided above that's why i'm creating a new question.

UPDATE

ok so today all of a sudden Jaxl actually created a log file in the same directory where my localhost files are stored C:\wamp\www\myproject , but it was actually creating the log when i was accessing it from my own PC. the log

jaxl_fsm:61 - 2016-04-29 07:25:33 - calling state handler 'setup' for   
incoming event 'connect'
jaxl_socket_client:95 - 2016-04-29 07:25:33 - trying tcp://gcm-preprod.googleapis.com:5236
jaxl_socket_client:104 - 2016-04-29 07:25:37 - connected to tcp://gcm-preprod.googleapis.com:5236
jaxl_loop:82 - 2016-04-29 07:25:37 - active read fds: 1, write fds: 0
jaxl_fsm:71 - 2016-04-29 07:25:37 - current state 'connected'
jaxl_fsm:61 - 2016-04-29 07:25:37 - calling state handler 'connected' for incoming event 'start_stream'
jaxl_loop:82 - 2016-04-29 07:25:37 - active read fds: 1, write fds: 1
jaxl_fsm:71 - 2016-04-29 07:25:37 - current state 'wait_for_stream_start'
jaxl_socket_client:201 - 2016-04-29 07:25:37 - sent 186/186 of data
jaxl_socket_client:202 - 2016-04-29 07:25:37 - <stream:stream xmlns:stream="http://etherx.jabber.org/streams" version="1.0" to="gcm.googleapis.com" xmlns="jabber:client" xml:lang="en" xmlns:xml="http://www.w3.org/XML/1998/namespace">
jaxl_loop:104 - 2016-04-29 07:25:37 - active read fds: 1, write fds: 0
jaxl_socket_client:188 - 2016-04-29 07:25:37 - read 7/7 of data
jaxl_socket_client:189 - 2016-04-29 07:25:37 -  F
jaxl_socket_client:175 - 2016-04-29 07:25:37 - socket eof, disconnecting
jaxl_loop:104 - 2016-04-29 07:25:37 - active read fds: 0, write fds: 0
jaxl_loop:104 - 2016-04-29 07:25:37 - active read fds: 0, write fds: 0
jaxl_loop:115 - 2016-04-29 07:25:37 - no more active fd's to select

here's my jaxl php code:

<?php
include_once 'jaxl.php';//to use JAXL librabry 

$client = new JAXL(array(
    'jid' => '/Projectid/@gcm.googleapis.com',
    'pass' => '', //API key
    'host' => 'gcm-preprod.googleapis.com',
    'port' => 5236,
   'strict' => false,
    'force_tls' => true,
    'log_level' => JAXL_DEBUG,
    'auth_type' => 'PLAIN',
    'protocol' => 'tls',
     'ssl' => TRUE,
    'log_path'=> 'myUpstreamlog.txt'  /*This create text file to comminication between gcm and your server*/
));

$client->add_cb('on__message_stanza', function($msg) {
 echo 'now what!!';
 });

 $client->add_cb('on_auth_success', function() {
// echo 'it should';
//Here is for sending downstream msg

    //registration token  of my android phone
    $reg_token = array('fy6HF-kKO3M:APA91bGO3F0BKHk6nfPpwf4iLJAZgLag2ZL7uRyRC2vHyE_hmgRCaaj2E5PbhobN0ki7_rfEfOyUjD9-5ml064mULKynalDt69G1FmY_k2CnalMRe-eFzUswPjUrx5yxCZOUfI3tsFSc');



    //Creating a message array 
    $msg = array
    (
        'hello  this is your server' 

    );

    //send back to phone
    $fields = array
    (
        'to'    => $reg_token,
        'message_id' => 1,
        'data'          => $msg,
        'time_to_live' => 600 ,
      'delay_while_idle'=> true,
      'delivery_receipt_requested' => true
    );



    //Using curl to perform http request 
    $ch = curl_init();
    curl_setopt( $ch,CURLOPT_URL, 'https://android.googleapis.com/gcm/send');
    curl_setopt( $ch,CURLOPT_POST, true );
    curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
    curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
    curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );

    //Getting the result 
    $result = curl_exec($ch );
    curl_close( $ch );

    //Decoding json from result 
    $res = json_decode($result);
$myfile = fopen("sendAcktoClient.txt", "w");
fwrite($myfile,  $res);
fclose($myfile);
  }); 

 $client->add_cb('on_error_message',function()
 {
 global $client;
 echo 'error<br/>';
 _info('got on_error_message cb jid'.$client->full_jid->to_string());
 });

$client->start();

?>

I tried changing the message id sent by my client(android phone) but im not getting any new logs.

here's my client's code

package com.example.meer.bustedtracking;

import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;

import com.google.android.gms.gcm.GoogleCloudMessaging;

import java.io.IOException;

/**
 * Created by User on 4/26/2016.
 */

public class SendFromClient extends AsyncTask<String,Void,String>{
    private Context context;

    private String SENDER_ID=""; //project id


    public SendFromClient(Context ctx){context=ctx;}

    @Override
    protected String doInBackground(String... params) {
        String msg = "";
        String id="1";//this should be unique for each msg
        final GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(context);
        try {
            Bundle data = new Bundle();
            data.putString("my_message", "Hello World");
            data.putString("my_action","SAY_HELLO");
           // String id = Integer.toString(msgId.incrementAndGet());
            gcm.send(SENDER_ID + "@gcm.googleapis.com", id, data);
            msg = "Sent message "+ id;
        } catch (IOException ex) {
            msg = "Error :" + ex.getMessage();
        }
        return msg;
    }

    @Override
    protected void onPostExecute(String msg) {
        Log.i("SendFromClient ",msg );
    }
}
1

There are 1 answers

0
Pc hobbit On BEST ANSWER

So after spending a very very long time to find out what is the correct implementation of the server side in GCM I found this question (almost the same as mine) server side in GCM meaning that I CAN implement GCM ( downstream and upstream ) in my localhost Google App engine is not necessary.

The server can be implemented using php (e.g with Jaxl library) or Java (e.g Smack library)

I used this sample code provided by Google Server-Client-GCM

  1. on my android I ran the Android code of the sample

  2. then I imported the server's code in an another Android studio window , supplying my api key and sender key just as instructed by the sample code in GitHub

  3. On my Android phone I just built the project and removed any piece of code that gradle could not find

  4. On my Server I entered the Build code in the Android Studio terminal ,after it was finished I entered the Run code (the codes can found in the server instructions in the Google samples, depending if you want to use a Java server or a Go server)

  5. My server is now running and it it waiting for a client.

  6. I ran the build on my Android phone then I sent a ping (the ping included registration token that was given by GCM when I ran the application on my phone)

  7. Looking back at My server I can see it received a msg from my Android phone (by looking at the registration token that was sent to the server)

The GCM server side was not an obvious implementation to me, I hope my post will help someone.