我正在开发 Windows 10 UWP 应用程序,但在发送和处理原始通知时遇到问题。
我正在从用 php 编写的服务器发送通知,当应用程序打开时,我可以收到发送的原始通知。以下是我的通知模板:
<toast launch='args'>
<visual>
<binding template = 'ToastGeneric'>
<text> başlık </text>
<text> Açıklama </text>
</binding>
</visual>
</toast>
我将上面的模板作为原始通知发送。
另外,我使用下面的方法作为后台任务
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();
}
}
最后,我的 pushreceived 事件:
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);
}
}
该方法的内部仅用于测试。
现在,我想问一下我的结构有什么问题或遗漏?任何建议,示例或帮助表示赞赏。
提前致谢。