Working With Alarms/Reminders in Windows Phone 7


Introduction

In this article we are going to how to use the Alarms and Reminders notifications in Windows Phone 7 Application real time. A notification is a message that pops up on the screen to the end users at a specified time scheduled initially. We can make the pop up with some customized message for user friendly requirements. Basically we can make 2 types of notifications an Alarm and a Reminder.

An Alarm is used to play a music file as a notification is launched as scheduled by the end users. A Reminder notification is similar to the Alarm notification expect we can have some text information to show the end user and also we can provide a navigation URI of a page to be opened on clicking of the reminder UI. Also we can include some query strings parameters to pass information that should be used with in the page or the application to which the navigation is pointing to. Microsoft has a limitation of using 50 Alarm or Reminder notifications to be used with in a single application.

Alarm and Reminders uses Alarm Class and Reminder Class respectively, we can see in this article on how to use these classes to create the alarm and reminders that can be used in real time. We will create a new Windows Phone 7 project and provide options to add an alarm and reminder. Let us jump start to see the step by step process on how to do these tasks one by one.

Steps

First let us create an Alarm application, Open Visual Studio 2010 and create a new application as shown in the screen below.

11.png

Now let us add 2 pages, one for adding an alarm content page and other is for adding a reminder content page. Once we added 2 pages in the main page let us add 2 buttons for the user to select to which we can navigate and add the notifications as shown in the screen below.

22.png

We have added 2 pages AddAlarm.XAML and AddReminder.XAML, on the button click event we need to navigate to these 2 pages as shown in the code behind.

XAML code

<Grid x:Name="LayoutRoot" Background="Transparent">

    <Grid.RowDefinitions>

        <RowDefinition Height="Auto"/>

        <RowDefinition Height="*"/>

    </Grid.RowDefinitions>

 

    <!--TitlePanel contains the name of the application and page title-->

    <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">

        <TextBlock x:Name="ApplicationTitle" Text="F5DEBUG WP7 TUTORIALS" Style="{StaticResource PhoneTextNormalStyle}"/>

        <TextBlock x:Name="PageTitle" Text="F5DEBUG" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>

    </StackPanel>

 

    <!--ContentPanel - place additional content here-->

    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">

        <Button Content="Alarm" Height="161" HorizontalAlignment="Left" Margin="65,108,0,0" Name="btnAlarm" VerticalAlignment="Top" Width="330"  Click="btnAlarm_Click"/>

        <Button Content="Reminder" Height="161" HorizontalAlignment="Left" Margin="65,306,0,0" Name="btnReminder" VerticalAlignment="Top" Width="330" Click="btnReminder_Click"/>

    </Grid>

</Grid>

 

C# code

private void btnAlarm_Click(object sender, RoutedEventArgs e)

{

NavigationService.Navigate(new Uri("/AddAlarm.xaml", UriKind.Relative));

}

 

private void btnReminder_Click(object sender, RoutedEventArgs e)

{

NavigationService.Navigate(new Uri("/AddReminder.xaml", UriKind.Relative));

}

ALARM Page

Now we need to add the content to the AddAlarm.XAML page, here to add an alarm we need to make use of the Alarm class which is inherited from the ScheduledNotification class. Now add the controls to the AddAlarm.XAML Page as shown in the screen below.

XAML code

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">

    <TextBlock Height="30" HorizontalAlignment="Left" Margin="26,94,0,0" Name="ttName" Text="Name" VerticalAlignment="Top" />

    <TextBox Height="72" HorizontalAlignment="Left" Margin="126,68,0,0" Name="txtName" Text="" VerticalAlignment="Top" Width="306" />

    <TextBlock Height="30" HorizontalAlignment="Left" Margin="26,172,0,0" Name="ttContent" Text="Content" VerticalAlignment="Top" />

    <TextBox Height="132" HorizontalAlignment="Left" Margin="126,146,0,0" Name="txtContent" Text="" VerticalAlignment="Top" Width="306" />

    <TextBlock Height="30" HorizontalAlignment="Left" Margin="26,310,0,0" Name="ttBegintime" Text="Begin Time" VerticalAlignment="Top" />

    <toolkit:DatePicker Name="datePicker1" Height="67" Margin="126,286,145,254" />

    <Button Content="Add" Height="72" HorizontalAlignment="Left" Margin="54,470,0,0" Name="btnAdd" VerticalAlignment="Top" Width="160" Click="btnAdd_Click" />

    <Button Content="Clear" Height="72" HorizontalAlignment="Left" Margin="220,470,0,0" Name="btnClear" VerticalAlignment="Top" Width="160" Click="btnClear_Click" />

    <toolkit:TimePicker HorizontalAlignment="Left" Margin="317,286,0,0" Name="timePicker1" VerticalAlignment="Top" Width="115" />

    <TextBlock Height="30" HorizontalAlignment="Left" Margin="26,383,0,0" Name="textBlock1" Text="Expiration" VerticalAlignment="Top" />

    <toolkit:DatePicker Height="67" Margin="126,359,145,181" Name="datePicker2" />

    <toolkit:TimePicker HorizontalAlignment="Left" Margin="317,359,0,0" Name="timePicker2" VerticalAlignment="Top" Width="115" />

</Grid>

 
3.gif


C# code

private void btnAdd_Click(object sender, RoutedEventArgs e)

{

AddAlaram();

}

 

void AddAlaram()

