Condividi tramite


Eseguire nuovi eventi in background ricevuti da SMS

La piattaforma SMS Mobile Broadband fornisce eventi di sistema separati per i nuovi dati SMS ricevuti, a seconda che sia una notifica SMS amministrativa da un operatore di rete mobile o un messaggio SMS generale. L'evento di sistema in background per una nuova notifica SMS amministrativa ricevuta da un operatore di rete mobile è accessibile solo da un'app a banda larga per dispositivi mobili.

Le app devono avere già ricevuto il consenso dell'utente per usare SMS per leggere nuovi messaggi SMS ricevuti in un'attività in background. Le app non possono leggere il contenuto di un nuovo messaggio SMS ricevuto da un'attività in background se accedono a SMS per la prima volta, perché l'app non può attivare la richiesta di consenso del dispositivo SMS di sistema da un'attività in background.

Gli esempi di codice seguenti illustrano un'attività in background progettata per l'esecuzione quando viene ricevuto un nuovo messaggio SMS.

Codice attività in background C#

namespace SmsBackgroundSample
{
  public sealed class SmsBackgroundTask : IBackgroundTask
  { 
    // The Run method is the entry point of a background task.

    public void Run(IBackgroundTaskInstance taskInstance)
    {
      // Associate a cancellation handler with the background task.

      taskInstance.Canceled += new BackgroundTaskCanceledEventHandler(OnCanceled);

      ManualResetEvent manualEventWaiter = new ManualResetEvent(false);
      manualEventWaiter.Reset();

      // Do the background task activity.

      DisplayToastAsync(taskInstance, manualEventWaiter);

      // Wait until the async operation is done. We need to do this else the background process will exit.
      manualEventWaiter.WaitOne(5000);

            Debug.Print("Background " + taskInstance.Task.Name + (" process ran"));

  }

  async void DisplayToastAsync(IBackgroundTaskInstance taskInstance, ManualResetEvent manualEventWaiter)
  {
    SmsReceivedEventDetails smsDetails = (SmsReceivedEventDetails)taskInstance.TriggerDetails;
    SmsBinaryMessage smsEncodedmsg = (SmsBinaryMessage) smsDetails.BinaryMessageMessage;
    SmsTextMessage smsTextMessage = Windows.Devices.Sms.SmsTextMessage.FromBinaryMessage(smsEncodedmsg);

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

    manualEventWaiter.Set();
  }

}

Codice dell'app JavaScript per registrare l'attività in background

var triggerAway = new Windows.ApplicationModel.Background.SystemTrigger(Windows.ApplicationModel.Background.SystemTriggerType.smsReceived, false);
var builderAway = new Windows.ApplicationModel.Background.BackgroundTaskBuilder();

builderAway.setTrigger(triggerAway);
builderAway.taskEntryPoint = "HelloWorldBackground.BackgroundTask1";
builderAway.name = "Sms";

var taskAway = builderAway.register();
taskAway.addEventListener("progress", new ProgressHandler(taskAway).onProgress);
taskAway.addEventListener("completed", new CompleteHandler(taskAway).onCompleted);

Sviluppo di app SMS