剛開始的思路是使用Background Task,寄希望于TimeTrigger中可以設(shè)置具體的時(shí)間,這樣,當(dāng)時(shí)間到了的時(shí)候執(zhí)行后臺(tái)人務(wù),Toast出現(xiàn)。但是TimeTrigger主要是用于LockScreen App,而且時(shí)間只能設(shè)置成每15分鐘一次,其他時(shí)間不行。TimeTrigger的主要任務(wù)是每15分鐘更新一次LockSreen App的Badge。
既然TimeTrigger不能用,那么就得另尋他法。在Notification命名空間下有這么一個(gè)類:ScheduledToastNotification,它的構(gòu)造函數(shù)是這樣的:
public:
ScheduledToastNotification(
XmlDocument^ content,
DateTime deliveryTime
)
該構(gòu)造函數(shù)構(gòu)造一個(gè)定時(shí)的Toast消息,并且只顯示一次。
有了該方法,定時(shí)推送就比較簡(jiǎn)單了,只要在你的代碼中實(shí)現(xiàn)以下代碼:
Windows::Globalization::Calendar^ cal = ref new Windows::Globalization::Calendar();
cal->AddMinutes(1);
Windows::Foundation::DateTime dateToFormat = cal->GetDateTime();
ToastTemplateType toastTemplate = ToastTemplateType::ToastText01;
XmlDocument^ toastXml = ToastNotificationManager::GetTemplateContent(toastTemplate);
XmlNodeList^ toastTextElements = toastXml->GetElementsByTagName("text");
toastTextElements->Item(0)->InnerText = "You need to do a work!";
auto notification = ref new ScheduledToastNotification(toastXml, dateToFormat);
ToastNotificationManager::CreateToastNotifier()->AddToSchedule(notification);
這里DateTime是個(gè)結(jié)構(gòu)體類型,我們只能通過(guò)Calender對(duì)象來(lái)獲取時(shí)間,因?yàn)槲覀冏詈蠼⒌氖且粋€(gè)ScheduledToastNotification類型的Toast,所以最后要哦那個(gè)AddToShedule來(lái)顯示出來(lái)。
posted on 2013-02-01 10:30
Dino-Tech 閱讀(505)
評(píng)論(0) 編輯 收藏 引用