토큰 저장
푸시 발송을 위한 토큰을 저장합니다.
URL 확인
이 API는 service-api.playnanoo.com 도메인을 사용합니다.
API 정보
- URL:
https://service-api.playnanoo.com/push/v20220701/save - Method:
PUT - 인증 필요: 예
DeviceInfo 상속
이 API의 Req 클래스는 DeviceInfo를 상속받습니다. DeviceInfo의 모든 속성이 자동으로 포함됩니다.
요청 파라미터
| 파라미터 | 타입 | 필수 | 설명 |
|---|---|---|---|
| token | string | 필수 | 푸시 토큰 (FCM/APNS) |
| receive_yn | string | 필수 | 푸시 메시지 수신 여부 ("Y" 또는 "N") |
| receive_night_yn | string | 필수 | 야간 푸시 메시지 수신 여부 ("Y" 또는 "N") |
야간 푸시
야간 푸시는 오후 09시부터 다음날 오전 08시까지의 푸시 메시지를 의미합니다.
응답 데이터
Res 클래스
| 필드 | 타입 | 설명 |
|---|---|---|
| Status | string | 처리 결과 상태 |
Unity C# 구현
BaseResponse 클래스
모든 API 응답의 기본 클래스입니다.
public class BaseResponse
{
public string ErrorCode;
public string Message;
public string WithdrawalKey;
public string BlockKey;
}
필드 설명:
ErrorCode: 에러 코드Message: 에러 메시지WithdrawalKey: 탈퇴 유예 상태인 경우 복구에 필요한 키 (탈퇴 유예 중인 계정만 제공)BlockKey: 차단된 계정인 경우 제공되는 키 (차단된 계정만 제공)
푸시 토큰 저장 클래스
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 방식 호출
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;
}
}
사용 예제
Android (Firebase Cloud Messaging)
using PlayNANOO;
using Firebase;
using Firebase.Messaging;
public class PlayNANOOExample : MonoBehaviour
{
void Start()
{
// Firebase 메시징 초기화
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($"푸시 토큰 저장 성공: {res.Status}");
},
onError: (error) =>
{
Debug.LogError($"푸시 토큰 저장 실패: [{error.ErrorCode}] [{error.Message}]");
}
));
}
}
iOS (Unity 2020.x 이상 - Mobile Notifications)
Unity Mobile Notifications 설치
- Unity Package Manager에서 Mobile Notifications를 설치합니다.
- Project Settings > Mobile Notifications > iOS에서 다음을 체크합니다:
- 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($"푸시 토큰 저장 성공: {res.Status}");
},
onError: (error) =>
{
Debug.LogError($"푸시 토큰 저장 실패: [{error.ErrorCode}] [{error.Message}]");
}
));
}
}
iOS (Unity 2019.x 이하)
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($"푸시 토큰 저장 성공: {res.Status}");
},
onError: (error) =>
{
Debug.LogError($"푸시 토큰 저장 실패: [{error.ErrorCode}] [{error.Message}]");
}
));
}
}
Firebase 설정 필요
Android에서 FCM을 사용하려면 Firebase 프로젝트 설정이 필요합니다:
- Firebase 콘솔에서 프로젝트 생성
- google-services.json 다운로드하여 Unity 프로젝트에 추가
- Firebase Unity SDK (FirebaseMessaging.unitypackage) Import
APNS 설정 필요
iOS에서 APNS를 사용하려면 Apple Developer 계정에서 Push Notification 인증서 설정이 필요합니다.
수신 설정 관리
사용자 설정에 따라 receiveYn과 receiveNightYn 값을 동적으로 변경하여 푸시 수신 설정을 관리할 수 있습니다.