2017-02-07 72 views
1

我試圖讓原始推送通知去後臺任務。這在應用程序處於前臺時已經可以工作,所以我知道這與通知交付沒有任何關係。UWP背景原始推送通知

下面是我使用的步驟。

在清單:

<Extensions> 
    <Extension Category="windows.backgroundTasks" 
       EntryPoint="TestApp.BackgroundNotifications"> 
     <BackgroundTasks> 
     <Task Type="pushNotification" /> 
     </BackgroundTasks> 
    </Extension> 
    </Extensions> 

背景類:

namespace TestApp { 
    public sealed class BackgroundNotifications : IBackgroundTask { 
    public void Run(IBackgroundTaskInstance taskInstance) { 
     Debug.WriteLine("Got background notification"); 
    } 
    } 
} 

然後登記:

BackgroundAccessStatus status = 
    await BackgroundExecutionManager.RequestAccessAsync(); 
    BackgroundTaskBuilder taskBuilder = 
    new BackgroundTaskBuilder { 
     Name = "MyBackgroundNotifications", 
     TaskEntryPoint = "TestApp.BackgroundNotifications" 
    }; 
    taskBuilder.SetTrigger(new PushNotificationTrigger()); 
    BackgroundTaskRegistration myTask = taskBuilder.Register(); 

當我發送原始推送通知我在看到這日誌但不是我的調試信息:

The program '[16660] backgroundTaskHost.exe' has exited with code 1 (0x1). 

有沒有我失蹤的一步?

回答

1

您發佈的代碼是正確的。在您的步驟中,您似乎忘記了請參閱通用Windows平臺(UWP)應用程序項目中的後臺任務項目。

在你UWP的應用程序,你可以右鍵點擊參考並選擇添加引用。在項目,選擇解決方案,然後選擇您的後臺任務項目的名稱,然後單擊OK

在此之後,你可以重建你的項目,那麼你的後臺任務應該能夠正常工作。請參閱Create and register an out-of-process background task

+0

是的,就是這樣。我不明白它必須在自己的項目中。 – JOTN