Windows Phone 8.1 SL推送通知后台任务运行方法过早退出.但它在一个示例演示项目中运行良好
本文关键字:运行 一个 项目 SL Phone 通知 退出 方法 后台任务 Windows | 更新日期: 2023-09-27 18:13:19
为了在后台保存RAW notification
有效负载,我添加了一个Windows Runtime Component
项目来实现后台任务执行。我设置了一个名为"BgTask"的类,实现了IBackgroundTask
和Run
方法。
namespace MyBgTask
{
public sealed class BgTask : IBackgroundTask
{
public void Run(IBackgroundTaskInstance taskInstance)
{
var a = taskInstance.GetDeferral();
string taskName = taskInstance.Task.Name;
Debug.WriteLine("Background " + taskName + " starting...");
// Store the content received from the notification so it can be retrieved from the UI.
//RawNotification notification = (RawNotification)taskInstance.TriggerDetails;
ApplicationDataContainer settings = ApplicationData.Current.LocalSettings;
//var pushdata = JsonConvert.DeserializeObject<PushModelData>(notification.Content);
//var pushdata = notification.Content;
var pushdata = "anoop";
if (settings.Values.ContainsKey(taskName))
{
var dataaa = settings.Values[taskName].ToString();
Debug.WriteLine(dataaa);
var lst = JsonConvert.DeserializeObject<List<string>>(dataaa);
lst.Add(pushdata);
var seri = JsonConvert.SerializeObject(lst);
Debug.WriteLine("saving >>> " + seri);
settings.Values[taskName] = seri;
}
else
{
var lst = new List<string>();
lst.Add(pushdata);
var seri = JsonConvert.SerializeObject(lst);
Debug.WriteLine("saving >>> " + pushdata);
settings.Values[taskName] = seri;
}
Debug.WriteLine("Background " + taskName + " completed!");
a.Complete();
}
}
}
在Packagemanifest中,我将Pushnotification EntryPoint注册为
MyBgTask.BgTask
在Main页面中,我注册了任务
private async void Button_Click(object sender, RoutedEventArgs e)
{
BackgroundAccessStatus backgroundStatus = await BackgroundExecutionManager.RequestAccessAsync();
if (backgroundStatus != BackgroundAccessStatus.Denied && backgroundStatus != BackgroundAccessStatus.Unspecified)
{
try
{
PushNotificationChannel channel = await PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync();
channel.PushNotificationReceived += OnPushNotification;
UnregisterBackgroundTask();
RegisterBackgroundTask();
}
catch (Exception ex)
{
// ... TODO
}
}
}
private void OnPushNotification(PushNotificationChannel sender, PushNotificationReceivedEventArgs args)
{
}
private void RegisterBackgroundTask()
{
BackgroundTaskBuilder taskBuilder = new BackgroundTaskBuilder();
taskBuilder.TaskEntryPoint = "MyBgTask.BgTask";
taskBuilder.Name = "BgTask";
PushNotificationTrigger trigger = new PushNotificationTrigger();
taskBuilder.SetTrigger(trigger);
try
{
BackgroundTaskRegistration task = taskBuilder.Register();
task.Progress += task_Progress;
task.Completed += BackgroundTaskCompleted;
Debug.WriteLine("Registration success: ");
}
catch (Exception ex)
{
Debug.WriteLine("Registration error: " + ex.Message);
UnregisterBackgroundTask();
}
}
void task_Progress(BackgroundTaskRegistration sender, BackgroundTaskProgressEventArgs args)
{
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
Debug.WriteLine("progress");
});
}
private void BackgroundTaskCompleted(BackgroundTaskRegistration sender, BackgroundTaskCompletedEventArgs args)
{
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
MessageBox.Show("Background work item triggered by raw notification with payload = " + " has completed!");
});
}
private bool UnregisterBackgroundTask()
{
foreach (var iter in BackgroundTaskRegistration.AllTasks)
{
IBackgroundTaskRegistration task = iter.Value;
if (task.Name == "BgTask")
{
task.Unregister(true);
return true;
}
}
return false;
}
设置Packagemanifest中的入口点。当我用大量代码向我的主要项目发送原始通知时,pushnotification event
会触发。但问题是,当事件触发时,Run方法中的代码块没有触发。它会自动退出,并显示消息
程序'[4484]BACKGROUNDTASKHOST.EXE'有代码1 (0x1)退出
并且每次都发生。我创建了另一个空白的示例windows phone silverlight项目,并实现了后台任务。我测试了发送RAW通知,现在它工作得很好,Run方法中的代码块做得很好。注意,两个项目都在同一个解决方案中。
我需要澄清以下几点1. 为什么后台任务在进入Run方法时自动退出?2. 这是因为我的项目较大或占用较高的应用程序内存吗?3.我怎样才能确定确切的原因呢?
请帮我想出一个解决方案,以工作的预期!
多谢
我认为问题不是你的BackgroundTask
,你可以忽略出口代码1。
我认为你的问题是:
你告诉操作系统创建一个新的触发器,当操作系统接收到一个新的PushNotification
时,启动一个新的BackgroundTask
。这个BackroundTask
或多或少是一个完全自己的应用程序,将由操作系统启动。你的应用程序和后台任务共享的一个视图是ApplicationData.Current.LocalSettings
,你可以在你的应用程序和BackgroundTask
之间交换数据。
当你创建/注册触发器时,你会得到一个BackgroundTaskRegistration
对象,你可以用它来绑定你的当前运行的应用程序,例如,到完成的事件。
只要你的应用在运行,一切都会正常工作,但如果你的应用被挂起或关闭,你的应用可能会失去对完成事件的绑定。
如果你想在应用程序未运行时向用户显示通知,请在BackgroundTask
中进行。
如果你需要从后台任务获取数据,即更新本地数据。保存到ApplicationData.Current.LocalSettings
,并在每次启动或恢复应用程序时检查它和事件绑定。
请记住使BackgroundTask
简单,因为每个BackgroundTask每小时只有几秒钟的计算时间。