跳转到主要内容

保存令牌

保存用于发送推送通知的令牌。

调用信息

PushNotification.Save(string token, PlayNANOODelegate callback) {}
PushNotification.Save(string token, bool receiveYn, bool receiveNightYn, PlayNANOODelegate callback) {}

调用详细信息

参数说明类型
token推送令牌string
receiveYn是否接收推送消息boolean
receiveNightYn是否接收夜间推送消息(晚上 9 点至次日早上 8 点)boolean
callback通信结果PlayNANOODelegate

Android 请求

using PlayNANOO;
using Firebase;

public class PlayNANOOExample : MonoBehaviour
{
Plugin plugin;

void Start() {
plugin = Plugin.GetInstance();

Firebase.Messaging.FirebaseMessaging.TokenReceived += OnTokenReceived;
Firebase.Messaging.FirebaseMessaging.MessageReceived += OnMessageReceived;
}

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

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

void SaveToken(string token)
{
string token = "string";
bool receiveYn = true;
bool receiveNightYn = true;
plugin.PushNotification.Save(token, receiveYn, receiveNightYn, (status, error, jsonString, values) =>
{
if (status.Equals(Configure.PN_API_STATE_SUCCESS))
{
Debug.Log("Success");
}
else
{
Debug.Log("Fail");
}
});
}
}

iOS 请求(Unity 2020.x 及以上版本)

图1. 安装 Mobile Notifications

  • 在 Unity Package Manager 中安装 Mobile Notifications。

图2. 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
{
Plugin plugin;

void Start()
{
plugin = Plugin.GetInstance();

StartCoroutine(RequestAuthorization());
}

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

plugin.PushNotification.Save(req.DeviceToken, (status, error, jsonString, values) => {
Debug.Log(jsonString);
});
}
}
}

iOS 请求(Unity 2019.x 及以下版本)

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

public class PlayNANOOExample : MonoBehaviour
{
Plugin plugin;

void Start()
{
NotificationServices.RegisterForNotifications(NotificationType.Alert | NotificationType.Badge | NotificationType.Sound, true);

plugin = Plugin.GetInstance();

IOSToken();
}

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

void SaveToken(string token)
{
string token = "string";
bool receiveYn = true;
bool receiveNightYn = true;
plugin.PushNotification.Save(token, receiveYn, receiveNightYn, (status, error, jsonString, values) =>
{
if (status.Equals(Configure.PN_API_STATE_SUCCESS))
{
Debug.Log("Success");
}
else
{
Debug.Log("Fail");
}
});
}
}

通信结果

HTTP/1.1 200 OK
Content-Type: application/json;charset=UTF-8
{
"Status" : "String"
}

通信结果详细信息

数据键说明类型
Status处理结果string