2013-05-08 61 views
1

我試圖通知推送發送到用戶,但是沒有辦法完成的是,Android的通知將PHP錯誤

還有就是我在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;  
} 

和我得到這個錯誤,當我執行的PHP

開捕致命錯誤:類GCM的對象無法在/path/gcm.php在線25

第25行= curl_setopt($ CH轉換爲字符串,CURLOPT_POSTFIELDS,$ data);

回答

1

嗨朋友我不得不爲Symfony 2.x開發一些捆綁推送通知部分。只使用Class並更改名稱空間,添加您的密鑰,希望它對您有所幫助。我很抱歉我的英語不好。

請記住,設備的ID必須在雲中註冊。

從控制器:

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

類:

<?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; 
    } 

} 
1

你缺少GCM的構造。在你的php裏面增加以下幾行gcm

function __construct() { 

}