Join Michael Lehman for an in-depth discussion in this video Reminders and notifications, part of Windows Phone SDK Essential Training.
Windows Phone gives you the ability to create scheduled actions for future times that display customized reminders even if your app's not running. For this course, we've create a little app called MoovZ that helps you remember to get up and move around when you're taking one of these courses or in for a long coding session. You can start a series of reminders anytime you want. You can look at the reminders you've already got scheduled, and you can stop the reminders anytime you want. So let's go ahead and take a look at it action, and then we'll take a look at the code. We can see here we've got our app going, up at the top here, we've got the app name MoovZ. We've got the page name, Control panel, we have our two buttons, Start MoovZ and Stop MoovZ.
And we have the section down here at the bottom where the list of moves is going to show up. We go ahead and we tap on Start MoovZ. We get this popup here that says your MoovZ are engaged and after we tap OK. You can see down here we have our lists of times when our notifications are going to appear on our screen. We don't have to be running the MoovZ on for the notifications to show up. Let's just go back to the home screen, and we'll see what happens in a couple minutes when the first MoovZ reminder is ready to come up. And there we can see, on the home screen, we've got a notification here from our app, MoovZ.
That's the thing up here at the top the next thing down is the title of this notification. In our case, MoovZ number one, and down below that you can see when your next move is going to show up. You can simply dismiss this by tapping on the Dismiss button or you can snooze it by tapping on the Snooze 4 and selecting 5 minutes, 10 minutes, an hour, 4 hours, or a day. Those are things defined by the system not by your app. In our case, we just click Dismiss. Our next MoovZ is going to show up in another minute and a half, and we're going to let that one show up.
And then, we're going to let the one after that show up, so that I can show you what happens when you have multiple notifications on the screen at once. (SOUND) Here is our first notification, MoovZ number 2. We're going to wait until the next notification shows up so you can see what happens if you don't get back to the device and dismiss or snooze. (SOUND) And here's our second notification. You notice it says MoovZ number 3. And it tells us when our next Moovz do. Now, it shows us that we have one of two notifications so that if we come down here and we tap Dismiss, you can see the previous one.
Moovz number 2 is still there underneath from Moovz that's not really significant. But for some applications, being able to make sure you see every single notification might be crucial. And that's why they stack up like that. Now let's go look and see how it's built. You've seen MoovZ in action, now let's take a look at how it's built. The UI is very simple, there's simply a Start MoovZ button, a Stop MoovZ button, and a reminders text block, where we put in the list of times that reminders are coming up in the future.
Now, let's go and take a look at the code. First of all, let's look at the Start MoovZ. MoovZ are created using something called the Scheduled Action Service, and every scheduled action has a name. So, what we do is we get the current time, so we've got our root time. We set our ReminderTextBlock to the empty string. And then, this is an arbitrary number, I set it up to have 10 moves. Now, you notice that the four loop goes from one to 11, as opposed to zero to 10, so we want the moves to be numbered one through 11, not zero through 9.
At the beginning here, on lines 51 and 52. What we do is go through and see if we have any existing reminder that is set up with that name. And if there is, we delete it. First up, let's make sure we don't have an existing schedule of action with the same name we're trying to create right now. Because you can come into the app and just hit the Start MoovZ button again in order to create ten new reminders. After we've deleted it, we create a reminder object. And a reminder object can have three things. It has a title, it has a begin time, and it has content.
We set our title equal to moovz, pound sign, and the id number of this particular reminder. We set the begin time equal to the root time, and then we add 60 seconds times our move time in minutes, times the number of this particular reminder. Back at the beginning here, we've set the move time in minutes for the purposes of demo to two. I'd recommend setting this to 15, 20, 30 minutes, however often you want to get up and stretch. Then after we've created the reminder, we also go and fill in the reminder text block.
The one that's at the bottom below the two buttons. In other words, we list the times as you saw of the upcoming reminders. So, this code here simply says if the reminder text block is empty. We put in this, the next time string. If it's not, we concatenate a new line and then our next time string at the end of that. And then finally, we simply call these scheduled action servers Add Method to add our reminder. And when we get done with all ten, we show a message box saying your moves are engaged. If the user clicks the Stop button. All we do is go through our loop of ten again.
Look for the scheduled actions and if we find them, we remove them. And then, we set the reminder text block to that string whose removed. Don't forget your moves. One last thing we did to fill in the reminder text block with the currently pending moves when the app starts. Come back up here, is we set up a page loaded event handler. And in there, we go through and iterate through the scheduled actions. And try to find whatever moves are already scheduled. If we can find that move, then we go and ask, is this move in the past? If it's something that's already happened, we don't put it into the list.
Otherwise, we simply append it to the reminder text block, showing you the list of reminders that are still yet to happen. That's it, that's all there is to MoovZ. And that's all there is to creating reminders in your apps in order to the system to provide notifications, even when your app isn't running.
- Downloading and installing the tools
- Understanding the SDK
- Designing the user experience (UX)
- Implementing commands
- Data binding with Silverlight
- Exploring the Model-View-ViewModel (MVVM) pattern
- Loading external data
- Capturing data from the camera, GPS, compass, and accelerometer
- Recording and playing sound
- Implementing persistence with isolated storage
- Leveraging built-in tasks, choosers, and launchers
- Expanding the available controls using the Silverlight and Coding4Fun toolkits
- Cloning a Windows Phone app
- Learning how to publish your app