2012-08-05 45 views
3

我試圖發送推送通知給我的android應用程序使用PHP服務器與Zend框架,但我得到的HttpResponse爲:請求無法解析爲JSON或包含無效的字段,錯誤代碼400 我已經使用了服務器代碼作爲Zend框架的演示給定的,主要內容如下:從PHP服務器使用Zend框架收到HttpResponse 400

<?php 
require_once 'Zend/Mobile/Push/Gcm.php'; 
require_once 'Zend/Mobile/Push/Message/Gcm.php'; 

$regId = $_POST["registrationid"]; 

$message = new Zend_Mobile_Push_Message_Gcm(); 
$message->setId(time()); 
$message->addToken($regId); 
$message->setData(array(
    'foo' => 'bar', 
    'bar' => 'foo', 
)); 

$gcm = new Zend_Mobile_Push_Gcm(); 
$gcm->setApiKey('my api key'); 

try { 
    $response = $gcm->send($message); 
} catch (Zend_Mobile_Push_Exception $e) { 
    // all other exceptions only require action to be sent or implementation of exponential backoff. 
    die($e->getMessage()); 
} 

// handle all errors and registration_id's 
foreach ($response->getResults() as $k => $v) { 
    if ($v['registration_id']) { 
     printf("%s has a new registration id of: %s\r\n", $k, $v['registration_id']); 
    } 
    if ($v['error']) { 
     printf("%s had an error of: %s\r\n", $k, $v['error']); 
    } 
    if ($v['message_id']) { 
     printf("%s was successfully sent the message, message id is: %s", $k, $v['message_id']); 
    } 
} 

回答

0

我沒有我的確切URL方便,我想上我的手機,但我最近在我的博客中提到了這一點。請參閱http://www.rogerethomas.com 您需要的是使用瀏覽器API密鑰,而不是服務器密鑰。不要問爲什麼,但似乎谷歌文檔有一個錯誤。

編輯:這裏是一個最近對我的回答鏈接從另一個類似的問題: GCM with PHP (Google Cloud Messaging)