0

我有一个基本的统一场景,其中涉及一个链接到 OnClick 方法的按钮。我知道这部分有效,因为我有 debug.log 告诉我。

我尝试在整个互联网上查找和堆栈溢出,但找不到我的问题的答案。

我怀疑问题的部分是授权请求。我正在使用 Unity 模拟器以及远程 5 应用程序进行测试。当我玩我的项目时,任何一个测试表格都没有要求通知。所以我的主要问题是为什么授权请求失败。我正在使用 Unity 提供的移动通知包,并尝试使用其中的设置,例如在应用启动时请求授权。

让我感到困惑的部分原因是我直接从 Unity 文档中获得了大部分代码。

using System;
using System.Collections;
using System.Collections.Generic;
using Unity.Notifications.iOS;
using UnityEngine;

public class NotificationManager : MonoBehaviour
{
    private void Start()
    {
        StartCoroutine(RequestAuthorization());
    }
    IEnumerator RequestAuthorization()
    {
        using (var req = new AuthorizationRequest(AuthorizationOption.Alert | AuthorizationOption.Badge, true))
        {
            Debug.Log(req.IsFinished);
            while (!req.IsFinished)
            {
                yield return null;
            };

            string res = "\n RequestAuthorization:";
            res += "\n finished: " + req.IsFinished;
            res += "\n granted :  " + req.Granted;
            res += "\n error:  " + req.Error;
            res += "\n deviceToken:  " + req.DeviceToken;
            Debug.Log(res);
        }
    }
    
    public void OnClick()
    {
        Debug.Log("Sending Notif");
        SendNotification();
    }
    
    private void SendNotification()
    {
        var timeTrigger = new iOSNotificationTimeIntervalTrigger()
        {
            TimeInterval = new TimeSpan(0, 0, 1),
            Repeats = false
        };

        var notification = new iOSNotification()
        {
            // You can specify a custom identifier which can be used to manage the notification later.
            // If you don't provide one, a unique string will be generated automatically.
            Identifier = "testNotification",
            Title = "Test Notification",
            Body = "Testing the notification capability",
            Subtitle = "What does the subtitle look like",
            ShowInForeground = true,
            ForegroundPresentationOption = (PresentationOption.Alert | PresentationOption.Sound),
            CategoryIdentifier = "category_a",
            ThreadIdentifier = "thread1",
            Trigger = timeTrigger,
        };

        iOSNotificationCenter.ScheduleNotification(notification);
    }
}
4

0 回答 0