Windows 8 Store Apps Learning (67) Background tasks: Push notifications

Source: Internet
Author: User
Tags valid xmlns

Introduced

Re-imagine Windows 8 Store Apps after a task

Push notification

Example

1. Client

Backgroundtask/pushnotification.xaml

<page x:class= "XamlDemo.BackgroundTask.PushNotification" xmlns= "http://schemas.microsoft.com/winfx/2006/xaml/
    Presentation "xmlns:x=" Http://schemas.microsoft.com/winfx/2006/xaml "xmlns:local=" Using:XamlDemo.BackgroundTask " Xmlns:d= "http://schemas.microsoft.com/expression/blend/2008" xmlns:mc= "http://schemas.openxmlformats.org/ markup-compatibility/2006 "mc:ignorable=" D "> <grid background=" Transparent "> <stackpanel margin= "0 0 0" > <textblock name= "lblmsg" fontsize= "14.667" textwrapping= "Wrap"/&G
                
            T
                
            <button name= "Btncreatechannel" content= "Create the Channel" margin= "0 0 0" click= "Btncreatechannel_click"/> <textbox name= "Txturi" margin= "0 0"/> <image Sour
   Ce= "Wns.png" margin= "0 0 0" horizontalalignment= "left" width= "a"/> </StackPanel> </Grid> </Page> 

Backgroundtask/pushnotification.xaml.cs

* * Demonstrates how to receive push notification * * NOTE: * Need to add background task declaration in package.appxmanifest and check "push notification" * After the application is created in the Win8 store, you need to copy the identity of the app's store to the Package.appxmanifest Identity Node * cannot run in emulator * New channel is valid for 30 days * * Wns-windows Push Notification S
Ervice * Push notification service side see: webserver/pushnotification within the file * * using System;
Using Windows.ApplicationModel.Background;
Using Windows.Networking.PushNotifications;
Using Windows.UI.Notifications;
Using Windows.UI.Popups;
Using Windows.UI.Xaml;
    
Using Windows.UI.Xaml.Controls; Namespace Xamldemo.backgroundtask {public sealed partial class Pushnotification:page {public pushnotif Ication () {this.
        InitializeComponent ();  Private async void Btncreatechannel_click (object sender, RoutedEventArgs e) {//When a push is received Raw notification, if the app is in the lock screen, you can trigger the background task to perform the associated logic (pushnotificationtrigger) backgroundaccessstatus status = Backgroundexecu
           Tionmanager.getaccessstatus (); if (status = = backgroundaccessstatus.unspecified) {status = await Backgroundexecutionmanager .
            Requestaccessasync ();  } if (status = = Backgroundaccessstatus.denied) {await new Messagedialog ("Please first add this app Add to lock Screen ").
                Showasync ();
            Return
            //Create a push notification channel, each new channel is valid for 30 days, so it is recommended to rebuild a channel each time you enter the app (but if you need to pay attention to a shorter interval, you'll be able to reuse the previous channel) Pushnotificationchannel channel = await
            Pushnotificationchannelmanager.createpushnotificationchannelforapplicationasync (); Channel The event that is triggered after the notification is received.
    
            Pushnotificationreceived + = channel_pushnotificationreceived; Channel. Close (); Close Channel//View more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/net///Channel. Expirationtime; Channel expiration time, after which channel is invalidated//channel URI address, and the server pushes notifications to this app via this URI txturi.text = C Hannel. Uri.ToString (); } void Channel_pushnotificationreceived (Pushnotificationchannel sender, Pushnotificationreceivedeventargs arg s) {switch (args). NotificationType) {case Pushnotificationtype.badge://Badge notification BADGEUPDA Temanager.createbadgeupdaterforapplication (). Update (args.
                    Badgenotification);
                Break Case PUSHNOTIFICATIONTYPE.RAW://RAW notification//When the raw notification is received, if the app is in the lock screen, the background task can be triggered to perform the relevant logic (Pushnotificati Ontrigger) String msg = args.
                    Rawnotification.content;
                Break Case Pushnotificationtype.tile://Tile Notice tileupdatemanager.createtileupdaterforapplication (). Update (args.
                    Tilenotification);
                Break Case Pushnotificationtype.toast://Toast Notice toastnotificationmanager.createtoastnotifier (). Show (args.
        Toastnotification);            Break
            Default:break; } args.            
        Cancel = true; }
    }
}

Package.appxmanifest

<?xml version= "1.0" encoding= "Utf-8"?> <package xmlns=
"http://schemas.microsoft.com/appx/2010/" Manifest ">
  <!--The following borrows the identity of" greedy snake "to demonstrate push notification-->
  <!--identity Name=" 10437webabcd.173815756dd78 "publisher=" cn=27514dec-c708-4edb-bfea-f956384483d0 "Version=" 1.0.0.0 "/-->
  " <identity name= "Webabcd.win8.XamlDemo" publisher= "Cn=wanglei" version= "1.0.0.0"  />
</Package>

2. Service End

Webserver/pushnotification/oauthtoken.cs

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.