我已经为原始推送通知创建了一个BackgroundTask,并按照以下步骤操作。但是当触发器设置为PushNotificationTrigger时,BackgroundTaks没有运行。
创建一个MyTask类,这是MyTask类的完整代码
namespace WindowsRuntimeComponent1
{
public sealed class MyTask : IBackgroundTask
{
public void Run(IBackgroundTaskInstance taskInstance)
{
RawNotification rawNotification = taskInstance.TriggerDetails as RawNotification;
}
}
}
在Windows Phone 8.1 Universal app中添加了Windows Runtime Component项目的参考。
Package.appxmanifest具有以下声明
BackgroundTask注册具有以下代码,该代码在单击按钮时执行。
private async void RegisterBackgroundTask(object sender, RoutedEventArgs e)
{
var access = await BackgroundExecutionManager.RequestAccessAsync();
if (access != BackgroundAccessStatus.Denied)
{
BackgroundTaskBuilder builder = new BackgroundTaskBuilder();
builder.AddCondition(new SystemCondition(SystemConditionType.InternetAvailable));
builder.Name = "PushTaskTest";
builder.TaskEntryPoint = typeof(WindowsRuntimeComponent1.MyTask).FullName;
builder.SetTrigger(new PushNotificationTrigger());
builder.Register();
}
}
注册PushnotificationChannel的代码
protected async override void OnNavigatedTo(NavigationEventArgs e)
{
var channel = await PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync();
NotificationHub hub = new NotificationHub(
"testmobileservicehub",
"Endpoint=sb://testmobileservicehub-ns.servicebus.windows.net/;SharedAccessKeyName=DefaultListenSharedAccessSignature;SharedAccessKey=WR6Fstfuc+1PpNspuJlcV3t57UPfXLU=");
var result = await hub.RegisterNativeAsync(channel.Uri);
}
现在,当我运行应用程序时,Lifecycle Events显示“无后台任务”
任何人都可以帮助解决为什么在推送通知触发器时没有运行BackgroundTask吗?