Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I set a weekly repeat push notifications in Unity (C#, currently targeting iOS)?

I am currently in the process of setting up push notifications for our app written in Unity (C#). Code in draft below.

(In summary: I get a user's current time at which he has logged in, and assign that time as the push notification time for its corresponding day of the week. If there are null times in other days of the week (0-6), I assign this time to those as well; otherwise, they are left alone as they have in that case been previously assigned the appropriate time for the day.)

Now, I set up the notification trigger for the day, hour, and minute of the next notification, and set "Repeats" to true. In documentation it states that the notification will be repeated every "defined time period"--so therefore I assume, e.g., if I have set Day to February 6, and hour and minute to 12:34p that this will repeat every February 6th at 12:34p.

What I would like is to have notifications repeat on a weekly basis. This was simple in Xcode because you could set a "Weekday" as opposed to a specific day, as is the case here. Is there any solution to making a notification repeat by day of the week?

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
        };
    }
}
like image 902
Mike Pandolfini Avatar asked Jan 28 '20 16:01

Mike Pandolfini


People also ask

How do I make notifications in unity?

You can do this in the Mobile Notification Settings menu in the Unity Editor (menu: Edit > Project Settings > Mobile Notification Settings) Alternatively, add it manually to the Xcode project, or use the Unity Xcode API.

What is a mobile push notification?

Push notifications look like SMS text messages and mobile alerts, but they only reach users who have installed your app. All the mobile platforms – iOS, Android, Fire OS, Windows and BlackBerry – have their own services for supporting push.


1 Answers

Ok, after reading some docs i think i found what you want. Instead of a iOSNotificationCalendarTrigger() you might want to use iOSNotificationTimeIntervalTrigger() instead. This allows you to pass in a C# TimeSpan. Which you can then just set to 7 days.

Last time i checked that should still work all year round ;)

The Calendar variant is specifically meant for "send this notification every x days, or every x hours" due to it not allowing days, or maybe you could shove 7*24 hours in there, i don't really see the use of that variant all that much. Then again i have seen stranger things inside Unity's stuff.

Let me know if this works out for you!

like image 91
Smileynator Avatar answered Oct 25 '22 23:10

Smileynator