UWP PushNotificationTrigger赢得';t触发后台任务
我正在尝试开发一个Windows10UWP应用程序,它有一个后台任务,由一个原始的Pushnotification触发器触发。作为测试,我还在应用程序上安装了一个处理程序,以查看原始通知是否确实正确推送,并且是否有效。以下是代码片段:UWP PushNotificationTrigger赢得';t触发后台任务,uwp,wns,Uwp,Wns,我正在尝试开发一个Windows10UWP应用程序,它有一个后台任务,由一个原始的Pushnotification触发器触发。作为测试,我还在应用程序上安装了一个处理程序,以查看原始通知是否确实正确推送,并且是否有效。以下是代码片段: PushNotificationChannel _channel = null; private async void AppInit() { _channel = await Common.WNSHelper.GetCh
PushNotificationChannel _channel = null;
private async void AppInit()
{
_channel = await Common.WNSHelper.GetChannelAsync();
if (_channel != null)
{
_channel.PushNotificationReceived += _channel_PushNotificationReceived;
}
await InstallPositionBackgroundTask();
}
private async void _channel_PushNotificationReceived(PushNotificationChannel sender, PushNotificationReceivedEventArgs args)
{
if (args.NotificationType == PushNotificationType.Raw)
{
Common.GeneralHelper.SendToast("At App PositionBackgroundTask");
}
}
private async Task InstallPositionBackgroundTask()
{
var taskName = "PositionBackgroundTask";
foreach (var tsk in BackgroundTaskRegistration.AllTasks)
{
if (tsk.Value.Name == taskName)
{
tsk.Value.Unregister(true);
}
}
var cost = BackgroundWorkCost.CurrentBackgroundWorkCost;
if (cost == BackgroundWorkCostValue.High)
{
return;
}
var allowed = await BackgroundExecutionManager.RequestAccessAsync();
if (allowed == BackgroundAccessStatus.AllowedSubjectToSystemPolicy
|| allowed == BackgroundAccessStatus.AlwaysAllowed)
{
var builder = new BackgroundTaskBuilder();
builder.Name = nameof(PositionBackgroundTask.PositionBackgroundTask);
builder.CancelOnConditionLoss = false;
builder.TaskEntryPoint = typeof(PositionBackgroundTask.PositionBackgroundTask).FullName;
builder.SetTrigger(new PushNotificationTrigger());
BackgroundTaskRegistration task = builder.Register();
task.Completed += new BackgroundTaskCompletedEventHandler(OnCompleted);
Common.GeneralHelper.SendToast("Position Task Installed. " + System.DateTime.Now.ToString());
}
}
private async void button_Click(object sender, RoutedEventArgs e)
{
// send it
var response = await Common.WNSHelper.PushRawAsync<Common.RawRequest>(_channel.Uri, new RawRequest() { RequestType = RawRequestType.RequestPosition, FromDeviceId = _allDevices[0].D_Id }, null);
}
这是舱单
<Extension Category="windows.backgroundTasks" EntryPoint="PositionBackgroundTask.PositionBackgroundTask">
<BackgroundTasks>
<Task Type="pushNotification" />
</BackgroundTasks>
</Extension>
结果证明我没有将IBackgroundTask子类化
应该是这样的
public sealed class PositionBackgroundTask : IBackgroundTask
后台任务必须是IBackgroundTask的子类
public sealed class PositionBackgroundTask:IBackgroundTask您是否能够在没有通知的情况下运行后台任务?也就是说,您可以使用代码/按钮单击手动触发它吗?据我所知,在Visual Studio中无法触发推送通知。我实际上有一个TimeTrigger事件后台任务,我能够在VisualStudio中测试它。有没有办法实际运行推送通知后台任务?我只是想确保您看到后台任务代码被另一个触发器触发。您似乎已经解决了问题。如果是这样的话,我建议你以后让其他人可以找到它。我也这样创建了,但我的服务中没有收到任何帮助通知?
public static async Task<HttpResponseMessage> PushRawAsync<T>(string channelUri, RawRequest request, T rawData)
{
// Get App authorization
string appDataSecret = MYAPPSECRET;
Uri requestUri = new Uri(@"https://login.live.com/accesstoken.srf");
var client = new System.Net.Http.HttpClient();
System.Net.Http.HttpResponseMessage response = await client.PostAsync(requestUri, new StringContent(appDataSecret, System.Text.Encoding.UTF8, "application/x-www-form-urlencoded"));
string responJsonText = await response.Content.ReadAsStringAsync();
WMNToken token = JsonConvert.DeserializeObject<WMNToken>(responJsonText);
string requestStr = JsonConvert.SerializeObject(request) + ";" + JsonConvert.SerializeObject(rawData);
var content = new StreamContent(new MemoryStream(Encoding.UTF8.GetBytes(requestStr)));
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue(token.Token_type, token.Access_token);
client.DefaultRequestHeaders.Add("X-WNS-Type", "wns/raw");
response = await client.PostAsync(channelUri, content);
return response;
}
Activation of the app 45737MyName.TestPushNotificationpartofemail_4rhf8erfmecqa!App for the Windows.BackgroundTasks contract failed with error: No such interface supported.
public sealed class PositionBackgroundTask : IBackgroundTask