使用解析在推送通知触发器的后台执行代码 [英] Execute a code in background on push notification trigger with parse
问题描述
我开发了一个使用解析进行推送通知的应用。我收到吐司形式的通知,但它没有触发PushNotificationTrigger。我试图调试代码,后台的代码没有被执行。
的原因是不工作的触发器。可能是什么原因?
I've developed an app which uses parse for push notification. I am receiving notifications in the form of toast but it is not triggering the PushNotificationTrigger. I tried to debug the code, and the code in the background is not getting executed. The reason is the trigger in not working. What might be the reason for this ?
private async void Button_Click(object sender, RoutedEventArgs e)
{
try
{
Debug.WriteLine("Registering task");
var taskRegistered = false;
var exampleTaskName = "BackgroundTask";
foreach (var task in BackgroundTaskRegistration.AllTasks)
{
if (task.Value.Name == exampleTaskName)
{
//taskRegistered = true;
task.Value.Unregister(true);
break;
}
}
await BackgroundExecutionManager.RequestAccessAsync();
if (!taskRegistered)
{
Debug.WriteLine("Registering task inside");
var builder = new BackgroundTaskBuilder();
builder.Name = exampleTaskName;
builder.TaskEntryPoint = "Tasks.BackgroundTask";
builder.Name = "PushNotification";
PushNotificationTrigger myTrigger = new PushNotificationTrigger();
builder.SetTrigger(myTrigger);
BackgroundTaskRegistration task = builder.Register();
await new MessageDialog("Task registered!").ShowAsync();
}
}
catch (Exception ex)
{
}
}
背景代码:
namespace Tasks
{
public sealed class BackgroundTask:IBackgroundTask
{
public void Run(IBackgroundTaskInstance taskInstance)
{
Debug.WriteLine("1");
BackgroundTaskDeferral _deferral = taskInstance.GetDeferral();
ToastTemplateType toastTemplate = ToastTemplateType.ToastText02;
XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(toastTemplate);
XmlNodeList textElements = toastXml.GetElementsByTagName("text");
textElements[0].AppendChild(toastXml.CreateTextNode("Background Task"));
textElements[1].AppendChild(toastXml.CreateTextNode("I'm message from your background task!"));
ToastNotificationManager.CreateToastNotifier().Show(new ToastNotification(toastXml));
_deferral.Complete();
}
}
}
请帮助我,因为我不明白由于哪个原因tigger没有被执行。
Please help me as I do not understand the reason due to which the tigger is not getting executed.
推荐答案
您好Ankita94,
Hi Ankita94,
作为友情提醒,请务必按照
发布指南:主题行标记
As a friendly reminder please make sure to add the appropriate tags to the title of your post as per Guide to posting: subject line tags
如果我们想使用PushNotificationTrigger在Bac kgroundTask,我们必须使用
原始通知而不是吐司通知。
If we want to use the PushNotificationTrigger in the BackgroundTask, we have to use the raw notification instead of the toast notification.
有关详细信息,请参阅以下文章:
#PushNotificationTrigger:
https ://msdn.microsoft.com/en-us/library/windows/apps/xaml/windows.applicationmodel.background.pushnotificationtrigger.aspx
。
#Support your具有后台任务的应用程序:
https:// msdn .microsoft.com / zh-CN / library / windows / apps / xaml / mt299103.aspx 。
For more information, please try to refer to the following articles:
#PushNotificationTrigger:
https://msdn.microsoft.com/en-us/library/windows/apps/xaml/windows.applicationmodel.background.pushnotificationtrigger.aspx
.
#Support your app with background tasks:
https://msdn.microsoft.com/en-us/library/windows/apps/xaml/mt299103.aspx .
最诚挚的问候,>
Amy Peng
Best Regards,
Amy Peng
这篇关于使用解析在推送通知触发器的后台执行代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!