在某些情况下Tile 推送不足以满足我们的需求,比如我们需要提醒用户未来3小时内将有大雨,亦或是某航班延迟等 我们需要醒目并且主动的告诉用户

image

如果我们需要使用toast 推送必须在配置文件中开启

image

image

 

但是用户还可以手动关闭推送,这种情况我们就无能为力了 ,所以在开启推送前需要检测下 是否允许推送

      ToastNotifier toastNotifier;
        private void GetTileUpdate()
        {
            TileNotifier = TileUpdateManager.CreateTileUpdaterForApplication();
            if (TileNotifier.Setting != NotificationSetting.Enabled)
            {
                var dialog = new MessageDialog("请开启推送通知");
                dialog.ShowAsync();
            }
        }
 

Toast 的本质和我们前面提到的tile 一样通过封装好的Xml 来执行 ,只是表象不同,所以两者的代码很相似

不过显著的区别有几点

1. toast 的推送最多同时显示3个 但是个数不限,前一个显示完毕后 后面的会补充上来

2. toast 推送到的时候可以显示声音

   see more http://msdn.microsoft.com/en-us/library/br230842.aspx

  1.简单toast

            var template = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText01);
            var tileAttributes = template.GetElementsByTagName("text")[0];
            tileAttributes.AppendChild(template.CreateTextNode("That's   tile test 1  !"));
            var notifications1 = new ToastNotification(template);
            toastNotifier.Show(notifications1);

   同样 如果对xml 结构很了解可以直接用xml

            var xml = @"<toast>
    <visual lang='en-US'>
        <binding template='ToastText01'>
            <text id='1'>test  2</text>
        </binding>  
    </visual>
</toast>";
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(xml);
            var notifications = new ToastNotification(xmlDoc);
            toastNotifier.Show(notifications);

2短代码是等效的

 

2.定时toast 和取消

 

    var template = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText01);
            var tileAttributes = template.GetElementsByTagName("text")[0];
            tileAttributes.AppendChild(template.CreateTextNode("That's   tile shedule toast notifications !"));
            var data = DateTimeOffset.Now.AddSeconds(2);
            var stn = new ScheduledToastNotification(template, data);
            toastNotifier.AddToSchedule(stn);
你需要注意 这里的时间类型是 
DateTimeOffset
   IReadOnlyList<ScheduledToastNotification> scheduled = notifier.GetScheduledToastNotifications();
我们可以通过GetScheduledToastNotifications来获得正在等待推送的toast 然后通过RemoveFromSchedule 方法来移除指定的toast 的推送
  notifier.RemoveFromSchedule(ScheduledToastNotification);
 
 
3.带参数的toast 一般的toast 显示时间受制与系统限制
image
如果我们想让我们的toast显示更长的时间可以 通过代码来设置duration 属性可以延长至25S 
 
            var template = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText01);
            var tileAttributes = template.GetElementsByTagName("text")[0];
            tileAttributes.AppendChild(template.CreateTextNode("That's   tile  toast notifications !"));
            var launch = template.CreateAttribute("launch"); 
            launch.Value = "aaaaa";
           template.FirstChild.Attributes.SetNamedItem(launch);
 
           var duration = template.CreateAttribute("duration");
           duration.Value = "long";
           template.FirstChild.Attributes.SetNamedItem(duration);
            var t = template.GetXml();
            var notifications = new ToastNotification(template);
            notifications.Activated += notifications_Activated;
            toastNotifier.Show(notifications);
 
通过launch 我们可以传递参数, 通过duration 我们可以设置时间 
duration 分为short (默认) 和long
 
 
 

通过上篇和这篇的介绍 你已经掌握了足够的本地推送技巧,希望对你用 ~Enjoy

代码下载

 posted on 2012-04-18 19:10  沐訫  阅读(783)  评论(1编辑  收藏  举报