Android Notifications push php error -
i trying send notification push user, there no way complete that,
there code use in php
$message = new gcm(); $message->sendmessagetophone(2, $message,$valor); class gcm { function sendmessagetophone($collapsekey, $messagetext, $gcmcode) { $apikey = 'there 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 error when execute php
catchable fatal error: object of class gcm not converted string in /path/gcm.php on line 25
line 25 = curl_setopt($ch, curlopt_postfields, $data);
hi friend had develop push notification part bundle on symfony 2.x use class , change namespaces, add key, hope it's you. , i'm sorry bad english.
remember id of device has registered in cloud.
from controller:
/* * google api key */ define("google_api_key", "xxxxxxxxxxxxxx"); // place 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 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; } }
Comments
Post a Comment