我正在开发一个Windows 10 UWP应用程序,我在原始通知发送和处理方面遇到了问题。
我从用php编写的服务器发送通知,当应用程序打开时,我可以收到发送的原始通知。以下是我的通知模板:
<toast launch='args'>
<visual>
<binding template = 'ToastGeneric'>
<text> başlık </text>
<text> Açıklama </text>
</binding>
</visual>
</toast>
我将上面的模板作为原始通知发送。
另外,我使用以下方法作为backgroundtask
public void Run(IBackgroundTaskInstance taskInstance)
{
// Get the background task details
ApplicationDataContainer settings = ApplicationData.Current.LocalSettings;
string taskName = taskInstance.Task.Name;
Debug.WriteLine("Background " + taskName + " starting...");
// Store the content received from the notification so it can be retrieved from the UI.
RawNotification notification = (RawNotification)taskInstance.TriggerDetails;
settings.Values[taskName] = notification.Content;
Windows.Data.Xml.Dom.XmlDocument doc = new XmlDocument();
var toast = new ToastNotification(doc);
var center = ToastNotificationManager.CreateToastNotifier();
center.Show(toast);
Debug.WriteLine("Background " + taskName + " completed!");
}
我在appmanifest中设置了任务入口点,我无法使用调试器进行测试。
我使用以下代码注册后台任务:
private void RegisterBackgroundTask()
{
BackgroundTaskBuilder taskBuilder = new BackgroundTaskBuilder();
PushNotificationTrigger trigger = new PushNotificationTrigger();
taskBuilder.SetTrigger(trigger);
// Background tasks must live in separate DLL, and be included in the package manifest
// Also, make sure that your main application project includes a reference to this DLL
taskBuilder.TaskEntryPoint = SAMPLE_TASK_ENTRY_POINT;
taskBuilder.Name = SAMPLE_TASK_NAME;
try
{
BackgroundTaskRegistration task = taskBuilder.Register();
task.Completed += BackgroundTaskCompleted;
}
catch (Exception ex)
{
// rootPage.NotifyUser("Registration error: " + ex.Message, NotifyType.ErrorMessage);
UnregisterBackgroundTask();
}
}
最后,我的接受事件:
private async void OnPushNotificationReceived(PushNotificationChannel sender, PushNotificationReceivedEventArgs e)
{
if (e.NotificationType == PushNotificationType.Raw)
{
e.Cancel = false;
Windows.Data.Xml.Dom.XmlDocument doc = new Windows.Data.Xml.Dom.XmlDocument();
var t = @" <toast launch='args'>
<visual>
<binding template = 'ToastGeneric'>
<text> başlık </text>
<text> Açıklama </text>
</binding>
</visual>
</toast> ";
doc.LoadXml(t);
var toast = new ToastNotification(doc);
var center = ToastNotificationManager.CreateToastNotifier();
center.Show(toast);
}
}
该方法的内部仅用于测试。
现在,我想问一下我的结构中有什么问题或错过了什么?任何建议,例子或帮助都表示赞赏。
提前致谢。
答案 0 :(得分:1)
您是否签入了已检查的舱单声明&#34;推送通知&#34;?
您还需要在RegisterBackgroundTask方法调用
的顶部await BackgroundExecutionManager.RequestAccessAsync();
以下是文档https://msdn.microsoft.com/pl-pl/library/hh700494.aspx,其中包含重要句子&#34;请求允许应用运行后台任务。&#34;
最后检查入口点和后台任务的名称