The Stock Quoter app uses all three types of push notifications, which are described in the following sections.
Tile notifications are sent periodically from the StockQuoteService
and are displayed as shown in Figure 15.7. The tile notification contains a Text1
data property equal to Stock Quote and a Text2
data property that is the result of calling the ToString
method of the StockQuote
object, which produces a combination of the stock symbol, the stock’s last price, and its change in price.
Toast notifications are sent periodically from the StockQuoteService
and are displayed as shown in Figure 15.4. The sample uses the title property of the tile notification to display the stock symbol, its price, and its change in price for the day. The count property is populated with an incrementing value, which is just for demonstration purposes.
For tile and raw notifications, the sample makes use of three images: StockUp.png, StockNone.png, and StockDown.png, located in the PushNotification/Images directory of the WPUnleashed.Examples project.
The logic for determining the background image for a tile notification is located server-side, as this excerpt from the StockQuoteService
shows:
string background;
if (stockQuote.ChangeInPrice > 0)
{
background = "PushNotification/Images/StockUp.png";
}
else if (stockQuote.ChangeInPrice < 0)
{
background = "PushNotification/Images/StockDown.png";
}
else
{
background = "PushNotification/Images/StockNone.png";
}
Image paths are relative to the application project.
Raw notifications make use of the serialized StockQuote
instance, as the following excerpt from the StockQuoteService
class shows:
notifier.SendRawNotification(
stockQuote, subscriptionUrl, PushNotificationPriority.RealTime);
The StockQuote
instance is serialized to a byte
array, which is then used as the content for the raw notification. For more information on the serialization process, see the previous section “Receiving a Raw Notification.”
When a raw notification is received by the phone app and the StockQuote
object is rehydrated in the PushNotificationViewModel
class, the object is assigned to the viewmodel’s StockQuote
property.
The view displays the stock information via the viewmodel’s StockQuote
property, as shown in the following excerpt from the PushNotificationView.xaml file (style attributes have been removed for clarity):
<TextBlock Text="Price:" />
<TextBlock Text="{Binding StockQuote.LastPrice}" />
<TextBlock Text="Change:" />
<TextBlock Text="{Binding StockQuote.ChangeInPrice}" />
The stock direction image is presented using the viewmodel’s StockPriceDirection
property. When the StockQuote
changes in the viewmodel, the StockPriceDirection
property is reevaluated and a different image displayed in the view. The following excerpt shows the StockQuote
property in the viewmodel:
StockQuote stockQuote;
public StockQuote StockQuote
{
get
{
return stockQuote;
}
set
{
Assign(ref stockQuote, value);
/* Set the direction so that the view
* doesn't need to do any evaluation. */
if (stockQuote == null || stockQuote.ChangeInPrice == 0)
{
StockPriceDirection = StockPriceDirection.None;
}
else if (stockQuote.ChangeInPrice > 0)
{
StockPriceDirection = StockPriceDirection.Up;
}
else
{
StockPriceDirection = StockPriceDirection.Down;
}
}
}
The view itself subscribes to the viewmodel’s PropertyChanged
event. When the StockPriceDirection
property changes, it determines what image should be displayed, depending on its value.
void viewModel_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == "StockPriceDirection")
{
string url;
switch (ViewModel.StockPriceDirection)
{
case StockPriceDirection.Down:
url = "Images/StockDown.png";
break;
case StockPriceDirection.Up:
url = "Images/StockUp.png";
break;
default:
url = "Images/StockNone.png";
break;
}
Image_StockQuote.Source = new BitmapImage(
new Uri(url, UriKind.Relative));
}
}
Updating the UI based on a viewmodel property could be better orchestrated using the page’s VisualStateManager
. In Chapter 18, “Incorporating Map-Based Positioning,” we discuss a custom VisualStateUtility
class that allows you to bind the visual state of a UI element to a viewmodel property.
The Stock Quoter application allows a user to unregister from receiving stock updates. By clicking the Unregister button, the viewmodel’s UnregisterPushNotification
method is called, which calls the PushNotificationSubscriber
object’s Unsubscribe
method, as shown:
public void UnregisterForPushNotification()
{
try
{
Message = "Unregistering for notifications.";
if (subscriber != null)
{
subscriber.Unsubscribe();
subscriber.Dispose();
subscriber = null;
}
Message = "Unregistered for notifications.";
}
catch (Exception ex)
{
Message = "Problem unregistering: " + ex.ToString();
}
if (channelUri != null)
{
try
{
UnregisterWithServer(channelUri.ToString());
}
catch (Exception ex)
{
Message = "Problem unregistering with server: " + ex.ToString();
}
}
}
When the PushNotificationSubscriber
is disposed, its Timer
and HttpNotificationChannel
are also disposed. The cloud service is then notified that push notifications should no longer be sent.
18.118.254.28