private void DisplayToast(IBackgroundTaskInstance taskInstance) { try { SmsReceivedEventDetails smsDetails = (SmsReceivedEventDetails)taskInstance.TriggerDetails; SmsBinaryMessage smsEncodedmsg = smsDetails.BinaryMessage; 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); } catch (Exception ex) { Debug.WriteLine("Error displaying toast: " + ex.Message); } }
// Handle a request to send a text message. private async void Send_Click(object sender, RoutedEventArgs e) { // If this is the first request, get the default SMS device. If this // is the first SMS device access, the user will be prompted to grant // access permission for this application. if (device == null) { try { rootPage.NotifyUser("Getting default SMS device ...", NotifyType.StatusMessage); device = await SmsDevice.GetDefaultAsync(); } catch (Exception ex) { rootPage.NotifyUser("Failed to find SMS device\n" + ex.Message, NotifyType.ErrorMessage); return; } } try { // Convert the entered message from hex to a byte array. byte[] data; ParseHexString(PduMessageText.Text, out data); // Create a binary message and set the data. SmsBinaryMessage msg = new SmsBinaryMessage(); msg.SetData(data); // Set format based on the SMS device cellular type (GSM or CDMA) msg.Format = (device.CellularClass == CellularClass.Gsm) ? SmsDataFormat.GsmSubmit : SmsDataFormat.CdmaSubmit; // Send message asynchronously. rootPage.NotifyUser("Sending message ...", NotifyType.StatusMessage); await device.SendMessageAsync(msg); rootPage.NotifyUser("Sent message sent in PDU format", NotifyType.StatusMessage); } catch (Exception ex) { rootPage.NotifyUser(ex.Message, NotifyType.ErrorMessage); // On failure, release the device. If the user revoked access or the device // is removed, a new device object must be obtained. device = null; } }
async Task DisplayToastAsync(IBackgroundTaskInstance taskInstance) { SmsReceivedEventDetails smsDetails = (SmsReceivedEventDetails)taskInstance.TriggerDetails; SmsDevice smsDevice = (SmsDevice)await SmsDevice.FromIdAsync(smsDetails.DeviceId); SmsBinaryMessage smsEncodedmsg = (SmsBinaryMessage)await smsDevice.MessageStore.GetMessageAsync(smsDetails.MessageIndex); 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); }