In my previous post I talked about some of the new multitasking features available in Windows Phone Mango. In this article, I’ll go over Scheduled Notifications in a bit more detail.
You will find all API details in the Microsoft.Phone.Scheduler namespace and the main items we will look at are
- Reminder – main class to create a reminder
- Alarm – main class to create an alarm
- ScheduledActionService – allows you to manage scheduled actions
A Scheduled Notification is fairly straight forward and essentially will popup a dialog box similar to the calendar reminder in the OS.
There are two types of Scheduled Notifications you can use and these are Alarm and Reminder. This is a good way to integrate with the native OS and not have to build basic pop ups but leverage what is already there.
I’m not going to go through an entire code walk through as there is a good example on MSDN on how to create reminders for Windows Phone. Here is a quick sample on creating a reminder
<span style="color: green;">//Create a reminder
</span><span style="color: #2b91af;">Reminder </span>myReminder = <span style="color: blue;">new </span><span style="color: #2b91af;">Reminder</span>(<span style="color: #a31515;">"buy milk"</span>);
myReminder.Title = <span style="color: #a31515;">"Buy Milk"</span>;
myReminder.Content = <span style="color: #a31515;">"Don't forget to buy milk!"</span>;
myReminder.BeginTime = <span style="color: #2b91af;">DateTime</span>.Now.AddSeconds(10);
myReminder.ExpirationTime = <span style="color: #2b91af;">DateTime</span>.Now.AddSeconds(15);
myReminder.RecurrenceType = <span style="color: #2b91af;">RecurrenceInterval</span>.None;
myReminder.NavigationUri = <span style="color: blue;">new </span><span style="color: #2b91af;">Uri</span>(<span style="color: #a31515;">"/MainPage.xaml?fromReminder=1"</span>, <span style="color: #2b91af;">UriKind</span>.Relative);
<span style="color: green;">//Add the reminder to the ScheduledActionService
</span><span style="color: #2b91af;">ScheduledActionService</span>.Add(myReminder);
Running this code on the emulator will result in the following
Not overly complex but a few key things to note
- You can specify a NavigationUri which will be used if the user launches your app from the reminder. This way you know if you came from the reminder and can show the specific reminder that was clicked
- Don’t try to add a reminder with the same name, you will get an exception saying that the reminder already exists
Creating an Alarm is almost the same as creating a Reminder with a few slight differences.
//Create an alarm Alarm alarm = new Alarm(“Test Alarm”); alarm.Title = “My Alarm Title”; alarm.Content = “My Test alarm”; alarm.BeginTime = DateTime.Now.AddSeconds(10); alarm.ExpirationTime = DateTime.Now.AddSeconds(15); alarm.RecurrenceType = RecurrenceInterval.None; alarm.Sound = new Uri(“”);
//Add the reminder to the ScheduledActionService
Again, pretty straight forward but a few key things to note with alarms are
- A user cannot click from the alarm to your application so you have no NavigationUri property
- You can specify a custom sound to play using the Sound property but the sound file must be included in your XAP and cannot be played from isolated storage.
Scheduled notifications I think will be useful in some application scenarios and it’s great that we as developers can offset some of the ‘reminder code’ to the API/OS to handle for us. This is just one of the ways Windows Phone now multitasks and I’ll follow up with the others in upcoming posts.