2012-07-17 64 views
1

我正在圍繞試圖弄清楚這一點。現在推送通知適用於iOS,現在嘗試與android GCM一樣。我被困在如何公開允許我收集用戶註冊ID的服務。使用java是我的服務器真正的痛苦,所以一個PHP唯一的解決方案將是有用的。 我的通知腳本看起來像這樣至今:使用php獲取GCM的android註冊ID使用php

require_once('config.php'); 
require_once 'Zend/Mobile/Push/Gcm.php'; 
require_once 'Zend/Mobile/Push/Message/Gcm.php'; 

$con = mysql_connect($CONFIG['db']['host'] , $CONFIG['db']['user'], $CONFIG['db']['pass']); 
if (!$con) { 
    echo "DBConnectionError: Could not connect to database"; 
} 

$db = mysql_select_db($CONFIG['db']['database'], $con); 
if (!$db) { 
    echo "DBSelectError: Could not select database"; 
} 

// optain list of devices to send pn to 
$qt = "SELECT uid userid, apntoken " 
    . "FROM members " 
    . "WHERE apntoken IS NOT NULL " 
    . "AND uid IN (" 
    . "SELECT fuid userid " 
    . "FROM avail " 
    . "WHERE apnsent IS NOT TRUE " 
    . "UNION " 
    . "SELECT uid userid " 
    . "FROM availbinary " 
    . "WHERE apnsent IS NOT TRUE " 
    . ") "; 

//send gcm message 
    $message = new Zend_Mobile_Push_Message_Gcm(); 
    $message->setId(time()); 
    $message->addToken($tokenRec['apntoken']); 
    $message->setData($alert); 

    $gcm = new Zend_Mobile_Push_Gcm(); 
    $gcm->setApiKey('AIzaSyCxxxxxxxxxxxxxxxnFn5mGkM'); 

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

任何想法,將不勝感激。

回答

2

你需要在你的php上創建一個POST方法讓用戶'上傳'他們的registration_id。有大量關於如何公開PHP POST請求的教程。假設這個POST請求的URI是http://yourserver/gcm/send_reg_id,並且這個body的Content-Type可以是任何你想要的(text,json,xml)。比方說,你用JSON去,你的應用程序將發送POST請求到上述網址,用了以下機身:

{ "reg_id" : "asdadsafdsf34ZXZZZZx", "user_id" : "xxxxxx" } 

你的服務器將提取POST請求體上面JSON字符串,並插入REG_ID到你的數據庫。

然後在您的應用程序中,只需要使用gcm-client.jar庫,並在收到onRegistered()回調後,將上述POST請求發送到您的服務器。

有一些事情要考慮:

  1. 使請求HTTPS是安全的
  2. 添加「USER_ID」由用戶跟蹤註冊,以在未來提供個性化推送通知。
+0

這就是我正在尋找的,非常感謝! – Zeb99 2012-07-17 16:27:20