推送 REST API
当 App 安装到用户设备后,如果要使用推送功能,云服务 SDK 会自动生成一个 Installation 对象。Installation 对象包含了推送所需要的所有信息。你可以使用 REST API,通过 Installation 对象进行推送。
请求的 Base URL 可以在开发者中心 > 你的游戏 > 游戏服务 > 云服务 > 即时通讯 > 设置查看。
对于 POST 和 PUT 请求,请求的主体必须是 JSON 格式,而且 HTTP Header 的 Content-Type 需要设置为 application/json。
请求的鉴权是通过 HTTP Header 里面包含的键值对来进行的,详见《存储 REST API》中 请求格式 一节的说明。
Installation
你可以通过 REST API 在云端增加安装对象。 使用 REST API 还可以达成一些客户端 SDK 无法完成的操作,比如查询所有的 installation 来找到一个 channel 的订阅者的集合。
| URL | HTTP | 功能 |
|---|---|---|
| /1.1/installations | POST | 上传安装数据 |
| /1.1/installations/<objectId> | GET | 获取安装数据 |
| /1.1/installations/<objectId> | PUT | 更新安装数据 |
| /1.1/installations | GET | 查询安装数据 |
| /1.1/installations/<objectId> | DELETE | 删除安装数据 |
增加 Installation
创建一个安装对象和普通的对象差不多,只是不同平台有不同的字段。
创建成功后,HTTP 的返回值为 201 Created,Location header 包括了新的安装的 URL:
Status: 201 Created
Location: https://{{host}}/1.1/installations/51ff1808e4b074ac5c34d7fd
返回的 body 是一个 JSON 对象,包括了 objectId 和 createdAt 这个创建对象的时间戳。
{
"createdAt": "2012-04-28T17:41:09.106Z",
"objectId": "51ff1808e4b074ac5c34d7fd"
}
DeviceToken
iOS 设备通常使用 DeviceToken 来唯一标识一台设备。
curl -X POST \
-H "X-LC-Id: {{appid}}" \
-H "X-LC-Key: {{appkey}}" \
-H "Content-Type: application/json" \
-d '{
"deviceType": "ios",
"deviceToken": "abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789",
"channels": [
"public", "protected", "private"
]
}' \
https://{{host}}/1.1/installations
installationId
对于 Android 设备,SDK 会自动生成 uuid 作为 installationId 保存到云端。
curl -X POST \
-H "X-LC-Id: {{appid}}" \
-H "X-LC-Key: {{appkey}}" \
-H "Content-Type: application/json" \
-d '{
"deviceType": "android",
"installationId": "12345678-4312-1234-1234-1234567890ab",
"channels": [
"public", "protected", "private"
]
}' \
https://{{host}}/1.1/installations
installationId 必须在应用内唯一。
获取 Installation
你可以通过 GET 方法请求创建的时候 Location 表示的 URL 来获取 Installation 对象。比如,获取上面创建的对象:
curl -X GET \
-H "X-LC-Id: {{appid}}" \
-H "X-LC-Key: {{appkey}}" \
https://{{host}}/1.1/installations/51ff1808e4b074ac5c34d7fd
返回的 JSON 对象包含所有用户提供的字段,加上 createdAt、updatedAt 和 objectId 字段:
{
"deviceType": "ios",
"deviceToken": "abcdefghijklmnopqrstuvwzxyrandomuuidforyourdevice012345678988",
"channels": [
""
],
"createdAt": "2012-04-28T17:41:09.106Z",
"updatedAt": "2012-04-28T17:41:09.106Z",
"objectId": "51ff1808e4b074ac5c34d7fd"
}
更新 Installation
安装对象可以向相应的 URL 发送 PUT 请求来更新。例如,通过设置 channels 属性来订阅某个推送频道:
curl -X PUT \
-H "X-LC-Id: {{appid}}" \
-H "X-LC-Key: {{appkey}}" \
-H "Content-Type: application/json" \
-d '{
"deviceType": "ios",
"deviceToken": "abcdefghijklmnopqrstuvwzxyrandomuuidforyourdevice012345678988",
"channels": [
"",
"foo"
]
}' \
https://{{host}}/1.1/installations/51ff1808e4b074ac5c34d7fd
再比如退订一个频道:
curl -X PUT \
-H "X-LC-Id: {{appid}}" \
-H "X-LC-Key: {{appkey}}" \
-H "Content-Type: application/json" \
-d '{
"channels": {
"__op":"Remove",
"objects":["customer"]
}
}' \
https://{{host}}/1.1/installations/51ff1808e4b074ac5c34d7fd
channels 本质上是数组属性,因此可以使用标准的数组操作。
又比如添加自定义属性:
curl -X PUT \
-H "X-LC-Id: {{appid}}" \
-H "X-LC-Key: {{appkey}}" \
-H "Content-Type: application/json" \
-d '{
"userObjectId": "<用户的 objectId>"
}' \
https://{{host}}/1.1/installations/51ff1808e4b074ac5c34d7fd