Windows Phone 8.1 Universal App,当触发器设置为Pushnotification时,BackgroundTask未运行

时间:2015-06-19 17:36:27

标签: windows windows-phone-8 push-notification windows-phone-8.1

我已经为原始推送通知创建了一个BackgroundTask,并按照以下步骤操作。但是当触发器设置为PushNotificationTrigger时,BackgroundTaks没有运行。

  1. 我创建了一个便携式Windows运行时组件项目。
  2. 创建一个MyTask类,这是MyTask类的完整代码

    namespace WindowsRuntimeComponent1
    {
        public sealed class MyTask : IBackgroundTask  
        {
            public void Run(IBackgroundTaskInstance taskInstance)
            {
                RawNotification rawNotification = taskInstance.TriggerDetails as RawNotification;
    
            }  
        }
    }
    
  3. 在Windows Phone 8.1 Universal app中添加了Windows Runtime Component项目的参考。

  4. Package.appxmanifest具有以下声明 enter image description here

  5. 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();
        }
    }
    
  6. 注册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);  
    }
    
  7. 现在,当我运行应用程序时,Lifecycle Events显示“无后台任务”

  8. enter image description here

    1. 但是当我把SystemTrigger放到生命周期事件时。
    2. enter image description here

      任何人都可以帮助解决为什么在推送通知触发器时没有运行BackgroundTask吗?

0 个答案:

没有答案