跳到主要内容

远程通知

远程通知功能的开发概要​

为了实现远程通知,需要进行以下开发。

  • 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.Initialize 完成回调被调用后执行)
6.Notification Token 被传递至回调。
7.将 Notification Token 被发送至游戏服务器。
8.在游戏服务器上将 Notification Token 与游戏用户数据绑定并保存。

游戏端的实现如下所述。

注意

每次启动时,请获取NotificationToken ,发送至游戏服务器进行更新。通过IdP 关联和 AndApp 客户端卸载, Notification Token 可能会被更改。

void Start(){
if (!AndApp.SDK.IsInitialized) {
AndApp.ConfigBuilder builder = AndApp.ConfigBuilder.Instance ().LoadFromFile();
AndApp.SDK.Initialize (builder, (error, launchOptions) => {
if (error != null) {
// 初始化失败时的处理
return;
}

AndApp.NotificationServices.RegisterForNotifications((AndApp.Error registerError, string token) => {
// 将获取的NotificationToken发送至游戏服务器
});
});
}
}

接收部分的实现​

在游戏端接收远程通知的方法有以下 2 种。

  • 对游戏未启动时收到的远程通知的通知有效载荷,在游戏启动时接收
  • 游戏已启动时,通过游戏内的事件处理程序,接收远程通知的通知有效载荷

以上2 种实现的实现示例如下所述。(示例中含有Notification Token 注册)

void Start(){
if (!AndApp.SDK.IsInitialized) {
AndApp.ConfigBuilder builder = AndApp.ConfigBuilder.Instance ().LoadFromFile();
AndApp.SDK.Initialize (builder, (error, launchOptions) => {
if (error != null) {
// 初始化失败时的处理
return;
}

AndApp.NotificationServices.RegisterForNotifications((AndApp.Error registerError, string token) => {
// 将获取的NotificationToken发送至游戏服务器
});

// 通过通知启动时,可获取参数
Debug.Log(launchOptions.Payload.Data["foo"]); // launchOptions.Payload.Data は Dictionary<string, object> 型。

// 进行游戏启动中接收用事件处理程序的注册
AndApp.NotificationServices.MessageReceivedEvent += OnMessageReceived;
});
}
}

void OnMessageReceived (AndApp.NotificationPayload payload){
// 可获取消息的标题和正文
Debug.Log(payload.MessageBody + payload.MessageTitle);

// 可获取使用data 参数指定的任意参数
Debug.Log(payload.Data["foo"]); // payload.Data 是 Dictionary<string, object> 型。
}
信息
launchOptions.Payload.Data 和 payload.Data 可作为 Dictionary<string, object> 型获取。以如下关联数组 (JSON 结构) 为例,介绍各要素的获取方法。
"data": {
"foo": 1,
"baz": [
{
"id": 1,
"description": "test1",
"lv": 100
},
{
"id": 2,
"description": "test2",
"lv": 200
}
]
"qux": {
"quux": "foobar"
}
}

通过 C# 获取各要素的示例。

Dictionary<string, object> data = options.Payload.Data as Dictionary<string, object>;
 
// 数值获取示例
int foo = (int)(long)data["foo"];
 
// 数组获取示例
List<object> baz = data["baz"] as List<object>;
 
// 数组要素获取示例
Dictionary<string, object> baz1 = baz[0] as Dictionary<string, object>;
 
// 数组要素获取示例2
foreach(Dictionary<string, object> record in baz)
{
if((int)(long)record["id"] == 2)
{
string description = (string)record["description"];
int lv = (int)(long)record["lv"];
break;
}
}
 
// 直接要素获取示例
string quux = (options.Payload.Data["qux"] as Dictionary<string, object>)["quux"];

发送部分的实现​

需要在服务器上使用 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 参数中添加 notificationMessage:anyUsers:create ,可获得有权使用 Notification Message API 发送远程通知的 Access Token 。

获得允许发送远程通知的 Access Token 的请求/响应示例如下。(在示例中使用 Sandbox 用 Endpoint )

 

信息

从 2017/07/04 起,除了指定单一 Token 和指定 @all 之外,还可以指定多 Token (最多 1000 项)。详情请见 Notification Message API 的参考 。

POST /1.0.0/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=notificationMessage%3AanyUsers%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补充
sandboxjava https://sb-api.andapp.jp/1.0.0/notification/messages 提供时 URI 可能会被更改
productionjava 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/18
    • AndApp.SDK.GetLaunchOptions() 是不存在的 API ,故删除。
    • 在 Initialize 中设置的回调参数中添加了 launchOpitions 。
  • 2016/07/28
    • Token Endpoint 的 URI 已更改,故更正。
    • 更正了 scope 。
  • 2016/06/20
    • 新建