{

Alarm alarm = new Alarm(txtName.Text.ToString());

alarm.Content = txtContent.ToString();

DateTime date = (DateTime)datePicker1.Value;

DateTime time = (DateTime)timePicker1.Value;

DateTime beginTime = date + time.TimeOfDay;

alarm.BeginTime = beginTime;

DateTime date1 = (DateTime)datePicker1.Value;

DateTime time1 = (DateTime)timePicker1.Value;

DateTime exptime = date1 + time1.TimeOfDay;

alarm.ExpirationTime = exptime;

alarm.RecurrenceType = RecurrenceInterval.Daily; 

ScheduledActionService.Add(alarm);

}

In the above code we have some properties which are to be considered before we write our requirement on top of it. The properties are as follows

  • Name – Unique Name for the Alarm
  • Title – This is default name, and we cannot manually set this property
  • Content – This property is to set some content for the alarm.
  • Begin Time – Beginning of the alarm
  • Expiration Time – Expiration time of the alarm
  • Recurrence Type – Recurrence type of the alarm
  • Sound – Sound file to play for the alarm.

Now we run the application and we can see the alarm configuration as shown in the screen below.

4.gif

5.gif

REMINDER Page

Now let us start with designing the Reminder page, before that we will add a new page called ReminderApp. The use of this page is on reminder trigger we will have option to navigate to a URI with passing some parameter. We will pass values to the pages from the Reminder page, we will see on how to do that with this page. Add contents from the tool box for the Reminder page as shown in the screen below and the code.

XAML code

Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"><TextBlock Height="30" HorizontalAlignment="Left" Margin="21,32,0,0" Name="ttName" Text="Name" VerticalAlignment="Top" />

<TextBox Height="72" HorizontalAlignment="Left" Margin="121,6,0,0" Name="txtName" Text="" VerticalAlignment="Top" Width="306" />

<TextBlock Height="30" HorizontalAlignment="Left" Margin="21,178,0,0" Name="ttContent" Text="Content" VerticalAlignment="Top" />

<TextBox Height="132" HorizontalAlignment="Left" Margin="121,152,0,0" Name="txtContent" Text="" VerticalAlignment="Top" Width="306" />

<TextBlock Height="30" HorizontalAlignment="Left" Margin="21,316,0,0" Name="ttBegintime" Text="Begin Time" VerticalAlignment="Top" />

<toolkit:DatePicker Height="67" Margin="121,292,150,248" Name="datePicker1" />

<Button Content="Add" Height="72" HorizontalAlignment="Left" Margin="49,476,0,0" Name="btnAdd" VerticalAlignment="Top" Width="160" />

<Button Content="Clear" Height="72" HorizontalAlignment="Left" Margin="215,476,0,0" Name="btnClear" VerticalAlignment="Top" Width="160" />

<toolkit:TimePicker HorizontalAlignment="Left" Margin="312,292,0,0" Name="timePicker1" VerticalAlignment="Top" Width="115" />

<TextBlock Height="30" HorizontalAlignment="Left" Margin="21,389,0,0" Name="textBlock1" Text="Expiration" VerticalAlignment="Top" />

<toolkit:DatePicker Height="67" Margin="121,365,150,175" Name="datePicker2" />

<toolkit:TimePicker HorizontalAlignment="Left" Margin="312,365,0,0" Name="timePicker2" VerticalAlignment="Top" Width="115" />

<TextBlock Height="30" HorizontalAlignment="Left" Margin="21,100,0,0" Name="ttTitle" Text="Title" VerticalAlignment="Top" />

<TextBox Height="72" HorizontalAlignment="Left" Margin="121,74,0,0" Name="txtTitle" Text="" VerticalAlignment="Top" Width="306" />

</Grid>

 6.gif

C# code

private void btnAdd_Click(object sender, RoutedEventArgs e)

{

AddAlaram();

}

void AddAlaram()

{

Alarm alarm = new Alarm(txtName.Text.ToString());

alarm.Content = txtContent.ToString();

DateTime date = (DateTime)datePicker1.Value;

DateTime time = (DateTime)timePicker1.Value;

DateTime beginTime = date + time.TimeOfDay;

alarm.BeginTime = beginTime;

DateTime date1 = (DateTime)datePicker1.Value;

DateTime time1 = (DateTime)timePicker1.Value;

DateTime exptime = date1 + time1.TimeOfDay;

alarm.ExpirationTime = exptime;

alarm.RecurrenceType = RecurrenceInterval.Daily;

ScheduledActionService.Add(alarm);

}

In the above code we have some properties which are to be considered before we write our requirement on top of the reminder properties. Some of the main properties are as follows

  • Name - Unique Name for the Reminder
  • Title - This is default name, and we cannot manually set this property
  • Content -This property is to set some content for the reminder.
  • Begin Time - Beginning of the Reminder
  • Expiration Time -Expiration time of the Reminder
  • Recurrence Type - Recurrence type of the Reminder
  • Navigation URI -On clicking of the application will navigate the user to the new page.

In the above code, if we see we have added a parameter to be passed with the navigation URI as query string. To get the query string value we need to write the below code in the new page (ReminderAppPage.XAML) as shown in the screen below.


C# code

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)

{

base.OnNavigatedTo(e);

string paramValue = "";

NavigationContext.QueryString.TryGetValue("param", out paramValue);

textBlock1.Text = paramValue;

}

 

Now we run the application and we can see the Reminder configuration as shown in the screen below.

7.gif

Conclusion

So in this article we have seen how to make use of the inbuilt Scheduled Notification mechanism to build applications which notifies to the end users with some custom messages. Alarm and Reminders plays some major role for notifications to the end users as per the requirements. 


Similar Articles