android - 使用 php 获取 GCM 的 android 注册 ID

标签 android zend-framework google-cloud-messaging

我正在兜圈子试图解决这个问题。让推送通知在 iOS 上正常工作,现在尝试在 Android GCM 上做同样的事情。我坚持如何公开允许我收集用户 registartionID 的服务。在我的服务器上使用 java 真的很痛苦,所以只有 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());
    }

任何想法将不胜感激。

最佳答案

您需要在您的 php 上创建一个 POST 方法,以便用户“上传”他们的 registration_id。有大量关于如何公开 PHP POST 请求的教程。假设这个 POST 请求的 URI 是 http://yourserver/gcm/send_reg_id,这个 body 的 Content-Type 可以是任何你想要的(文本、json、xml)。假设您使用 json,您的应用将向上述 url 发送一个 POST 请求,正文如下:

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

您的服务器将从 POST 请求正文中提取上述 json 字符串,并将 reg_id 插入您的数据库。

然后在您的应用中,只需使用 gcm-client.jar 库,并在收到 onRegistered() 回调后,将上述 POST 请求发送到您的服务器。

有几点需要考虑:

  1. 将请求设为 https 以确保安全
  2. 添加“user_id”以跟踪用户注册情况,以便将来提供个性化的推送通知。

关于android - 使用 php 获取 GCM 的 android 注册 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11523857/

相关文章:

java - 恢复旧项目但明显合并失败?

android - 如何使用 Material 和 AndroidX 组件将折叠工具栏与 TabLayout 一起使用?

php - 如何在 SocialEngine 中更新表格

android - 当我尝试运行应用程序时构建失败

android - 如何使用requestSync延迟syncAdapter的执行

web-applications - firebase 云消息传递和 firebase 通知之间有什么区别?

android - 如何从 Github 在 Android Studio 1.1 中导入和运行项目?

android - 查看寻呼机和 fragment 生命周期

mysql - Magento/Zend 框架 : Custom Join Statement : ERROR : Can't prepare expression when tableName is instance of Zend_Db_Expr

php - 检查用户是否通过电子邮件激活其帐户并在单击激活链接后显示消息 PHP/JavaScript