Skip to main content

Token Save

Saves the token for push notifications.

URL Verification

This API uses the service-api.playnanoo.com domain.

API Information

  • URL: https://service-api.playnanoo.com/push/v20220701/save
  • Method: PUT
  • Authentication Required: Yes
DeviceInfo Inheritance

The Req class of this API inherits from DeviceInfo. All properties of DeviceInfo are automatically included.

Request Parameters

ParameterTypeRequiredDescription
tokenstringRequiredPush token (FCM/APNS)
receive_ynstringRequiredWhether to receive push messages ("Y" or "N")
receive_night_ynstringRequiredWhether to receive night push messages ("Y" or "N")
Night Push

Night push refers to push messages from 9 PM to 8 AM the next day.

Response Data

Res Class

FieldTypeDescription
StatusstringProcessing result status

Unity C# Implementation

BaseResponse Class

The base class for all API responses.

public class BaseResponse
{
public string ErrorCode;
public string Message;
public string WithdrawalKey;
public string BlockKey;
}

Field descriptions:

  • ErrorCode: Error code
  • Message: Error message
  • WithdrawalKey: Key required for recovery if in withdrawal grace period (provided only for accounts in withdrawal grace period)
  • BlockKey: Key provided for blocked accounts (provided only for blocked accounts)

Push Token Save Class

using System;
using System.Collections;
using UnityEngine.Networking;

public class PushSave
{
static string url = "https://service-api.playnanoo.com/push/v20220701/save";

[Serializable]
public class Req : DeviceInfo
{
public string token;
public string receive_yn;
public string receive_night_yn;

public IEnumerator Send(
string token,
bool receiveYn,
bool receiveNightYn,
Action<Res> onSuccess,
Action<BaseResponse> onError)
{
if (!string.IsNullOrEmpty(token)) this.token = token;
this.receive_yn = receiveYn ? "Y" : "N";
this.receive_night_yn = receiveNightYn ? "Y" : "N";

// PUT method call
yield return HttpClient.Send<Req, Res>(
UnityWebRequest.kHttpVerbPUT,
url,
requireToken: true,
body: this,
onSuccess: onSuccess,
onError: onError
);
}
}

[Serializable]
public class Res : BaseResponse
{
public string Status;
}
}

Usage Examples

Android (Firebase Cloud Messaging)

using PlayNANOO;
using Firebase;
using Firebase.Messaging;

public class PlayNANOOExample : MonoBehaviour
{
void Start()
{
// Initialize Firebase Messaging
Firebase.Messaging.FirebaseMessaging.TokenReceived += OnTokenReceived;
Firebase.Messaging.FirebaseMessaging.MessageReceived += OnMessageReceived;
}

public void OnTokenReceived(object sender, TokenReceivedEventArgs token)
{
Debug.Log("Received Registration Token: " + token.Token);
SavePushToken(token.Token);
}

public void OnMessageReceived(object sender, MessageReceivedEventArgs e)
{
Debug.Log("Received a new message from: " + e.Message.From);
}

void SavePushToken(string fcmToken)
{
PushSave.Req req = new PushSave.Req();

StartCoroutine(req.Send(
token: fcmToken,
receiveYn: true,
receiveNightYn: true,
onSuccess: res =>
{
Debug.Log($"Push token saved successfully: {res.Status}");
},
onError: (error) =>
{
Debug.LogError($"Failed to save push token: [{error.ErrorCode}] [{error.Message}]");
}
));
}
}

iOS (Unity 2020.x and later - Mobile Notifications)

Unity Mobile Notifications Installation
  1. Install Mobile Notifications from Unity Package Manager.
  2. In Project Settings > Mobile Notifications > iOS, check the following:
    • Enable Push Notifications
    • Register for Push Notifications on App Launch
using PlayNANOO;
using Unity.Notifications.iOS;

public class PlayNANOOExample : MonoBehaviour
{
void Start()
{
StartCoroutine(RequestAuthorization());
}

IEnumerator RequestAuthorization()
{
var authorizationOption = AuthorizationOption.Alert | AuthorizationOption.Badge | AuthorizationOption.Sound;
using (var req = new AuthorizationRequest(authorizationOption, true))
{
while (!req.IsFinished)
{
yield return null;
}

if (req.Granted && !string.IsNullOrEmpty(req.DeviceToken))
{
Debug.Log("Push notification authorized. Token: " + req.DeviceToken);
SavePushToken(req.DeviceToken);
}
else
{
Debug.Log("Push notification authorization denied");
}
}
}

void SavePushToken(string apnsToken)
{
PushSave.Req req = new PushSave.Req();

StartCoroutine(req.Send(
token: apnsToken,
receiveYn: true,
receiveNightYn: true,
onSuccess: res =>
{
Debug.Log($"Push token saved successfully: {res.Status}");
},
onError: (error) =>
{
Debug.LogError($"Failed to save push token: [{error.ErrorCode}] [{error.Message}]");
}
));
}
}

iOS (Unity 2019.x and earlier)

using PlayNANOO;
using NotificationServices = UnityEngine.iOS.NotificationServices;
using NotificationType = UnityEngine.iOS.NotificationType;

public class PlayNANOOExample : MonoBehaviour
{
void Start()
{
NotificationServices.RegisterForNotifications(
NotificationType.Alert | NotificationType.Badge | NotificationType.Sound,
true);

IOSToken();
}

void IOSToken()
{
byte[] token = NotificationServices.deviceToken;
if (token != null)
{
string tokenString = System.BitConverter.ToString(token).Replace("-", "");
SavePushToken(tokenString);
}
}

void SavePushToken(string apnsToken)
{
PushSave.Req req = new PushSave.Req();

StartCoroutine(req.Send(
token: apnsToken,
receiveYn: true,
receiveNightYn: false,
onSuccess: res =>
{
Debug.Log($"Push token saved successfully: {res.Status}");
},
onError: (error) =>
{
Debug.LogError($"Failed to save push token: [{error.ErrorCode}] [{error.Message}]");
}
));
}
}
Firebase Setup Required

To use FCM on Android, Firebase project setup is required:

  1. Create a project in Firebase console
  2. Download google-services.json and add it to Unity project
  3. Import Firebase Unity SDK (FirebaseMessaging.unitypackage)
APNS Setup Required

To use APNS on iOS, Push Notification certificate setup is required in your Apple Developer account.

Reception Settings Management

You can manage push reception settings by dynamically changing the receiveYn and receiveNightYn values according to user settings.