새 SMS 수신 백그라운드 이벤트 실행
모바일 광대역 SMS 플랫폼은 모바일 네트워크 운영자의 관리 SMS 알림인지 일반 SMS 메시지인지에 따라 수신되는 새 SMS 데이터에 대해 별도의 시스템 이벤트를 제공합니다. 모바일 네트워크 운영자로부터 수신되는 새 관리 SMS 알림에 대한 백그라운드 시스템 이벤트는 모바일 광대역 앱에서만 액세스할 수 있습니다.
앱은 이미 SMS를 사용하여 백그라운드 작업에서 받은 새 SMS 메시지를 읽는 사용자 동의를 받았어야 합니다. 앱은 백그라운드 작업에서 시스템 SMS 디바이스 동의 프롬프트를 트리거할 수 없으므로 처음으로 SMS에 액세스하는 경우 백그라운드 작업에서 받은 새 SMS 메시지의 내용을 읽을 수 없습니다.
다음 코드 예제에서는 새 SMS 메시지가 수신될 때 실행되도록 설계된 백그라운드 작업을 보여 줍니다.
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();
}
}
백그라운드 작업을 등록하는 JavaScript 앱 코드
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);