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);
            }
        }
Exemple #2
0
        // 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
            {
                // Create a text message - set the entered destination number and message text.
                SmsTextMessage msg = new SmsTextMessage();
                msg.To   = SendToText.Text;
                msg.Body = SendMessageText.Text;

                // Send the message asynchronously
                rootPage.NotifyUser("Sending message ...", NotifyType.StatusMessage);
                await device.SendMessageAsync(msg);

                rootPage.NotifyUser("Text message sent", NotifyType.StatusMessage);
            }
            catch (Exception err)
            {
                rootPage.NotifyUser(err.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;
            }
        }
Exemple #3
0
        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);
        }
        // Handle a received message event.
        async void device_SmsMessageReceived(SmsDevice sender, SmsMessageReceivedEventArgs args)
        {
            await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal,
                                      () =>
            {
                try
                {
                    // Get message from the event args.
                    SmsTextMessage msg = args.TextMessage;
                    msgCount          += 1;

                    ReceivedCountText.Text   = msgCount.ToString();
                    ReceivedFromText.Text    = msg.From;
                    ReceivedMessageText.Text = msg.Body;
                    rootPage.NotifyUser(msgCount.ToString() + ((msgCount == 1) ? " message" : " messages") + " received", NotifyType.StatusMessage);
                }
                catch (Exception ex)
                {
                    rootPage.NotifyUser(ex.Message, NotifyType.ErrorMessage);
                }
            });
        }
Exemple #5
0
        // Handle a request to read a message.
        private async void Read_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;
                }
            }

            // Clear message display.
            DateText.Text        = "";
            ReadFromText.Text    = "";
            ReadMessageText.Text = "";

            try
            {
                // Parse the message ID - must be number between 1 and maximum message count.
                uint id;
                if (uint.TryParse(ReadIdText.Text, out id) &&
                    (id >= 1) && (id <= device.MessageStore.MaxMessages))
                {
                    rootPage.NotifyUser("Reading message ...", NotifyType.StatusMessage);

                    // Get the selected message from message store asynchronously.
                    ISmsMessage msg = await device.MessageStore.GetMessageAsync(id);

                    // See if this is a text message by querying for the text message interface.
                    ISmsTextMessage textMsg = msg as ISmsTextMessage;
                    if (textMsg == null)
                    {
                        // If it is a binary message then try to convert it to a text message.
                        if (msg is SmsBinaryMessage)
                        {
                            textMsg = SmsTextMessage.FromBinaryMessage(msg as SmsBinaryMessage);
                        }
                    }

                    // Display the text message information.
                    if (textMsg != null)
                    {
                        DateText.Text        = textMsg.Timestamp.DateTime.ToString();
                        ReadFromText.Text    = textMsg.From;
                        ReadMessageText.Text = textMsg.Body;

                        rootPage.NotifyUser("Message read.", NotifyType.StatusMessage);
                    }
                }
                else
                {
                    rootPage.NotifyUser("Invalid ID number entered.", NotifyType.ErrorMessage);
                }
            }
            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;
            }
        }