【问题标题】:c# windows 10 raw notification when app is CLOSEDc# windows 10 应用程序关闭时的原始通知
【发布时间】:2016-01-27 13:45:43
【问题描述】:

我正在开发 Windows 10 UWP 应用程序,但在发送和处理原始通知时遇到问题。

我正在从用 php 编写的服务器发送通知,当应用程序打开时,我可以收到发送的 raw 通知。以下是我的通知模板:

           <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);
        }
    }

方法内部仅用于测试。

现在,我想问一下我的结构有什么问题或遗漏?感谢任何建议、示例或帮助。

提前致谢。

【问题讨论】:

    标签: c# notifications push-notification uwp wns


    【解决方案1】:

    您是否签入了已检查“推送通知”的清单声明?

    您还需要在顶部调用 RegisterBackgroundTask 方法

    await BackgroundExecutionManager.RequestAccessAsync();
    

    这是https://msdn.microsoft.com/pl-pl/library/hh700494.aspx 的文档,其中重要的一句“请求允许应用程序运行后台任务”。

    最后检查后台任务的入口点和名称

    【讨论】:

    • 嗨@matihuf 我正在尝试开发一个测试 UWP 并在关闭时接收后台原始通知,这是我们的第一个应用程序,我们需要一个非常基本的示例来实现这一点。现在我们有一个 Azure 帐户并设置了 WNS,在我的解决方案中还有一个名为 backgroundtask.cs 的项目,目标是接收通知并且找不到为此的基本启动,你能帮助我们吗这?谢谢
    • 我现在没有任何示例代码,但是请阅读此docs.microsoft.com/en-us/windows/uwp/controls-and-patterns/…您对“原始通知触发的后台任务”部分感兴趣
    • 感谢帮助的哥们,我去看看并回复结果... :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-20
    • 2017-12-16
    • 1970-01-01
    • 1970-01-01
    • 2021-05-19
    • 1970-01-01
    相关资源
    最近更新 更多