gpt4 book ai didi

c# - 在 WNS 中使用原始推送通知

转载 作者:太空宇宙 更新时间:2023-11-03 17:01:36 25 4
gpt4 key购买 nike

我一直在尝试在我的 Windows 和 Windows Phone 8.1 应用程序上为 Raw Push Notifications 实现 BackgroundTask,但它似乎不起作用。我已经成功地让基于 toast 的推送通知正常工作,但据我所知,Raw 通知会静默将数据推送到应用程序,并且由应用程序来显示 toast 通知或更新应用程序的磁贴。

我查看了 BackgroundTask 示例并完全遵循它但没有任何效果 (https://code.msdn.microsoft.com/windowsapps/Background-Task-Sample-9209ade9)。

这是我采取的步骤

  1. 在与我的其他项目(称为 NotificationServer)相同的解决方案中创建了一个 Windows 运行时组件项目
  2. 将类重命名为 RawTask.cs 并实现了 IBackgroundTask 及其 Run 方法
  3. 创建了一个方法来创建 toast 通知

    private void SendNotification(字符串文本) { 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);
    }
  4. 向 Run 方法添加代码

    BackgroundTaskDeferral _deferral = taskInstance.GetDeferral();

            RawNotification notification = (RawNotification)taskInstance.TriggerDetails;
    string content = notification.Content;

    // ...
    SendNotification("test");

    // ...

    _deferral.Complete();
  5. 将我的应用 list 更新为 Toast Capable = YESLock Screen Notifications = Badge

  6. 为后台任务添加了声明,Supported Task Type = Push NotificationEntry Point = NotificationServer.RawTask

  7. 添加代码以注册后台任务

    public static BackgroundTaskRegistration RegisterBackgroundTask(string taskEntryPoint, 字符串任务名称, IBackgroundTrigger 触发器, IBackgroundCondition条件) { // //检查此后台任务的现有注册。 //

            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 发送推送。

最佳答案

有四种类型的推送通知:

  1. 磁贴更新
  2. 徽章更新
  3. toast 通知
  4. 原始通知

所有 Windows 运行时应用程序都可以在前台使用前三个推送通知。只有锁屏应用可以接收来自 WNS 的原始推送通知。因此,您必须将您的应用设为锁屏应用。

可以关注this topic这样做。

关于c# - 在 WNS 中使用原始推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27642646/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com