Before we start let’s have a look at these terms:
Tile – an image which represents mobile application on start screen.
Badge – conveys summary or status information specific to mobile application.
Four types of notification can be identified in Windows Phone 8.1:
A set of API calls that send notifications while your app is running, directly updating the tile or badge, or sending a toast notification (a simple feedback about an operation in a small popup).
Notifications that update tiles and badges regularly at a fixed time interval by polling a cloud service for new content.
A set of API calls that schedule a notification in advance, to update at the time you specify. For example calendar.
Notifications sent from a cloud server, even if your app isn’t running.
Each of this notification types can be sown in Notification Center.
They can be visualized as follows:
Alarm and reminders
In order to receive notifications which are initiated by a server (push notification) we must work with a cloud service. In this example we will build a background tasks to generate notifications locally. This approach has few limitations of Internet and CPU usage (details can be found here http://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh977056.aspx). If we are not able to use cloud services, we’re looking for a different solution. Background tasks can be activated by several types of triggers like: timer, internet status change and so on. If we choose timer as a trigger we can create and run a background task which executes an action every 15 minutes (this is a minimum interval for Windows Phone 8). We created the example of mobile application, which collects information from web server and creates notifications on mobile device using background tasks.
We assume you already have a solution where you developed Windows Phone application. This procedure is to add a background task which then is used for notification purposes.
First we create a new project in existing solution and select Windows Runtime Component.
Select “Windows Phone 8.1” as target in project properties.
Create a new class which gets some information from web server and send notifications to mobile device. The class should be public sealed class and implement NotificationBackgroundTask interface.
We override the run method, which holds the main logic of our background task. This method can be synchronous or asynchronous as well.
my example this method calls Test() method which calls a method in our own WebApi connector class. As the WebApi connector class is not a part of this post you should have a look at highlighted code (yellow) which does the job (creates toast notification).
At the end we call deferral.Complete() to tell the system that task is now completed.
Listing of Test() method:
The listing of the method which creates toast notification.
When the background task project is completed we should be able to use it. We need to create a new reference to our new project in the main application project.
In the App Manifest/Declarations tab of the background task we should add new Declaration type (Background task). Define properties of the task, in this case Timer. In Entry point we provide the background task class name.
After all, we can finally run the task in our application. Here is the method to register the task.
And now we want to tell the system to register the task and execute it every 15 minutes.
That’s all. Our background task is ready. Every 15 minutes the task will check whether there are any messages on the server and if so create notifications to the user. Be aware that the task should not exceed network and CPU limits I mentioned before. Please also note that we can only chose 15, 30, 60 and 120 minutes intervals of the timer, otherwise we get an error. Thank you.