在 WNS 中使用原始推送通知
本文关键字:通知 原始 WNS | 更新日期: 2023-09-27 18:35:18
我一直在尝试在我的Windows和Windows Phone 8.1应用程序上实现BackgroundTask的原始推送通知,但它似乎不起作用。我已经成功地让基于 Toast 的推送通知工作,但据我所知,Raw 通知将数据静默推送到应用程序,由应用程序显示 Toast 通知或更新应用程序的磁贴。
我已经查看了背景任务示例并完全遵循它,但没有任何效果(https://code.msdn.microsoft.com/windowsapps/Background-Task-Sample-9209ade9)。
以下是我采取的步骤
- 在与其他项目相同的解决方案中创建了一个 Windows 运行时组件项目(称为通知服务器)
- 将类重命名为 RawTask.cs并实现了
IBackgroundTask
及其Run
方法 -
创建了创建 Toast 通知的方法
私有无效发送通知(字符串文本) { XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText01);
XmlNodeList elements = toastXml.GetElementsByTagName("text"); foreach (IXmlNode node in elements) { node.InnerText = text; } ToastNotification notification = new ToastNotification(toastXml); ToastNotificationManager.CreateToastNotifier().Show(notification); }
-
向 Run 方法添加了代码
BackgroundTaskDeferral _deferral = taskInstance.GetDeferral();
RawNotification notification = (RawNotification)taskInstance.TriggerDetails; string content = notification.Content; // ... SendNotification("test"); // ... _deferral.Complete();
-
将我的应用清单更新为
Toast Capable = YES
和Lock Screen Notifications = Badge
-
添加了具有
Supported Task Type = Push Notification
和Entry Point = NotificationServer.RawTask
的后台任务的声明 -
添加了用于注册后台任务的代码
public static BackgroundTaskRegistration RegisterBackgroundTask(string taskEntryPoint, 字符串任务名称, IBackground触发器触发器, IBackground条件条件) { // 检查此后台任务的现有注册。 //
foreach (var cur in BackgroundTaskRegistration.AllTasks) { if (cur.Value.Name == taskName) { // // The task is already registered. // return (BackgroundTaskRegistration)(cur.Value); } } // // Register the background task. // var builder = new BackgroundTaskBuilder(); builder.Name = taskName; builder.TaskEntryPoint = taskEntryPoint; builder.SetTrigger(trigger); if (condition != null) { builder.AddCondition(condition); } BackgroundTaskRegistration task = builder.Register(); return task; }
并执行它
var reg = RegisterBackgroundTask("NotificationServer.RawTask", "RawNotifications", new PushNotificationTrigger(), null);
我在这里缺少什么吗,我的应用程序似乎没有响应推送通知事件。我已确保我的应用与应用商店中的应用相关联,并且推送使用正确的客户端密码和应用 ID 发送。
有四种类型的推送通知:
- 磁贴更新
- 徽章更新
- 祝酒词通知
- 原始通知
所有 Windows 运行时应用都可以在前台使用前三个推送通知。只有锁屏应用才能接收来自 WNS 的原始推送通知。因此,必须将应用设置为锁屏应用。
您可以按照本主题执行此操作。