GCM PHP - passing a two-dimensional array

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

I am looking for a solution to solve this problem. Because I think it’s inappropriate to keep pushing the information one by one.

Below are two different scenarios

Single dimensional array (which successfully click on the 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 dimensional arrays (which cannot click on the 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 divided 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;
}
+4
source share
2 answers

You are sending 2 JSON and that is the problem. You can combine them into one:

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

0

, .

@Ali.

, , . , .

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);

, , .

0

Source: https://habr.com/ru/post/1612673/


All Articles