Floating FB popout byF5debug

Learn Windows Phone 7 Development in 31 Days – Day 5 – Working with Alarms/Reminders in WP7

 

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.     [more]

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.

clip_image002

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.

clip_image004

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:

01

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

02

<Grid.RowDefinitions>

03

<RowDefinition Height="Auto"/>

04

<RowDefinition Height="*"/>

05

</Grid.RowDefinitions>

06

 

07

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

08

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

09

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

10

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

11

</StackPanel>

12

 

13

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

14

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

15

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

16

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

17

</Grid>

18

</Grid>


C# Code:

1

private void btnAlarm_Click(object sender, RoutedEventArgs e)

2

{

3

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

4

}

5

 

6

private void btnReminder_Click(object sender, RoutedEventArgs e)

7

{

8

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

9

}

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:

01

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

02

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

03

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

04

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

05

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

06

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

07

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

08

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

09

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

10

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

11

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

12

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

13

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

14

</Grid>

clip_image006


C# Code:

01

private void btnAdd_Click(object sender, RoutedEventArgs e)

02

{

03

AddAlaram();

04

}

05

 

06

void AddAlaram()

07

{

08

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

09

alarm.Content = txtContent.ToString();

10

 

11

DateTime date = (DateTime)datePicker1.Value;

12

DateTime time = (DateTime)timePicker1.Value;

13

DateTime beginTime = date + time.TimeOfDay;

14

alarm.BeginTime = beginTime;

15

 

16

DateTime date1 = (DateTime)datePicker1.Value;

17

DateTime time1 = (DateTime)timePicker1.Value;

18

DateTime exptime = date1 + time1.TimeOfDay;

19

alarm.ExpirationTime = exptime;

20

 

21

alarm.RecurrenceType = RecurrenceInterval.Daily;

22

 

23

ScheduledActionService.Add(alarm);

24

}

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.

clip_image008

clip_image010

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:

01

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

02

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

03

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

04

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

05

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

06

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

07

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

08

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

09

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

10

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

11

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

12

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

13

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

14

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

15

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

16

</Grid>

clip_image012

C# Code:

01

private void btnAdd_Click(object sender, RoutedEventArgs e)

02

{

03

AddReminder();

04

}

05

 

06

void AddReminder()

07

{

08

Reminder reminder = new Reminder(txtName.Text.ToString());

09

 

10

reminder.Title = txtTitle.Text.ToString();

11

reminder.Content = txtContent.Text.ToString();

12

 

13

DateTime date = (DateTime)datePicker1.Value;

14

DateTime time = (DateTime)timePicker1.Value;

15

DateTime beginTime = date + time.TimeOfDay;

16

reminder.BeginTime = beginTime;

17

 

18

DateTime date1 = (DateTime)datePicker1.Value;

19

DateTime time1 = (DateTime)timePicker1.Value;

20

DateTime exptime = date1 + time1.TimeOfDay;

21

// reminder.ExpirationTime = exptime;

22

 

23

reminder.RecurrenceType = RecurrenceInterval.Daily;

24

 

25

string struri = "?param=Reminder App Passed";

26

Uri navigationUri = new Uri("/ReminderAppPage.xaml" + struri, UriKind.Relative);

27

reminder.NavigationUri = navigationUri;

28

 

29

ScheduledActionService.Add(reminder);

30

}

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:

1

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

2

{

3

base.OnNavigatedTo(e);

4

 

5

string paramValue = "";

6

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

7

 

8

textBlock1.Text = paramValue;

9

}

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

clip_image014

clip_image016

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.

About Author: Karthikeyan Anbarasan, Microsoft MVP (Most Valuable Professional) in ASP.NET/IIS Architecture. He is the Founder and Chief Editor of the www.f5debug.net website and has authored books on Windows Phone and Business Intelligence(SSIS). He is also a Passionate Speaker and a Blogger on Microsoft Technologies.

You can Join Me On: Facebook, Twitter, Google Plus, LinkedIn

Comments (1) -

Pravin
Pravin
9/24/2012 12:45:37 AM #

using System.Windows.Controls.Toolkit

in the above code.. toolkit:TimePicker is not working..  
i tried installing 7.1.1 update also.. still not working.

is there anyother assembly reference we need to add here ?
plz help.
thank you!

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading

Recent Posts

Random Posts

Most Viewed

URL Shortner

Create your own short urls!!!

Site Counter

free counters

Disclaimer

This is a personal weblog. The opinions expressed here represent my own and not those of my employer. For accuracy and official reference refer to MSDN/ TechNet. I have documented my personal experience on this blog.

Protected by Copyscape Web Plagiarism Finder

Site Meter