2012-07-17 38 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