如何在 Unity(C#,目前针对 iOS)中设置每周重复推送通知?

Mik*_*ini 6 c# datetime unity-game-engine push-notification ios

我目前正在为我们用 Unity (C#) 编写的应用程序设置推送通知。下面草稿中的代码。

(总而言之:我获取用户当前登录时间,并将该时间指定为其对应的一周中的某天的推送通知时间。如果一周中的其他天(0-6)有空时间,我也将这个时间分配给那些人;否则,他们会被单独留下,因为在这种情况下,他们之前已经被分配了当天的适当时间。)

现在,我为下一个通知的天、小时和分钟设置了通知触发器,并将“重复”设置为 true。在文档中,它指出通知将在每个“定义的时间段”重复 - 因此我假设,例如,如果我将日期设置为 2 月 6 日,并将小时和分钟设置为 12:34p,这将在每年 2 月 6 日重复下午 12:34。

我想要的是每周重复一次通知。这在 Xcode 中很简单,因为您可以设置“工作日”而不是特定的一天,就像这里的情况。是否有任何解决方案可以在一周中的某天重复通知?

private void IOSNotificationManager()
{
    // determine whether user has already allowed or disallowed notifications--won't run again if user has already made decision
    StartCoroutine(RequestAuthorization());

    // Schedule daily notification for user based on time of play
    // iOS uses local time, while Android uses UTC
    DateTime userTime = DateTime.Now;

    // Set a reminder for this specific day of the week (0 = Sunday, 6 = Saturday).
    // Note that this will overwrite any previous time set for this day.
    GameData.PushNotificationTimes[(int)userTime.DayOfWeek] = userTime;

    // Schedule the week of push notifications for days that haven't already been scheduled
    for (var i = 0; i < 7; i++)
    {
        if (GameData.PushNotificationTimes[i] == null)
        {
            // get the number of days after which the notification should occur
            int daysToNotification = (i - (int)userTime.DayOfWeek + 7) % 7;
            DateTime nextDay = userTime.AddDays(daysToNotification);

            GameData.PushNotificationTimes[i] = nextDay;
        }

        Debug.Log("The push notification time scheduled for day " + i + " is " + GameData.PushNotificationTimes[i]);
    }

    for (var i = 0; i < 7; i++)
    {
        DateTime pushNotificationTime = GameData.PushNotificationTimes[i];

        var calendarTrigger = new iOSNotificationCalendarTrigger()
        {
            Day = pushNotificationTime.Day,
            Hour = pushNotificationTime.Hour,
            Minute = pushNotificationTime.Minute,
            // Indicate whether the notification is repeated every defined time period.
            // For instance if hour and minute fields are set the notification will be triggered every day at the specified hour and minute.
            Repeats = true
        };
    }
}
Run Code Online (Sandbox Code Playgroud)

Smi*_*tor 3

好的,在阅读了一些文档后,我想我找到了你想要的。iOSNotificationCalendarTrigger()您可能想使用而不是 a iOSNotificationTimeIntervalTrigger()。这允许您传入 C# TimeSpan。然后您可以将其设置为 7 天。

上次我检查过,应该全年都可以使用;)

日历变体专门用于“每 x 天或每 x 小时发送此通知”,因为它不允许几天,或者也许您可以在其中塞入 7*24 小时,我真的没有看到该变体的用途就这么多。然后我又在 Unity 的东西里看到了奇怪的东西。

让我知道这是否适合您!