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 of the reception part
- When game side obtains notification payload of a remote notification, this implementation is required.
- Implementation of the transmission part
- Using Notification Message API of AndApp RESTful API, transmit a remote notification to the specified Notification Token.
- For the transmission of a remote notification, two types of “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).
-
On the game side, call AndApp::NotificationServices::RegisterForNotifications() (Call it after the callback of AndApp::SDK::Initialize completion has been called).
-
The Notification Token is passed to the callback.
-
Send the Notification Token to the game server.
-
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:
For the NotificationToken, every time you start a 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 AndApp client.
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) {
// initialization failure
return;
}
AndApp::NotificationServices::RegisterForNotifications([](const AndApp::Error &error, const std::string ¬ificationToken) {
// Send the acquired NotificationToken to the game server
});
});
}
return 0;
}
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).
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) {
// Process at the time initialization failure
return;
}
AndApp::NotificationServices::RegisterForNotifications([](const AndApp::Error ®isterError, const std::string &token) {
if (registerError.GetCode() != 0) {
// RegisterForNotifications process at the time of failure
return;
}
// Send the acquired NotificationToken to the game server
});
// Payload parameter can be obtained from argument launchOptions at startup via notification
printf("messageTitle: %s", launchOptions.GetPayload().GetMessageTitle().c_str());
// game to register a callback for the parameter acquisition of the case received during the start-up
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)));
});
});
}
}
Implementation of the transmission part
Using 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
By adding Notification_message: 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 the example of Request / Response for obtaining the Access Token which is permitted to transmit the remote notification. (The example uses the endpoint for Sandbox.)
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/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
}
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:
| Environment | URI | Comment |
|---|---|---|
| sandbox | java https://sb-api.andapp.jp/1.0.0/notification/messages | URI may be changed when API is provided. |
| production | java https://api.andapp.jp/1.0.0/notification/messages | URI may be changed when API is provided. |
For details of Notification Message API, 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 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 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 JSON object, and you can freely enter data in it.
The 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 expiry 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 limit of time 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 the message 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 the 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
- A warning (NotificationToken needs to be obtained every time) was added.
- August 30, 2016
- Omission of error procedure in RegisterForNotifications in sample code was corrected.
- Omission of error procedure in RegisterForNotifications in sample code was corrected.
- Newly created