【发布时间】:2014-04-19 12:14:01
【问题描述】:
我已经编写了一个基于 Slim PHP 网络的 API,该网络将被 Web(浏览器)和 Android 客户端使用,我有一个关于为 Android 客户端实现推送通知的 Google Cloud Messaging 的问题。基本上,这是我目前的数据结构
users:
--Fields:
----id
----name
----email
----password
posts:
--Fields:
----id
----post_title
----post_category
----post_content
----image_url
----created_at
comments:
--Fields:
----id
----comment
----mood
----created_at
post_comments (which holds the relationship between the post and the comment):
--Fields:
----id
----post_id
----comment_id
user_comments (which holds the relationship between the user and the comment):
--Fields:
----id
----user_id
----comment_id
我想要做的是在发布表添加一行时向所有设备(满足某些要求)发送通知。我很确定我了解 PHP 的实际推送功能,但我不确定如何构造它来注册在 android 设备上运行的设备。我正在考虑向我的用户表中添加两个字段,如下所示:
----has_device
----device_registration_id
并让 android 设备在登录时发送 has_device=1 参数(而不是 web 客户端发送 has_device=0 表示 false)并检查该用户的 device_registration_id 字段是否为空,如果是则生成新的注册id 并填充该字段。我在正确的轨道上吗?
编辑:这将是我的 PHP 发送函数。
function sendNotification( $apiKey, $registrationIdsArray, $messageData )
{
$headers = array("Content-Type:" . "application/json", "Authorization:" . "key=" . $apiKey);
$data = array(
'data' => $messageData,
'registration_ids' => $registrationIdsArray
);
$ch = curl_init();
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
curl_setopt( $ch, CURLOPT_URL, "https://android.googleapis.com/gcm/send" );
curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 0 );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, 0 );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode($data) );
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
【问题讨论】:
-
我只是想知道将这两个字段添加到 users 表是否就足够了,或者我是否需要一个单独的 gcm_users 表。我以前从未使用过 GCM,所以我不知道业务逻辑是如何工作的。
标签: php android mysql push-notification google-cloud-messaging