BackgroundTask and LockScreen
一般LockScreen的信息包括以下幾部分:
3、在程序中可以提示用於是否將應用放置於鎖屏上,通過BackgroundExecutionManager.RequestAccessAsync()會出現一個對話框,當選擇“allow”,程序將會放置於鎖屏中,但鎖屏應用最多只能有7個,當超過七個,會詢問用戶替換哪個應用。
下面我們實現一個簡單的定時推送信息至LockScreen APP:
第二步:新建一個后台任務類NotificationBackTask.cs文件,功能就是一人簡單的定時器,實現定時推送,代碼與前一篇的很相似,通過 BackgroundTaskProgressEventHandler去觸發,目前我還沒找到其它的方式去觸發主程序,還望高手指教一下~
同樣需要在聲明中添加后台任務 :
第四步:實現PushNotification.xaml的后台代碼
點擊”Set LockScreen App”按鈕:
{
BackgroundAccessStatus status = await BackgroundExecutionManager.RequestAccessAsync();
switch (status)
{
case BackgroundAccessStatus.AllowedWithAlwaysOnRealTimeConnectivity:
tbInfo.Text = " This app is on the lock screen and has access to Always-On Real Time Connectivity. ";
btnSendBadge.IsEnabled = true;
break;
case BackgroundAccessStatus.AllowedMayUseActiveRealTimeConnectivity:
tbInfo.Text = " This app is on the lock screen and has access to Active Real Time Connectivity. ";
btnSendBadge.IsEnabled = true;
break;
case BackgroundAccessStatus.Denied:
tbInfo.Text = " This app is not on the lock screen. ";
break;
case BackgroundAccessStatus.Unspecified:
tbInfo.Text = " The user has not yet taken any action. This is the default setting and the app is not on the lock screen. ";
break;
default:
break;
}}
若APP沒有加入LockScreen,會出現以下畫面:
點擊“Send Badge BackTask”按鈕,同樣需要先注冊task
var builder = new BackgroundTaskBuilder();
builder.TaskEntryPoint = SampleBackgroundTaskEntryPoint;
SystemTrigger trigger = new SystemTrigger(SystemTriggerType.UserAway, false);
builder.SetTrigger(trigger);
SystemCondition condition = new SystemCondition(SystemConditionType.UserPresent);
if (condition != null)
{
builder.AddCondition(condition);
}
task = builder.Register();
task.Completed += task_Completed;
task.Progress += task_Progress;
在task_Progress事件中實現消息的推送:
{
Dispatcher.RunAsync(CoreDispatcherPriority.Normal,() =>
{
var taskRegistration = sender as IBackgroundTaskRegistration;
var progressArgs = args as BackgroundTaskProgressEventArgs;
if ((taskRegistration != null) && (progressArgs != null))
{//BadgeNumericNotificationContent badgeContent = new BadgeNumericNotificationContent(progressArgs.Progress);
//BadgeUpdateManager.CreateBadgeUpdaterForApplication().Update(badgeContent.CreateNotification());
ITileWideSmallImageAndText03 tileContent = TileContentFactory.CreateTileWideSmallImageAndText03();
tileContent.TextBodyWrap.Text = "This tile notification has an image, but it won't be displayed on the lock screen";
tileContent.Image.Src = "ms-appx:///Assets/tile-sdk.png";
tileContent.RequireSquareContent = false;
}
});
}
以上實現了兩種推送,一種是簡單badge,還有一種是Tile信息帶文本信息的,對於后一種帶詳細信息,需要在PC Settings里手動設置,文本信息才會在LOCKSCREEN中顯示,如下:
以上就是LockScreen的簡單介紹,可能還有些不足之處,還望高手指點~