0
votes

I created a task running in the background to receive SMS. I followed the example of windows itself as support.

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

however, when my app is closed and windows are in sleep mode, upon returning, the app displays only one of several messages it should show.

When the app is open or closed, however with windows running all messages arrive normally.

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

Run method:

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

Show toast:

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

I know the battery saver is influencing, but even when I go in the settings of windows and select the management of the app as managed by the user and allowing tasks in the background, only 1 message arrives when returning from sleep.

If I do not change this setting it does not receive any messages even with windows running

is there any other configuration that can be made so that when returning from sleep mode display all the toast?

1
Which trigger you have used? - Nico Zhu - MSFT
the trigger is "SmsMessageReceivedTrigger" - Guilherme Sanches
Do you have any updates? - Nico Zhu - MSFT
no Unfortunately. I still get only 1 toast when I get back from sleep - Guilherme Sanches

1 Answers

0
votes

I have created a code sample with your code. But I could not reproduce your issue. It works in my side even if the app is closed and windows are in sleep mode. The only difference is that I declared cellularMessaging in the app's capability.

<Package xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10"
         xmlns:mp="http://schemas.microsoft.com/appx/2014/phone/manifest"
         xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10" 
          xmlns:r="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"
         IgnorableNamespaces="uap mp r">

      ......

 <Capabilities>
    <r:Capability Name="cellularMessaging" />
  </Capabilities>
</Package>

And this is code sample you could refer.