gpt4 book ai didi

php - 用于使用 Google Cloud 消息传递的 API 的数据结构

转载 作者:行者123 更新时间:2023-11-29 00:15:22 25 4
gpt4 key购买 nike

我已经编写了一个基于 Slim PHP 网络的 API,供网络(浏览器)和 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;
}

最佳答案

添加这两个字段是不够的,因为注册 ID 绑定(bind)到设备,而不是当前登录该设备的用户。

如果一个用户在设备上注销而另一个用户登录,您建议的实现意味着两个用户在您的数据库中将具有相同的注册 ID,并且向所有设备发送通知将向该设备发送两个通知。

如果您清除注销用户行中的注册 ID 列,则可以解决此问题。如果您希望拥有 Adnroid 设备的用户占所有用户的一小部分,那么拥有一个单独的 gcm_users 表会更有意义。这样您就不需要遍历更大的用户表来向所有设备发送 GCM 消息。

最后,这里还有一些关于how to store a GCM registration ID into mysql的补充建议.

关于php - 用于使用 Google Cloud 消息传递的 API 的数据结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23169928/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com