When you’re creating your application, you
might need the ability to alert the user about specific notifications.
Windows Phone supports this by enabling you to create two types of
notifications: alarms and reminders.
Alarms are notifications that
show a message and allow the user to tap the alarm to start your
application. Additionally, the user can snooze the alarm or dismiss it.
With an alarm, you can also specify a custom sound located in your application to use as the alarm sound. You can see an alarm in action in Figure 1.
FIGURE 1 An alarm
Reminders are similar to alarms, but they have additional functionality:
• You can specify the title of a reminder (which is always “Alarm” for alarms).
• The user can specify how long to snooze a reminder.
• You
can decide to send contextual information when the user taps a reminder
(whereas the alarm can simply launch the application).
• You cannot specify a specialized sound for reminders.
Figure 2
shows a reminder. If alarms or reminders are not closed when additional
notifications appear, they will be stacked with an identifier that
mentions how many notifications need to be handled, as shown in Figure 3.
FIGURE 2 A reminder
FIGURE 3 Stacked notifications
Alarms
and reminders are both tied to your application. They are notifications
for your application. If you want to allow users to disable/enable
these notifications, you must supply that functionality in your
application. There is no operating system-level management UI for these
notifications.
Tip
The precision of alarm and reminder times is
to the minute. If you create notifications that need higher precision,
the service that handles alarms and reminders will not show the
notification more than after per minute. Be prepared for the alarms that
are within a minute of a notification to be stacked.
Now that you’ve seen how notifications look, let’s discuss how you actually create your own notifications.
Creating an Alarm
To create a new alarm, you can simply create a new Alarm
object and instantiate all the relevant properties, like so:
// Create Alarm - name must be unique per app
var alarmName = Guid.NewGuid().ToString();
var alarm = new Alarm(alarmName)
{
// When the Alarm should sound
BeginTime = DateTime.Parse("2011-12-25T06:30"),
// The Message in the Alarm
Content = "Wake Up for Christmas!",
// What sound to play for the alarm
Sound = new Uri("santa.wav", UriKind.Relative)
};
// Add the Alarm to the Phone
ScheduledActionService.Add(alarm);
Each alarm must have a name
that is unique to your application (that is, names do not need to be
globally unique). So, it is common to just use a GUID to name your
alarm. This name is not visible to the user. After you construct the
alarm using the unique name, you would typically specify several key
properties, including the following:
• BeginTime: This is the time the alarm will alert the user.
• Content: This is the text to be shown in the alarm beneath the “Alarm” title.
• Sound: This is a URI to the sound in your .xap file that should be used to play when the alarm occurs.
Finally, you will use the ScheduledActionService
to add the alarm to the phone. This service is the starting point for both alarms and reminders (as they are both considered a ScheduledAction
). You can simply add your alarm to the phone by using the service class as shown in the example.
You can also create alarms to have recurrence. To do this, you should set both the RecurrenceType
and the ExpirationTime
. The RecurrenceType
is an enumeration that enables you to specify whether the alarm is hourly, daily, weekly, and so forth. The ExpirationTime
specifies how long to repeat the recurrence:
// ...
// Set Recurrence
alarm.RecurrenceType = RecurrenceInterval.Yearly;
alarm.ExpirationTime = DateTime.Today.AddYears(10);
// ...
The Alarm
class has a Title
property that looks like you can set the title of your alarm, but this is not supported (that is, it throws a NotSupportedException
). The title of all alarms is “Alarm.”
Creating a Reminder
Because alarms and reminders both derive from ScheduledAction
, you can correctly assume that creating a reminder is similar to creating an alarm:
// Create Reminder - name must be unique per app
var reminderName = Guid.NewGuid().ToString();
var reminder = new Reminder(reminderName)
{
// Reminder Time
BeginTime = DateTime.Today.AddHours(6),
// Title for the Reminder
Title = "Wake Up Reminder",
// The description below the Title
Content = "You should get up now...",
// The page to navigate to (and any other context)
// when the user taps the reminder
NavigationUri =
new Uri("/MainPage.xaml?wake=true", UriKind.Relative),
};
// Add the reminder
ScheduledActionService.Add(reminder);
However, creating a reminder is different from creating an alarm, in several ways. First, the Title
property is supported, so you can specify what the title is on your reminder. Unfortunately, unlike the alarm, the Sound
property is missing, so reminders can only have the sound that the
system defines as the reminder sound (although the user can set that in
the phone’s Options section).
The biggest difference between an alarm and a reminder is that you can specify the NavigationUri
to specify the deep linking of the reminder to a specific page (with context). The NavigationUri
allows you to specify a relative URL of the page to show in your
applications as well as an optional query string to include context to
the reminder. If the user taps on the reminder, it will open your app
and navigate to the URI you’ve specified.
Accessing Existing Notifications
After you’ve added notifications to the phone, you might want to support managing those notifications. The ScheduledActionService
class has several members that can help you manage your notifications:
• Add: This adds a new notification to the phone.
• Find: This locates a notification by name.
• GetActions<T>: This returns a read-only collection of notifications based on the type (alarm or reminder).
• Remove: This removes a specifically named notification from the phone.
• Replace: This replaces a named notification with a new notification (of the same name).
For example, to get a list of reminders and bind them to a list box, you can simply use the GetActions
method:
// Returns an IEnumerable<Reminder> object
var reminders = ScheduledActionService.GetActions<Reminder>();
// A ListBox named 'theBox'
theBox.ItemsSource = reminders;
The base class for the notifications (ScheduledAction
) supports a read-only property called IsScheduled
,
which is used to tell you whether the alarm or reminder is still
scheduled to show a notification in the future. Although the class
supports the IsEnabled
property, it is always true
for alarms and reminders.