首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Unity3D iOS设备令牌始终为空

Unity3D iOS设备令牌始终为空
EN

Stack Overflow用户
提问于 2017-06-30 17:43:45
回答 2查看 1.1K关注 0票数 0

我正在尝试让推送通知在iOS上工作,但我无法访问设备令牌!

我的Unity版本是5.4.1f1。

我已经在XCode中启用了推送通知功能,并且所有证书都设置正确:

在我的start方法的脚本中,我调用以下代码:

代码语言:javascript
复制
UnityEngine.iOS.NotificationServices.RegisterForNotifications
    (UnityEngine.iOS.NotificationType.Alert | UnityEngine.iOS.NotificationType.Badge
    | UnityEngine.iOS.NotificationType.Sound, true);

然后,在update方法中,我调用这个方法:

代码语言:javascript
复制
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一直是空的。

有人能在这件事上给我指个方向吗?我还可以尝试什么,或者我如何才能获得更多关于哪里出了问题的信息?

EN

回答 2

Stack Overflow用户

发布于 2017-06-30 17:54:36

试试这个

Go to your application target. Choose Capabilities and ensure that ‘Push Notifications’ is enabled there.

票数 0
EN

Stack Overflow用户

发布于 2018-03-23 00:24:20

您需要在协程或更新中检查deviceToken。

代码语言:javascript
复制
    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连接。我猜你是在一台真正的设备上运行它。

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44843441

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档