简体   繁体   English

应用未运行Windows Phone 8.1时的RawPushNotification

[英]RawPushNotification when app not running Windows Phone 8.1

I have been trying to write Background Task that would show raw push notification as toast. 我一直在尝试编写后台任务,将原始推送通知显示为烤面包。 I got push notifications working when app is running. 当应用程序运行时,我得到了推送通知。

This is my background task class: 这是我的后台任务类:

public sealed class BackgroundNotificationsTask : IBackgroundTask
{

    public void Run(IBackgroundTaskInstance taskInstance)
    {
        RawNotification notification = (RawNotification)taskInstance.TriggerDetails;
        string content = notification.Content;

        Debug.WriteLine("Background raw notification obtained!");

        //SendNotification(content);
    }

    private void SendNotification(string text)
    {
        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);
    }

}

Then I Register In MainPage.xaml.cs 然后我在MainPage.xaml.cs中注册

    private void RegisterTasks()
    {
        BackgroundAccessStatus status = await BackgroundExecutionManager.RequestAccessAsync();

        var taskRegistered = false;
        var exampleTaskName = "NotificationsBackground";

        foreach (var task in BackgroundTaskRegistration.AllTasks)
        {
            if (task.Value.Name == exampleTaskName)
            {
                taskRegistered = true;
                break;
            }
        }

        if(!taskRegistered)
        {
            var builder = new BackgroundTaskBuilder();

            builder.Name = exampleTaskName;
            builder.TaskEntryPoint = "BackgroundTasks.NotificationsBackground";
            builder.SetTrigger(new Windows.ApplicationModel.Background.PushNotificationTrigger());

            try
            {
                BackgroundTaskRegistration task = builder.Register();

                Debug.WriteLine("Background Task registered.");
            }
            catch (Exception e)
            {
                Debug.WriteLine("Background Task register exception: " + e.ToString());
            }
        }
    }

Now in appxmanifest I have set 'Lock screen notifications' to Badge, then in Declarations I have added Background Task with properies Push notification selected and entry point set as BackgroundNotificationsTask.cs 现在,在appxmanifest中,我已将“锁定屏幕通知”设置为Badge,然后在声明中,我添加了背景任务,并选择了属性推送通知,并将入口点设置为BackgroundNotificationsTask.cs。

![screen][2] ![画面] [2]

Am I doing something wron or is there something that I am missing? 我是在做些奇怪的事还是缺少什​​么?

EDIT: 编辑:

Right now when i obtain push notification the app closes... anyone know why? 现在,当我收到推送通知时,应用程序关闭了……有人知道为什么吗?

嘿

There are a couple of things you're doing wrong. 您做错了几件事。

1) Put your BackgroundTask in a separate project 1)将BackgroundTask放在一个单独的项目中

BackgroundTask projects should be Windows Runtime Components. BackgroundTask项目应为Windows运行时组件。 Also make sure that your background task resides under an accessible namespace. 另外,请确保您的后台任务位于可访问的命名空间下。 Do not forget to reference the background task project from your app project. 不要忘记从您的应用程序项目中引用后台任务项目。

2) Register the correct class 2)注册正确的课程

When registering your background task, always use the fully qualified class name and not the file name: 注册后台任务时,请始终使用完全限定的类名而不是文件名:

BackgroundTasks.BackgroundNotificationsTask

This is the entry point you'll have to use in the package manifest file and in your code (given that the task class is in the project explained under 1) and the namespace is called BackgroundTasks ). 这是您必须在程序包清单文件和代码中使用的入口点(假设任务类在项目中已在1下说明),并且命名空间称为BackgroundTasks )。

3) Call RequestAccessAsync() 3)调用RequestAccessAsync()

Make sure you call this before registering any tasks: 在注册任何任务之前,请确保调用此命令:

BackgroundAccessStatus status = await BackgroundExecutionManager.RequestAccessAsync();

Edit: There is a pretty good walkthrough on MSDN https://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh977055.aspx 编辑:在MSDN上有一个很好的演练https://msdn.microsoft.com/zh-cn/library/windows/apps/xaml/hh977055.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM