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
//Create a reminder Reminder myReminder = new Reminder("buy milk"); myReminder.Title = "Buy Milk"; myReminder.Content = "Don't forget to buy milk!"; myReminder.BeginTime = DateTime.Now.AddSeconds(10); myReminder.ExpirationTime = DateTime.Now.AddSeconds(15); myReminder.RecurrenceType = RecurrenceInterval.None; myReminder.NavigationUri = new Uri("/MainPage.xaml?fromReminder=1", UriKind.Relative); //Add the reminder to the ScheduledActionService ScheduledActionService.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.