Skip to main content

Remote Notifiction

Overview of remote notification function development​

To implement a remote notification, the following development is required.

  • Registration of a Notification Token
    • Using AndApp SDK, obtain an identifier, a Notification Token, which can identify the AndApp client. A remote notification is to be sent to a Notification Token.
    • Store the obtained Notification Token in the game server. In the game server, store it in association with the user information.
  • Implementation the reception part
    • When game side obtains notification payload of a remote notice, this implementation is required.
  • Implementation of the transmission part
    • Using the Notification Message API of AndApp RESTful API, transmit a remote notification to the specified Notification Token.
    • For the transmission of a remote notification, “Notification onto desktop” or “Data notification only” can be selected.

Registration of a Notification Token​

It is required to obtain the Notification Token using AndApp SDK and to store it, in the game server, in association with the user information of the game. The steps are as follows:

The following is a description of the above sequence diagram (The numbers corresponding to internal processing are omitted).

1. On the game side, call AndApp.NotificationServices.RegisterForNotifications() (Call it after the callback of AndApp.Initialize completion has been called).
6. The Notification Token is passed to the callback.
7. Send the Notification Token to the game server.
8. In the game server, store the Notification Token in association with the user data of the game.

Implementation on the game side is as follows:

warning

For the Notification Token, every time you start game, obtain it and send it to the game server to update it, because the Notification Token can be changed by IdP-Cooperation or uninstallation of the AndApp client.

void Start(){
if (!AndApp.SDK.IsInitialized) {
AndApp.ConfigBuilder builder = AndApp.ConfigBuilder.Instance ().LoadFromFile();
AndApp.SDK.Initialize (builder, (error, launchOptions) => {
if (error != null) {
// Procedure when initialization failed
return;
}

AndApp.NotificationServices.RegisterForNotifications((AndApp.Error registerError, string token) => {
// Send the obtained NotificationToken to the game server.
});
});
}
}

Implementation of the reception part​

As shown below, there are two methods to receive the remote notification on the game side:

  • At the time the game starts, the game side receives notification payload of the remote notification which was received when the game was not started.
  • When the game has already started, the game side receives notification payload of the remote notification by the event handler in the game.

Example of implementation of above two methods is shown below (the example includes Notification Token registration).

void Start(){
if (!AndApp.SDK.IsInitialized) {
AndApp.ConfigBuilder builder = AndApp.ConfigBuilder.Instance ().LoadFromFile();
AndApp.SDK.Initialize (builder, (error, launchOptions) => {
if (error != null) {
// Procedure when initialization failed
return;
}

AndApp.NotificationServices.RegisterForNotifications((AndApp.Error registerError, string token) => {
// Send the obtained NotificationToken to the game server
});

// You can get the parameters at the start of via notification
Debug.Log(launchOptions.Payload.Data["foo"]); // Type of launchOptions.Payload.Data is Dictionary<string, object>.

// Register event handler for reception during game execution.
AndApp.NotificationServices.MessageReceivedEvent += OnMessageReceived;
});
}
}

void OnMessageReceived (AndApp.NotificationPayload payload){
// Title and text of the message can be obtained.
Debug.Log(payload.MessageBody + payload.MessageTitle);

// Any parameter can be obtained by specifying the data parameter.
Debug.Log (payload.Data [ "foo"]); // Type of payload.Data is Dictionary<string, object>.
}
info
launchOptions.Payload.Data and payload.Data can be obtained as Dictionary<string, object>  type.
For example, for the following associative array (JSON structure), obtain each element using the following method.
"data": {
"foo": 1,
"baz": [
{
"id": 1,
"description": "test1",
"lv": 100
},
{
"id": 2,
"description": "test2",
"lv": 200
}
]
"Limited": {
"quux": "foobar"
}
}

The following is the example where each element is obtained using C#.

Dictionary<string, object> data = options.Payload.Data as Dictionary<string, object>;
 
// Example: to obtain a numerical value
int foo = (int)(long)data["foo"];
 
// Example: To obtain an array
List<object> baz = data["baz"] as List<object>;
 
