远程通知
远程通知功能的开发概要
为了实现远程通知,需要进行以下开发。
- Notification Token 注册
- 使用 AndApp SDK ,获取 Notification Token 这一可识别 AndApp 客户端的标识符。对 Notification Token 发送远程通知
- 将获取的 Notification Token 保存至游戏服务器。在游戏服务器上与用户信息绑定并保存。
- 接收部分的实现
- 要在游戏端获取远程通知的通知有效载荷,需要实现接收部分。
- 发送部分的实现
- 使用 AndApp RESTful API 这一 Notification Message API ,向指定的 Notification Token 发送远程通知。
- 可选择发送“桌面通知”和“只通知数据”这 2 种远程通知。
Notification Token 注册
需要使用 AndApp SDK 获取 Notification Token ,将 Notification Token 与游戏用户信息形成绑定后保存在游戏服务器上。顺序如下。
对上述顺序图的说明如下(已将内部处理编号去除)。
1. 在游戏端调用 AndApp::NotificationServices::RegisterForNotifications() (请在 AndApp::SDK::Initialize 完成回调被调用后执行)
6.Notification Token 被传递至回调。
7.将 Notification Token 发送至游戏服务器。
8.在游戏服务器上将 Notification Token 与游戏用户数据绑定并保存。
游戏端的实现如下所述。
每次启动时,请获取 NotificationToken ,发送至游戏服务器进行更新。通过 IdP 关联和 AndApp 客户端卸载, Notification Token 可能会被更改。
int main(int argc, char *argv[]){
if (!AndApp::SDK::IsInitialized()) {
std::unique_ptr<AndApp::ConfigBuilder> builder = AndApp::ConfigBuilder::Instance();
std::string clientId = "XXXXXXXXXXXXXXXXXXX";
builder->SetClientId(clientId);
AndApp::SDK::Initialize(std::move(builder), [](const AndApp::Error &error, const AndApp::LaunchOptions &launchOptions) {
if (error.GetCode() != 0) {
// 初始化失败时的处理
return;
}
AndApp::NotificationServices::RegisterForNotifications([](const AndApp::Error &error, const std::string ¬ificationToken) {
// 将获取的 NotificationToken 发送至游戏服务器
});
});
}
return 0;
}
接收部分的实现
在游戏端接收远程通知的方法有以下 2 种。
- 对游戏未启动时收到的远程通知的通知有效载荷,在游戏启动时接收
- 游戏已启动时,通过游戏内的事件处理程序,接收远程通知的通知有效载荷
以上2 种实现的实现示例如下所述。(示例中含有 Notification Token 注册)
int main(int argc, char *argv[]){
if (!AndApp::SDK::IsInitialized()) {
std::unique_ptr<AndApp::ConfigBuilder> builder = AndApp::ConfigBuilder::Instance();
std::string clientId = "XXXXXXXXXXXXXXXXXXX";
builder->SetClientId(clientId);
AndApp::SDK::Initialize(std::move(builder), [](const AndApp::Error &error, const AndApp::LaunchOptions &launchOptions) {
if (error.GetCode() != 0) {
// 初始化失败时的处理
return;
}
AndApp::NotificationServices::RegisterForNotifications([](const AndApp::Error ®isterError, const std::string &token) {
if (registerError.GetCode() != 0) {
// RegisterForNotifications 失败时的处理
return;
}
// 将获取的 NotificationToken 发送至游戏服务器
});
// 通过通知启动时,可从 launchOptions 参数获取 Payload 参数
printf("messageTitle: %s", launchOptions.GetPayload().GetMessageTitle().c_str());
// 进行游戏启动中接收时用于获取参数的回调注册
AndApp::NotificationServices::SetMessageReceivedEventCallback([](const AndApp::NotificationPayload &payload){
printf("messageTitle: %s", payload.GetMessageTitle().c_str());
printf("messageBody: %s", payload.GetMessageBody().c_str());
/*
{
"boolValue": true,
"IntValue": 123,
"StringValue": "helloworld",
"ArrayValue": [
"string1",
true,
234
]
}
*/
std::shared_ptr<AndApp::JsonObject> notificationPayload = payload.GetData();
printf("Payload: %s", notificationPayload->ToString());
printf("Payload: ");
printf(" boolValue: %d", notificationPayload->GetBool("boolValue"));
printf(" IntValue: %d", (int)static_cast<INT64>(notificationPayload->GetDouble("IntValue")));
printf(" StringValue: %s", notificationPayload->GetString("StringValue").c_str());
std::shared_ptr<AndApp::JsonArray> subArrayObj = notificationPayload->GetJsonArray("ArrayValue");
printf(" ArrayValue: ");
printf(" %s", subArrayObj->GetString(0).c_str());
printf(" %d", subArrayObj->GetBool(1));
printf(" %lf", (int)static_cast<INT64>(subArrayObj->GetDouble(2)));
});
});
}
}
发送部分的实现
需要在服务器上使用 AndApp RESTful API 的 Notification Message API ,实现远程通知的发送部分。
获取 Access Token
首先,为了使用 Notification Message API ,需要使用 OAuth 2.0 的 REST API 认可 中记载的 Client Credentials Grant 发出 Token Request 来获取 Access Token 。
通过在以上 Token Request 时可指定的 scope 参数中添加 notification_message:create ,可获得有权使用 Notification Message API 发送远程通知的。
获得允许发送远程通知的 Access Token 的请求/响应示例如下。(在示例中使用 Sandbox 用 Endpoint )
从 2017/07/04 起,除了指定单一 Token 和指定@all 之外,还可以指定多 Token (最多 1000 项)。详情请见 Notification Message API 的参考 。
POST /1.0.0/connect/token HTTP/1.1
Host: sb-connect.andapp.jp:443
Authorization: Basic MTY0NDM3NjYyMTM6UkU1eVJ4NFEyMGF0VW1VaUpGckVFc3N1SzQ3cjVEalNWN2NuODRpQXN5Tw==
Content-Type: application/x-www-form-urlencoded
grant_type=client_credentials&scope=notification_message%3Acreate
HTTP/1.1 200 OK
Content-Type: application/json
Cache-Control: no-cache
Pragma: no-cache
{
"token_type": "Bearer",
"access_token": "eyj...",
"expires_in": 3600
}
使用 Notification Message API 发送远程通知
Notification Message API 在 sandbox / production 的终点不同。终点的 URI 如下。
| 环境 | URI | 补充 |
|---|---|---|
| sandbox | java https://sb-api.andapp.jp/1.0.0/notification/messages | 提供时 URI 可能会被更改 |
| production | java https://api.andapp.jp/1.0.0/notification/messages | 提供时 URI 可能会被更改 |
Notification Message API 的详情请见 Notification Message API 的参考 。
各类远程通知的发送方法
远程通知有“桌面通知”和“只通知数据”这 2 种。有关概要说明,请见 AndApp 的功能概要的“远程通知”。
桌面通知
使用 Notification Message API 发送“桌面通知”的方法如下。
- 请在请求参数 to 中指定表示发送目的地的 Notification Token
- 请在请求参数 message 中指定桌面通知的文本
- title 为标题文本
- body 为正文文本
- 请在请求参数 data 中指定关联数组 (JSON Object) (这里可任意设置。也可以不设置)
- 请在 JSON Object 中任意设置该 data 的数据内容
通知请求/响应的示例如下。※ 响应参数会反馈有关发送消息的信息。
“桌面通知”示例
POST /1.0.0/notification/messages HTTP/1.1
Host: sb-api.andapp.jp:443
Content-Type: application/json
Authorization: Bearer {access_token}
{
"to": "836e63e88fd34a71b0cad916f76d17d0",
"message": {
"title": "AndApp",
"body": "Hello, world"
},
"data": {
"foo": 1,
"bar": false
}
}
HTTP/1.1 201 Created
Content-Type: application/json
{
"id": 5102305,
"clientId": 9863675,
"to": "836e63e88fd34a71b0cad916f76d17d0",
"message": {
"title": "AndApp",
"body": "Hello, world"
},
"data": {
"foo": 1,
"bar": false
},
"expiresIn": 86400,
"createdAt": 1465914904,
"updatedAt": 1465914904
}
只通知数据
使用 Notification Message API “只通知数据”的方法如下。
- 请在请求参数 to 中指定表示发送目的地的 Notification Token
- 请在请求参数 data 中指定关联数组 (JSON Object )(这里可任意设置。也可以不设置))
- 请在 JSON Object 中任意设置该 data 的数据内容
通知请求/响应的示例如下。
“只通知数据”的示例
POST /1.0.0/notification/messages HTTP/1.1
Host: sb-api.andapp.jp:443
Content-Type: application/json
Authorization: Bearer {access_token}
{
"to": "836e63e88fd34a71b0cad916f76d17d0",
"data": {
"foo": 1,
"bar": false
}
}
HTTP/1.1 201 Created
Content-Type: application/json
{
"id": 5102305,
"clientId": 9863675,
"to": "836e63e88fd34a71b0cad916f76d17d0",
"message": null,
"data": {
"foo": 1,
"bar": false
},
"expiresIn": 86400,
"createdAt": 1465914904,
"updatedAt": 1465914904
}
指定通知的有效期限
在使用 Notification Message API 的远程通知中,可设置通知的有效时间 (秒) 。通知的有效时间指定后,可设置从远程通知的创建时间起最短 0 秒、最长 4 周的通知浏览期限。过此浏览期限的消息会被删除,远程通知不能送达。
通知有效期限的指定方法如下。
- 请在请求参数 to 中指定表示发送目的地的 Notification Token
- 请在请求参数 message 或 data 中指定通知内容
- 请在请求参数 expiresIn 中指定有效时间,单位为秒
通知请求/响应的示例如下。
设置有效时间的设置示例
POST /1.0.0/notification/messages HTTP/1.1
Host: sb-api.andapp.jp:443
Content-Type: application/json
Authorization: Bearer {access_token}
{
"to": "836e63e88fd34a71b0cad916f76d17d0",
"message": {
"title": "AndApp",
"body": "Hello, world"
},
"data": {
"foo": 1,
"bar": false
},
"expiresIn": 43200
}
HTTP/1.1 201 Created
Content-Type: application/json
{
"id": 5102305,
"clientId": 9863675,
"to": "836e63e88fd34a71b0cad916f76d17d0",
"message": {
"title": "AndApp",
"body": "Hello, world"
},
"data": {
"foo": 1,
"bar": false
},
"expiresIn": 43200
"createdAt": 1465914904,
"updatedAt": 1465914904
}
更新履历
- 2017/07/10
- 原本未记载指定多 Token 的最大数量,故增补。
- 2017/07/04
- 已可指定多 Token ,故增补。
- 2016/11/12
- 添加了每次获取 NotificationToken 的警示语。
- 2016/08/30
- 样例代码 RegisterForNotifications 的错误处理遗漏,故更正。
- 2016/08/19
- 新建