PushNotifications的Backgroundtask失去了他的触发器

时间:2016-05-31 12:15:59

标签: c# windows-10 uwp windows-10-mobile

  1. 首先我为PushNotificationTrigger注册了后台任务 - 这里没问题,我可以在BackgroundTaskRegistration.AllTask​​s中找到它。但推送通知没有任何活动。并且调试告诉我注册的任务没有触发器 - 尽管我使用PushNotificationTrigger注册了它。我错过了什么吗?

    private BackgroundTaskRegistration RegisterBackgroundTask(string taskName, IBackgroundTrigger connectedTrigger)
    {
        bool taskRegistered = false;
        BackgroundTaskRegistration backgroundTask = null;
    
        foreach (var task in BackgroundTaskRegistration.AllTasks)
        {
            if (task.Value.Name == taskName)
            {
                taskRegistered = true;
                backgroundTask = (BackgroundTaskRegistration)task.Value;
                break;
            }
        }
    
        if (!taskRegistered)
        {
            var builder = new BackgroundTaskBuilder();
    
            builder.Name = taskName;
            builder.TaskEntryPoint = taskName;
            builder.SetTrigger(connectedTrigger);
            backgroundTask = builder.Register();
            backgroundTask.Completed += new BackgroundTaskCompletedEventHandler(OnCompleted);
            backgroundTask.Progress += new BackgroundTaskProgressEventHandler(BackgroundTask_Progress);
        }
        return backgroundTask;
    }
    
    async private void OnCompleted(IBackgroundTaskRegistration sender, BackgroundTaskCompletedEventArgs e)
    {
        string toastVisual = "<visual><binding template='ToastGeneric'><text>Eine Testnachricht</text><text>OnCompleted has triggered</text></binding></visual>";
        string toastSound = "<audio src=\"ms-appx:///Sounds/blupp.wav\" loop=\"false\"/>";
        string toastXmlString = String.Format(@"<toast>{0}{1}</toast>", toastVisual, toastSound);
    
        XmlDocument toastXml = new XmlDocument();
        toastXml.LoadXml(toastXmlString);
    
        var toast = new ToastNotification(toastXml);
    
        DateTime expdate = DateTime.Now.AddDays(1);
        toast.ExpirationTime = expdate;
    
        ToastNotificationManager.CreateToastNotifier().Show(toast);
    }
    
    private void BackgroundTask_Progress(BackgroundTaskRegistration sender, BackgroundTaskProgressEventArgs args)
    {
        string toastVisual = "<visual><binding template='ToastGeneric'><text>Eine Testnachricht</text><text>OnProgress has triggered</text></binding></visual>";
        string toastSound = "<audio src=\"ms-appx:///Sounds/blupp.wav\" loop=\"false\"/>";
        string toastXmlString = String.Format(@"<toast>{0}{1}</toast>", toastVisual, toastSound);
    
        XmlDocument toastXml = new XmlDocument();
        toastXml.LoadXml(toastXmlString);
    
        var toast = new ToastNotification(toastXml);
    
        DateTime expdate = DateTime.Now.AddDays(1);
        toast.ExpirationTime = expdate;
    
        ToastNotificationManager.CreateToastNotifier().Show(toast);
    }
    
  2. 接下来我尝试用BackGround类解决。但是通过该解决方案,我无法找到任何已注册的任务。我想我错过了一些开始,但无法弄清楚是什么。

    public async void Run(IBackgroundTaskInstance taskInstance)
    {
        BackgroundTaskDeferral _deferral = taskInstance.GetDeferral();
    
        System.Diagnostics.Debug.WriteLine("BackgroundTasks here");
    
        var taskRegistered = false;
        var taskName = "alertPushNotification";
    
        foreach (var task in BackgroundTaskRegistration.AllTasks)
        {
            if (task.Value.Name == taskName)
            {
                taskRegistered = true;
                break;
            }
        }
    
        if (!taskRegistered)
        {
            var builder = new BackgroundTaskBuilder();
    
            builder.Name = taskName;
            builder.TaskEntryPoint = "RuntimeComponent1.ExampleBackgroundTask";
            builder.SetTrigger(new PushNotificationTrigger());
            BackgroundTaskRegistration task = builder.Register();
            task.Completed += new BackgroundTaskCompletedEventHandler(OnCompleted);
        }
    
        _deferral.Complete();
    }
    
    private void OnCompleted(IBackgroundTaskRegistration task, BackgroundTaskCompletedEventArgs args)
    {
        string toastVisual = "<visual><binding template='ToastGeneric'><text>Eine Testnachricht</text><text>OnCompleted has triggered</text></binding></visual>";
        string toastSound = "<audio src=\"ms-appx:///Sounds/blupp.wav\" loop=\"false\"/>";
        string toastXmlString = String.Format(@"<toast>{0}{1}</toast>", toastVisual, toastSound);
    
        XmlDocument toastXml = new XmlDocument();
        toastXml.LoadXml(toastXmlString);
    
        var toast = new ToastNotification(toastXml);
    
        DateTime expdate = DateTime.Now.AddDays(1);
        toast.ExpirationTime = expdate;
    
        ToastNotificationManager.CreateToastNotifier().Show(toast);
    }
    
    private void OnProgress(IBackgroundTaskRegistration task, BackgroundTaskProgressEventArgs args)
    {
        string toastVisual = "<visual><binding template='ToastGeneric'><text>Eine Testnachricht</text><text>OnProgress has triggered</text></binding></visual>";
        string toastSound = "<audio src=\"ms-appx:///Sounds/blupp.wav\" loop=\"false\"/>";
        string toastXmlString = String.Format(@"<toast>{0}{1}</toast>", toastVisual, toastSound);
    
        XmlDocument toastXml = new XmlDocument();
        toastXml.LoadXml(toastXmlString);
    
        var toast = new ToastNotification(toastXml);
    
        DateTime expdate = DateTime.Now.AddDays(1);
        toast.ExpirationTime = expdate;
    
        ToastNotificationManager.CreateToastNotifier().Show(toast);
    }
    

    }

  3. 有没有人可以给我一个提示,我错过了哪些代码?

0 个答案:

没有答案