GCM PHP – Passing Two-Dimensional Array

I am trying to use Google Cloud Messaging (GCM) to push a two-dimensional array to device. However, I found that GCM only able to send a single array at one time.

I am looking for a solution to overcome this issue. Because I think it is not practical to keep pushing information one by one.

Read More

Below are two different scenarios

Single Dimension Array(Which is successfully to push to device)

Array
(
    [pump_name] => LEVO 92
    [pump_price] => 2.5
)
1
{"multicast_id":8959934119853137719,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1445482576757470%9dde29c5f9fd7ecd"}]}
Array
(
    [pump_name] => LEVO 95
    [pump_price] => 3
)
1
{"multicast_id":6988128903201803494,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1445482576797158%9dde29c5f9fd7ecd"}]}

Two Dimension Array(Which is fail to push to device)

Array
(
    [0] => Array
        (
            [pump_name] => LEVO 92
            [pump_price] => 2.5
        )

    [1] => Array
        (
            [pump_name] => LEVO 95
            [pump_price] => 3
        )

)
1
Field "data" must be a JSON array: [{"pump_name":"LEVO 92","pump_price":"2.5"},{"pump_name":"LEVO 95","pump_price":"3"}]

Codes (I split it into two parts)

Pump Price Function

function pump_price() {

    global $wpdb;

    $result = array();
    $temp_result = array();
    $counter = 1;

    $rows = $wpdb->get_results($wpdb->prepare("SELECT DISTINCT meta_value FROM postmeta WHERE meta_key LIKE %s", 'pump_price_list_%', 'pump_%'));

    if($rows) :
        foreach($rows AS $row) :
            if($counter % 2 == 0) :

                // Store Value Into Temporary Array
                $temp_result["pump_price"] = $row->meta_value;

                array_push($result, $temp_result);

                // Unset Temporary Array
                unset($temp_result);
                $temp_result = array();

            else :

                $temp_result['pump_name'] = $row->meta_value;

            endif;
            $counter++;
        endforeach;
    endif;

    sendGoogleCloudMessage($result);

}

GCM Function

function sendGoogleCloudMessage($result) {

    define( 'API_ACCESS_KEY', 'MY-API-KEY' );

    $registrationIds = array('MY-DEVICE-ID');

    $headers = array (
        'Authorization: key=' . API_ACCESS_KEY,
        'Content-Type: application/json'
    );

    $fields = array (
        'registration_ids'  => $registrationIds,
        'data'              => $result
    );


    $ch = curl_init();
    curl_setopt( $ch,CURLOPT_URL, 'https://android.googleapis.com/gcm/send' );
    curl_setopt( $ch,CURLOPT_POST, true );
    curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
    curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
    curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
    curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
    $results = curl_exec($ch );
    curl_close( $ch );

    echo $results;
}

Related posts

2 comments

  1. You are sending 2 JSONs and thats the problem. You can combine them into one like this:

    {  
        "array1": {
                   "pump_name":"LEVO 92",
                   "pump_price":"2.5"
                  },
        "array2": {
                   "pump_name":"LEVO 95",
                   "pump_price":"3"
                  }
    }
    

    Then you have to spilt them client side

  2. Hi for those people who looking solution for this issue.

    I managed to find a solution after referring to @Ali suggestion.

    But there are some codes need to change before you can pass two-dimensional data or more. Please read the solution below.

    Solution

    Convert your array into Object

    if($rows) :
        foreach($rows AS $row) :
            if($counter % 2 == 0) :
    
                // Store Value Into Temporary Array
                $temp_result["pump_price"] = $row->meta_value;
    
                array_push($result, (object)$temp_result);
    
                // Unset Temporary Array
                unset($temp_result);
                $temp_result = array();
    
            else :
    
                $temp_result['pump_name'] = $row->meta_value;
    
            endif;
            $counter++;
        endforeach;
    endif;
    
    sendGoogleCloudMessage((object)$result);
    

    Please correct me if this is not an ideal solution.

Comments are closed.