保存令牌
保存用于推送发送的令牌。
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") |
夜间推送
夜间推送是指晚上 9 点到次日早上 8 点之间的推送消息。
响应数据
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 项目中
- Import Firebase Unity SDK(FirebaseMessaging.unitypackage)
需要 APNS 设置
在 iOS 上使用 APNS 需要在 Apple Developer 账号中进行 Push Notification 证书设置。
接收设置管理
可以根据用户设置动态更改 receiveYn 和 receiveNightYn 的值来管理推送接收设置。