我正在尝试让推送通知在iOS上工作,但我无法访问设备令牌!
我的Unity版本是5.4.1f1。
我已经在XCode中启用了推送通知功能,并且所有证书都设置正确:

在我的start方法的脚本中,我调用以下代码:
UnityEngine.iOS.NotificationServices.RegisterForNotifications
(UnityEngine.iOS.NotificationType.Alert | UnityEngine.iOS.NotificationType.Badge
| UnityEngine.iOS.NotificationType.Sound, true);然后,在update方法中,我调用这个方法:
private bool RegisterTokenWithPlayfab( System.Action successCallback,
System.Action<PlayFabError> errorCallback )
{
byte[] token = UnityEngine.iOS.NotificationServices.deviceToken;
if(token != null)
{
// Registration on backend
}
else
{
string errorDescription = UnityEngine.iOS.NotificationServices.registrationError;
Debug.Log( "Push Notifications Registration failed with: " + errorDescription );
return false;
}
}令牌始终为空,因此每次调用时都会输入else-branch。而且,registrationError一直是空的。
有人能在这件事上给我指个方向吗?我还可以尝试什么,或者我如何才能获得更多关于哪里出了问题的信息?
发布于 2017-06-30 17:54:36
试试这个
Go to your application target. Choose Capabilities and ensure that ‘Push Notifications’ is enabled there.
发布于 2018-03-23 00:24:20
您需要在协程或更新中检查deviceToken。
using UnityEngine;
using UnityEngine.Networking;
using System.Collections;
using NotificationServices = UnityEngine.iOS.NotificationServices;
using NotificationType = UnityEngine.iOS.NotificationType;
public class NotificationRegistrationExample : MonoBehaviour
{
bool tokenSent;
void Start()
{
tokenSent = false;
NotificationServices.RegisterForNotifications(
NotificationType.Alert |
NotificationType.Badge |
NotificationType.Sound, true);
}
void Update()
{
if (!tokenSent)
{
byte[] token = NotificationServices.deviceToken;
if (token != null)
{
// send token to a provider
string token = System.BitConverter.ToString(token).Replace('-', '%');
Debug.Log(token)
tokenSent = true;
}
}
}
}这个实现很糟糕,但是我们没有来自Unity端的回调,所以我们需要继续监听这个变量值。
查看文档:
https://docs.unity3d.com/ScriptReference/iOS.NotificationServices.RegisterForNotifications.html
似乎也需要互联网。我猜那里正在进行一项苹果服务。
是,即使用户拒绝权限,注册错误也为空。
我所做的是使用UniRX并在协程上设置一个带超时的观察值,这样它就不会一直要求它了。如果您接受了,但没有收到令牌,则可能是internet连接。我猜你是在一台真正的设备上运行它。
https://stackoverflow.com/questions/44843441
复制相似问题