Over a million developers have joined DZone.

Windows Phone: Reminder in Depth

· Mobile Zone

In this article I will discuss how to create Reminder in Windows Phone. I will also deep dive in few other aspects of Reminder feature in Windows Phone.

Let's create a simple reminder. I will use timepicker control to set the reminder and activate it. Timepicker is available in Silverlight Windows Phone Toolkit

Download Silverlight Windows Phone Toolkit

Step 1: Create a silverlight for Windows Phone project.

Step 2: Add reference of Microsoft.Phone.Controls.Toolkit.dll
Step 3: Add namespace of Microsoft.Phone.Controls.Toolkit in MainPage.xaml.

xmlns:toolkit="clr- namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"

Step 4: Add timepicker control inside grid of MainPage.xaml. It has timePicker_ValueChanged which will trigger on change of time.

 

<toolkit:TimePicker Header="Start Time" x:Name="timePicker" Margin="0,250,0,0" Width="450" HorizontalAlignment="Left" ValueChanged="timePicker_ValueChanged"/>

Step 5: Add Microsoft.Phone.Scheduler directive.

 

using Microsoft.Phone.Scheduler;

Step 6: In the code behind of MainPage.xaml add timePicker_ValueChanged method.

 

 

private void timePicker_ValueChanged(object sender, EventArgs args)
{
   string reminderName = Guid.NewGuid().ToString();
   Reminder reminder = new Reminder(reminderName);
   reminder.Title = "My Reminder";
   DateTime date = DateTime.Today;
   DateTime beginTime = date + ((DateTime)timePicker.Value).TimeOfDay;
   reminder.BeginTime = beginTime;
   
   //reminder.ExpirationTime = date.AddHours(20);
   
   reminder.RecurrenceType = RecurrenceInterval.Daily;
   ScheduledActionService.Add(reminder);
}

We can use Guid to generate random name of reminder. Begintime of reminder can be set using DateTime.Today and the value selected in timepicker.

We can set RecurrenceType by using RecurrenceInterval enum.

The options of enum are:

 

None = 0 : No recurrence. The notification is launched once at the time specified by Microsoft.Phone.Scheduler.ScheduledAction.BeginTime


Daily = 1: Daily recurrence

Weekly = 2: Weekly recurrence

Monthly = 3: Monthly recurrence

EndOfMonth = 4: Recurring at the end of each month

Yearly = 5: Yearly recurrence

Note: If BeginTime is exceeded by more than 4 hours then Reminder won't be launched.

Step 7: Now wrap the above code which loops for 50 times like below.

private void timePicker_ValueChanged(object sender, EventArgs args)
{
   for (int i = 0; i < 50; i++)
   {
      string reminderName = Guid.NewGuid().ToString();
      Reminder reminder = new Reminder(reminderName);
      reminder.Title = "My Reminder";

      DateTime date = DateTime.Today;
      DateTime beginTime = date + ((DateTime)timePicker.Value).TimeOfDay;
      reminder.BeginTime = beginTime;

      //reminder.ExpirationTime = date.AddHours(20);
      reminder.RecurrenceType = RecurrenceInterval.Daily;
      ScheduledActionService.Add(reminder);
   }
}

Step 8: Uninstall this app from emulator or device if you have already run it.Now run the program again with 50 loops and set the reminder. It will work fine. Now run the emulator again, you will notice that you will get InvalidOperationException was unhandled as shown below.

Windows Phone - Reminder BNS Error

BNS Error: The maximum number of ScheduledActions of this type have already been added.

Step 9: Now uninstall the app with 50 loop and increase the loop count to 51 and run the app again.

You will get the same above error again.

So, conclusion is there is limit of 50 active reminders per application in Windows Phone. Once you add Reminder to ScheduledActionService, it remains in application until you remove it.

As per above the Reminder will stop working once you reach threshold of 50.

To make reminder work N number of times, the previous reminder(s) needs to be removed from ScheduledAction before setting new reminder.

Step 10: Add System.IO.IsolatedStorage directive.

using System.IO.IsolatedStorage;

Step 11: Store the reminder name in the IsolatedStorage. Remove the reminder name from the ScheduledActionSerive before adding new one.

 

IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
string strReminder;
if (settings.TryGetValue<string>("Reminder", out strReminder))
{
   ScheduledActionService.Remove(strReminder);
}

string reminderName = Guid.NewGuid().ToString();
settings["Reminder"] = reminderName;

Now one can set reminder N number of times.

This ends the article of Reminder in Windows Phone.

Topics:

Published at DZone with permission of Sumit Dutta, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}