Different Windows Phone (10)

Source: Internet
Author: User
Tags baseuri bind tostring xmlns


Push Notification (push notification) Tile notification, push custom information



Introduced



Special push notification for Windows Phone 7.5 (SDK 7.1)



Push Tile Notification



Push Custom Information



Example



1, push to send Tile notice



Client



Pushtile.xaml


<phone:PhoneApplicationPage 

 
    x:Class="Demo.PushNotification.PushTile"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"
    mc:Ignorable="d" d:DesignHeight="768" d:DesignWidth="480"
    shell:SystemTray.IsVisible="True"> 
   
    <StackPanel Orientation="Vertical"> 
        <Button Name="btnRegister" Content="Get Channel Uri" Click="btnRegister_Click" /> 
        <TextBox Name="txtUrl" /> 
        <TextBlock Name="lblMsg" TextWrapping="Wrap" /> 
    </StackPanel> 
       
</phone:PhoneApplicationPage>


PushTile.xaml.cs


/ *
 * Demo push notification
 *
 * HttpNotificationChannel-Push notification channel
 * HttpNotificationChannel.Find (string channelName)-find and return channel, an app can only have one channel
 *
 * ChannelUri-Channel URI for push notifications
 * ChannelName-the name of the channel
 * ConnectionStatus-the connection status of the channel, either ChannelConnectionStatus.Connected or ChannelConnectionStatus.Disconnected
 *
 * ChannelUriUpdated-event triggered when the channel URI is obtained
 * ErrorOccurred-event triggered when an exception occurs
 * ConnectionStatusChanged-event triggered when the connection status changes
 * ShellToastNotificationReceived-If Toast is received during the program running, it will not be displayed, but this event will be triggered.
 * What is running: the program is displayed in the foreground, and HttpNotificationChannel.Find (channelName) is called (create one if not), and the channel is in the connected state
 * HttpNotificationReceived-event triggered when a custom message notification is received (this message can only be received while the program is running)
 *
 * Open ()-open channel
 * Close ()-close the channel
 *
 * BindToShellToast ()-Bind this channel to Toast notifications
 * BindToShellTile ()-Bind this channel to Tile notifications, only local resources can be referenced
 * BindToShellTile (Collection <Uri> baseUri)-Bind this channel to the Tile notification, allowing remote resources to be referenced (when using remote images, it cannot be https, it must be less than 80KB, and it must be downloaded within 30 seconds)
 * baseUri-collection of domain names for remote resources allowed (max 256 characters)
 * IsShellToastBound-whether this channel is bound to Toast notifications
 * IsShellTileBound-Whether this channel is bound to Tile notifications
 * /
   
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
   
using Microsoft.Phone.Notification;
using System.Text;
using System.Diagnostics;
using System.Collections.ObjectModel;
   
namespace Demo.PushNotification
{
    public partial class PushTile: PhoneApplicationPage
    {
        public PushTile ()
        {
            InitializeComponent ();
        }
   
        private void btnRegister_Click (object sender, RoutedEventArgs e)
        {
            // find the specified channel in the current application
            string channelName = "myChannel";
            HttpNotificationChannel channel = HttpNotificationChannel.Find (channelName);
   
            // Set of domain names of remote resources allowed to be referenced
            Collection <Uri> allowDomains = new Collection <Uri> ();
            allowDomains.Add (new Uri ("http://www.cnblogs.com/"));
            allowDomains.Add (new Uri ("http://images.cnblogs.com/"));
   
            if (channel == null) // create a channel if not found
            {
                channel = new HttpNotificationChannel (channelName);
   
                channel.ChannelUriUpdated + = new EventHandler <NotificationChannelUriEventArgs> (channel_ChannelUriUpdated);
                channel.ErrorOccurred + = new EventHandler <NotificationChannelErrorEventArgs> (channel_ErrorOccurred);
   
                channel.Open ();
   
                channel.BindToShellTile (allowDomains);
            }
            else // already exists then use this existing channel
            {
                channel.ChannelUriUpdated + = new EventHandler <NotificationChannelUriEventArgs> (channel_ChannelUriUpdated);
                channel.ErrorOccurred + = new EventHandler <NotificationChannelErrorEventArgs> (channel_ErrorOccurred);
   
                if (channel.ConnectionStatus == ChannelConnectionStatus.Disconnected)
                    channel.Open ();
                if (! channel.IsShellTileBound)
                    channel.BindToShellTile (allowDomains);
   
                // Get notification uri
                txtUrl.Text = channel.ChannelUri.ToString ();
                Debug.WriteLine (channel.ChannelUri.ToString ());
            }
        }
   
        void channel_ChannelUriUpdated (object sender, NotificationChannelUriEventArgs e)
        {
            Dispatcher.BeginInvoke (() =>
            {
                // Get notification uri
                txtUrl.Text = e.ChannelUri.ToString ();
                Debug.WriteLine (e.ChannelUri.ToString ());
            });
        }
   
        void channel_ErrorOccurred (object sender, NotificationChannelErrorEventArgs e)
        {
            Dispatcher.BeginInvoke (() => MessageBox.Show (e.Message));
        }
    }
} 



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.