从睡眠模式返回时的后台任务,仅显示一次 - UWP

时间:2018-05-07 17:08:39

标签: c# windows uwp toast background-task

我创建了一个在后台运行的任务来接收短信。我按照windows本身的例子作为支持。

https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/SmsSendAndReceive

然而,当我的应用程序关闭且窗口处于睡眠模式时,返回时,应用程序只显示应显示的几条消息中的一条。

当应用程序打开或关闭时,无论Windows运行,所有邮件都会正常到达。

private async void Register_BackgroundTask()
    {
        var settings = ApplicationData.Current.LocalSettings;           
        try
        {
            var access = await BackgroundExecutionManager.RequestAccessAsync();
            switch (access)
            {
                case BackgroundAccessStatus.Unspecified:
                    break;
                case BackgroundAccessStatus.AlwaysAllowed:
                    break;
                case BackgroundAccessStatus.AllowedSubjectToSystemPolicy:
                    break;
                case BackgroundAccessStatus.DeniedBySystemPolicy:
                    break;
                case BackgroundAccessStatus.DeniedByUser:
                    break;
                default:
                    break;
            }

            SmsMessageType _messageType = SmsMessageType.Text; // set as Text as default
            _messageType = SmsMessageType.Text;

            SmsFilterRule _filterRule = new SmsFilterRule(_messageType);

            SmsFilterActionType _type = SmsFilterActionType.Accept;
            _type = SmsFilterActionType.Accept;

            SmsFilterRules _Rules = new SmsFilterRules(_type);
            IList<SmsFilterRule> rules = _Rules.Rules;
            rules.Add(_filterRule);

            BackgroundTaskBuilder taskBuilder = new BackgroundTaskBuilder();
            SmsMessageReceivedTrigger trigger = new SmsMessageReceivedTrigger(_Rules);

            taskBuilder.SetTrigger(trigger);
            taskBuilder.TaskEntryPoint = BackgroundTaskEntryPoint;
            taskBuilder.Name = BackgroundTaskName;

            foreach (var cur in BackgroundTaskRegistration.AllTasks)
            {
                if (cur.Value.Name == BackgroundTaskName)
                {                       
                    return;                        
                }
            }

            BackgroundTaskRegistration taskRegistration = taskBuilder.Register();
            taskRegistration.Completed += new BackgroundTaskCompletedEventHandler(OnCompleted);

            //LOG              
        }
        catch (Exception ex)
        {
            //LOG               
        }          
    }

运行方法:

public void Run(IBackgroundTaskInstance taskInstance)
    {
        var deferal = taskInstance.GetDeferral();      
        taskInstance.Canceled += new BackgroundTaskCanceledEventHandler(OnCanceled);                       
        DisplayToast(taskInstance);
        deferal.Complete();
    }

显示吐司:

    private void DisplayToast(IBackgroundTaskInstance taskInstance)
    {
        try
        {              
                SmsMessageReceivedTriggerDetails smsDetails = taskInstance.TriggerDetails as SmsMessageReceivedTriggerDetails;
                SmsTextMessage2 smsTextMessage;

                if (smsDetails.MessageType == SmsMessageType.Text)
                {
                    smsTextMessage = smsDetails.TextMessage;                        

                    XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText02);                           
                    XmlNodeList stringElements = toastXml.GetElementsByTagName("text");                        
                    stringElements.Item(0).AppendChild(toastXml.CreateTextNode(smsTextMessage.From));
                    stringElements.Item(1).AppendChild(toastXml.CreateTextNode(smsTextMessage.Body));

                    ToastNotification notification = new ToastNotification(toastXml);
                    ToastNotificationManager.CreateToastNotifier().Show(notification);                        
                }
            // Message ACK to operator
            smsDetails.Accept();
            }
        catch (Exception ex)
        {
            Debug.WriteLine("Error displaying toast: " + ex.Message);               
        }
    }

我知道电池保护程序正在影响,但即使我进入Windows的设置并选择由用户管理的应用程序管理并允许后台执行任务,从睡眠状态返回时只有1条消息到达。 / p>

如果我不更改此设置,即使运行Windows

,也不会收到任何消息

是否还有其他配置可以使从睡眠模式返回时显示所有的吐司?

1 个答案:

答案 0 :(得分:0)

我已经使用您的代码创建了一个代码示例。但我无法重现你的问题。即使应用程序已关闭且窗口处于睡眠模式,它也能在我身边工作。唯一的区别是我在应用程序的功能中声明了cellularMessaging

snapshot_date_str

这是code sample你可以参考。