1

i am trying to send a notification push to an user, but there is no way to complete that,

There is the code i use in php

    $message = new gcm(); 
    $message->sendMessageToPhone(2, $message,$valor);

    class gcm 
    { 
    function sendMessageToPhone($collapseKey, $messageText, $gcmcode)  
{           
         $apiKey = 'there is my apikey';  
         $headers = array('Authorization:key=' . $apiKey); 
         $data = array(      'registration_id' => $gcmcode,      'collapse_key' => $collapseKey,      'data.message' => $messageText);
         $ch = curl_init(); 
         curl_setopt($ch, CURLOPT_URL, "https://android.googleapis.com/gcm/send"); 
         if ($headers)     curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);  
         curl_setopt($ch, CURLOPT_POST, true); 
         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
         curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
         $response = curl_exec($ch);  
         $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);  
         if (curl_errno($ch)) {     return 'fail';    }    
         if ($httpCode != 200) {     return 'status code 200';    }  
         curl_close($ch);   
         return $response;      
}

and i get that error when i execute the php

Catchable fatal error: Object of class gcm could not be converted to string in /path/gcm.php on line 25

Line 25 = curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

2 Answers 2

1

Hi friend I had to develop a push notification part for some bundle on Symfony 2.x only use the Class and change the namespaces, add you key, I hope it's help you. and i'm sorry for my bad english.

Remember that the Id of the device has to be registered in the Cloud.

from controller:

    /*
     * Google API Key
     */
    define("GOOGLE_API_KEY", "xxxxxxxxxxxxxx"); // Place your Google API Key
    $gcm = new \MessageBundle\Classes\GCM();

    $message = "Hello";
$registatoin_ids = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
$gcm->send_notification($registatoin_ids, $message);

The class:

    <?php


/**
 * GCM
 *
 * @author Esteban lopez
 */
class GCM {


    // constructor
    function __construct() {

    }

    /**
     * Sending Push Notification
     */
    public function send_notification($registatoin_ids, $message) {

        // Set POST variables
        $url = 'https://android.googleapis.com/gcm/send';

        $fields = array(
            'registration_ids' => $registatoin_ids,
            'data' => $message,
        );

        $headers = array(
            'Authorization: key=' . GOOGLE_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, $headers);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        // Disabling SSL Certificate support temporarly
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));

        // Execute post
        $result = curl_exec($ch);
        if ($result === FALSE) {
            die('Curl failed: ' . curl_error($ch));
        }

        // Close connection
        curl_close($ch);
       // echo $result;
    }

}
Sign up to request clarification or add additional context in comments.

Comments

1

You are missing the constructor of gcm. Add the following lines to your php inside class gcm

function __construct() {

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.