【发布时间】:2015-01-22 09:55:19
【问题描述】:
我一直在尝试编写将原始推送通知显示为 toast 的后台任务。当应用程序运行时,我收到了推送通知。
这是我的后台任务类:
public sealed class BackgroundNotificationsTask : IBackgroundTask
{
public void Run(IBackgroundTaskInstance taskInstance)
{
RawNotification notification = (RawNotification)taskInstance.TriggerDetails;
string content = notification.Content;
Debug.WriteLine("Background raw notification obtained!");
//SendNotification(content);
}
private void SendNotification(string text)
{
XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText01);
XmlNodeList elements = toastXml.GetElementsByTagName("text");
foreach (IXmlNode node in elements)
{
node.InnerText = text;
}
ToastNotification notification = new ToastNotification(toastXml);
ToastNotificationManager.CreateToastNotifier().Show(notification);
}
}
然后我在 MainPage.xaml.cs 中注册
private void RegisterTasks()
{
BackgroundAccessStatus status = await BackgroundExecutionManager.RequestAccessAsync();
var taskRegistered = false;
var exampleTaskName = "NotificationsBackground";
foreach (var task in BackgroundTaskRegistration.AllTasks)
{
if (task.Value.Name == exampleTaskName)
{
taskRegistered = true;
break;
}
}
if(!taskRegistered)
{
var builder = new BackgroundTaskBuilder();
builder.Name = exampleTaskName;
builder.TaskEntryPoint = "BackgroundTasks.NotificationsBackground";
builder.SetTrigger(new Windows.ApplicationModel.Background.PushNotificationTrigger());
try
{
BackgroundTaskRegistration task = builder.Register();
Debug.WriteLine("Background Task registered.");
}
catch (Exception e)
{
Debug.WriteLine("Background Task register exception: " + e.ToString());
}
}
}
现在在 appxmanifest 中,我将“锁屏通知”设置为徽章,然后在声明中添加了带有属性的后台任务,选择了推送通知并将入口点设置为 BackgroundNotificationsTask.cs
![屏幕][2]
我做错了什么还是我遗漏了什么?
编辑:
现在,当我收到推送通知时,应用程序关闭...有人知道为什么吗?
【问题讨论】:
-
显示你是如何注册你的任务的
标签: c# windows-phone-8.1 winrt-xaml