Acquisition example of // Example: To obtain an element of an array
Dictionary<string, object> baz1 = baz[0] as Dictionary<string, object>;
 
// Example (2): To obtain an element of an array
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;
}
}
 
// Example: to obtain an element directly
string quux = (options.Payload.Data["qux"] as Dictionary<string, object>)["quux"];

Implementation of the transmission part​

Using the Notification Message API of AndApp RESTful API, you need to implement the transmission part of the remote notification on the server.

Obtaining an Access Token​

First, to use the Notification Message API, you need to obtain an Access Token by doing a Token Request which uses Client Credentials Grant described in licensed for REST API by OAuth 2.0 .

 By adding notificationMessage:anyUsers:create to the scope parameter which can be specified when doing Token Request above, you can obtain the Access Token which has authority to transmit the remote notification using the Notification Message API.

The following is an example of Request / Response for obtaining the Access Token which is permitted to transmit the remote notification. (The example uses the endpoint for Sandbox.)

 

info

From July 4, 2017, in addition to single Token specification and “@all” specification, multiple Tokens specification (maximum 1000 items) became possible. For details, see  reference of the 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
}

Transmission of the remote notification using the Notification Message API​

The Notification Message API varies with the endpoint of sandbox / production. The URI of the endpoint is as follows:

EnvironmentURIComment
sandboxjava https://sb-api.andapp.jp/1.0.0/notification/messages URI may be changed when API is provided.
productionjava https://api.andapp.jp/1.0.0/notification/messages URI may be changed when API is provided.

For details of the Notification Message API, see Reference Notification Message API .

Transmission method for each remote notification type​

There are two types of remote notification, “Notification onto desktop” and “Data notification only.”  For the outline, see remote notification , in the overview of AndApp function..

Notification onto desktop​

The method to do “Notification onto desktop” using the Notification Message API is as follows:

  • Assign the Notification Token which represents the transmission destination to the request parameter “to.”
  • Assign the text of desktop notification to the request parameter “message.”
    • “title” is the title of the text.
    • “body” is the body of the text.
  • Assign the associative array (JSON Object) to the request parameter “data.” (This setting is optional. There is no problem if you don’t do this setting.)
    • The content of “data” is JSON object, and you can freely enter data in it.

The example of notification Request/Response is shown below. Note: The information about the message to be transmitted will be returned in response parameter.

『デスクトップ上への通知』の例

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
}
Data notification only​

The method to do “Data notification only” using the Notification Message API is as follows:

  • Assign the Notification Token which represents the transmission destination to the request parameter “to.”
  • Assign the associative array (JSON Object) to the request parameter “data.” (This setting is optional. There is no problem if you don’t do this setting.)
    • The content of “data” is a JSON object, and you can freely enter data in it.

An example of notification Request/Response is shown below.

『データ通知のみ』の例

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
}

Specification of the expiring date of the notification​

For the remote notification using the Notification Message API, you can specify the valid period of time (in seconds). By specifying this valid period of time, you can set the time limit for reading the notification in a range from minimum 0 second to maximum 4 weeks from the creation date and time of the remote notification. For messages past this limit of time for reading, the remote notification will not arrive but it will be deleted.

The method to set the expiry date of the notification is as follows:

  • Assign the Notification Token which represents the transmission destination to the request parameter “to.”
  • Assign the content of notification to the request parameter “message” or “data.”
  • Assign the valid period (in seconds) to the request parameter “expiresIn.”

An example of notification Request / Response is shown below.

有効時間を設定した設定例

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
}

Update History​

  • July 10, 2017
    • Maximum number of items of multiple Tokens specification was added.
  • July 4, 2017
    • The comment (Multiple Tokens specification became possible) was added.
  • November 12, 2016
    • Warning (Notification Token needs to be obtained every time) was added.
  • August 18, 2016
    • Non-existent API AndApp.SDK.GetLaunchOptions() was deleted.
    • launchOptions was added to the argument of callback which is set in Initialize.
  • July 28, 2016
    • URI of the Token Endpoint was updated with its change.
    • “scope” is modified.
  • June 20, 2016
    • Newly created