Ejemplo n.º 1
0
        private static async void MessageSender(string contactNumber)
        {
            if (_device == null)
            {
                try
                {
                    _device = SmsDevice2.GetDefault();
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex);
                    return;
                }
            }
            //if (_device == null) return;

            var msg = new SmsTextMessage2
            {
                To   = contactNumber,
                Body = "I am in need of help. My coordinates are\n Latitude:" + _latitude + "Longitude \n" + _longitude
            };
            var result = await _device.SendMessageAndGetResultAsync(msg);

            if (!result.IsSuccessful)
            {
                return;
            }
            var msgStr = "";

            msgStr += "Text message sent, To: " + _phonenumber;
            var msg1 = new MessageDialog("Message Sent!" + msgStr);
            await msg1.ShowAsync();
        }
Ejemplo n.º 2
0
        private async void MessageSender(string contactNumber)
        {
            if (_device == null)
            {
                try
                {
                    _device = SmsDevice2.GetDefault();
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex);
                    return;
                }
            }
            //if (_device == null) return;


            var msg = new SmsTextMessage2
            {
                To   = contactNumber,
                Body = Message + "\t My coordinates are\n Latitude:" + _latitude + "Longitude \n" + _longitude
            };
            var result = await _device.SendMessageAndGetResultAsync(msg);

            SosPageText += "Sending Message.... \n";
            RaisePropertyChanged(() => SosPageText);

            if (!result.IsSuccessful)
            {
                if (result.NetworkCauseCode.Equals(50))
                {
                    SosPageText += "\n Network Error in sending SMS. Possibly no balance!";
                }
                SosPageText += "Message Sending Failed \n";
                RaisePropertyChanged(() => SosPageText);
                return;
            }
            var msgStr = "";

            msgStr      += "Text message sent, To: " + _phonenumber;
            SosPageText += msgStr + "\n";
            RaisePropertyChanged(() => SosPageText);
        }
Ejemplo n.º 3
0
        public void SendSmsInBackground(string recipient, string message = null)
        {
            if (string.IsNullOrEmpty(recipient))
            {
                throw new ArgumentException(nameof(recipient));
            }

            message = message ?? string.Empty;

            if (CanSendSmsInBackground)
            {
                var sendingMessage = new SmsTextMessage2
                {
                    Body = message,
                    To   = recipient
                };

                SmsDevice2.GetDefault().SendMessageAndGetResultAsync(sendingMessage).AsTask().Wait();
            }
        }
Ejemplo n.º 4
0
        private async void SendButton_Click(object sender, RoutedEventArgs e)
        {
            if (device == null)

            {
                try

                {
                    Debug.WriteLine("Getting default SMS device ...");

                    device = SmsDevice2.GetDefault();
                }

                catch (Exception ex)

                {
                    Debug.WriteLine(ex.Message);

                    return;
                }
            }



            string msgStr = "";

            if (device != null)

            {
                var wors = recipientbox.Text.Split("; ");
                foreach (var wor in wors)
                {
                    try

                    {
                        // Create a text message - set the entered destination number and message text.

                        SmsTextMessage2 msg = new SmsTextMessage2();

                        msg.To = wor;

                        msg.Body = MessageTB.Text;

                        // Send the message asynchronously

                        Debug.WriteLine("Sending message ...");

                        SmsSendMessageResult result = await device.SendMessageAndGetResultAsync(msg);



                        if (result.IsSuccessful)

                        {
                            msgStr = "";

                            msgStr += "Text message sent, cellularClass: " + result.CellularClass.ToString();

                            IReadOnlyList <Int32> messageReferenceNumbers = result.MessageReferenceNumbers;



                            for (int i = 0; i < messageReferenceNumbers.Count; i++)

                            {
                                msgStr += "\n\t\tMessageReferenceNumber[" + i.ToString() + "]: " + messageReferenceNumbers[i].ToString();
                            }

                            Debug.WriteLine(msgStr);
                        }

                        else

                        {
                            msgStr = "";

                            msgStr += "ModemErrorCode: " + result.ModemErrorCode.ToString();

                            msgStr += "\nIsErrorTransient: " + result.IsErrorTransient.ToString();

                            if (result.ModemErrorCode == SmsModemErrorCode.MessagingNetworkError)

                            {
                                msgStr += "\n\tNetworkCauseCode: " + result.NetworkCauseCode.ToString();



                                if (result.CellularClass == CellularClass.Cdma)

                                {
                                    msgStr += "\n\tTransportFailureCause: " + result.TransportFailureCause.ToString();
                                }

                                Debug.WriteLine(msgStr);
                            }
                        }
                    }

                    catch (Exception ex)

                    {
                        Debug.WriteLine(ex.Message);
                    }
                }
            }

            else

            {
                Debug.WriteLine("Failed to find SMS device");
            }
        }
Ejemplo n.º 5
0
        private async void Send_Click(object sender, RoutedEventArgs e)
        {
            if (SendToText.Text == "")
            {
                rootPage.NotifyUser("Please enter sender number", NotifyType.ErrorMessage);
                return;
            }

            if (SendMessageText.Text == "")
            {
                rootPage.NotifyUser("Please enter message", NotifyType.ErrorMessage);
                return;
            }

            // 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 = SmsDevice2.GetDefault();
                }
                catch (Exception ex)
                {
                    rootPage.NotifyUser(ex.Message, NotifyType.ErrorMessage);
                    return;
                }
            }

            string msgStr = "";

            if (device != null)
            {
                try
                {
                    // Create a text message - set the entered destination number and message text.
                    SmsTextMessage2 msg = new SmsTextMessage2();
                    msg.To   = SendToText.Text;
                    msg.Body = SendMessageText.Text;

                    // Send the message asynchronously
                    rootPage.NotifyUser("Sending message ...", NotifyType.StatusMessage);
                    SmsSendMessageResult result = await device.SendMessageAndGetResultAsync(msg);

                    if (result.IsSuccessful)
                    {
                        msgStr  = "";
                        msgStr += "Text message sent, cellularClass: " + result.CellularClass.ToString();
                        IReadOnlyList <Int32> messageReferenceNumbers = result.MessageReferenceNumbers;

                        for (int i = 0; i < messageReferenceNumbers.Count; i++)
                        {
                            msgStr += "\n\t\tMessageReferenceNumber[" + i.ToString() + "]: " + messageReferenceNumbers[i].ToString();
                        }
                        rootPage.NotifyUser(msgStr, NotifyType.StatusMessage);
                    }
                    else
                    {
                        msgStr  = "";
                        msgStr += "ModemErrorCode: " + result.ModemErrorCode.ToString();
                        msgStr += "\nIsErrorTransient: " + result.IsErrorTransient.ToString();
                        if (result.ModemErrorCode == SmsModemErrorCode.MessagingNetworkError)
                        {
                            msgStr += "\n\tNetworkCauseCode: " + result.NetworkCauseCode.ToString();

                            if (result.CellularClass == CellularClass.Cdma)
                            {
                                msgStr += "\n\tTransportFailureCause: " + result.TransportFailureCause.ToString();
                            }
                            rootPage.NotifyUser(msgStr, NotifyType.ErrorMessage);
                        }
                    }
                }
                catch (Exception ex)
                {
                    rootPage.NotifyUser(ex.Message, NotifyType.ErrorMessage);
                }
            }
            else
            {
                rootPage.NotifyUser("Failed to find SMS device", NotifyType.ErrorMessage);
            }
        }
        private async Task DisplayToast(ChatMessage message)
        {
            var information = await ContactUtils.FindContactInformationFromSender(message.From);

            string thumbnailpath = "";
            string text          = "";

            string deviceid = SmsDevice2.GetDefault().DeviceId;

            foreach (var attachment in message.Attachments)
            {
                try
                {
                    //extra += " " + attachment.MimeType;

                    if (attachment.MimeType == "application/smil")
                    {
                    }

                    if (attachment.MimeType == "text/vcard")
                    {
                        text += "Contact content in this message. ";
                    }

                    if (attachment.MimeType.StartsWith("image/"))
                    {
                        text += "Image content in this message. ";
                        var imageextension = attachment.MimeType.Split('/').Last();
                        thumbnailpath = await SaveFile(attachment.DataStreamReference, "messagepicture." + DateTimeOffset.Now.ToUnixTimeMilliseconds() + "." + imageextension);
                    }

                    if (attachment.MimeType.StartsWith("audio/"))
                    {
                        text += "Audio content in this message. ";
                        var audioextension = attachment.MimeType.Split('/').Last();
                    }
                }
                catch
                {
                    //extra += " oops";
                }
            }

            var toastContent = new ToastContent()
            {
                Visual = new ToastVisual()
                {
                    BindingGeneric = new ToastBindingGeneric()
                    {
                        Children =
                        {
                            new AdaptiveText()
                            {
                                Text         = information.DisplayName,
                                HintMaxLines = 1
                            },
                            new AdaptiveText()
                            {
                                Text = string.IsNullOrEmpty(message.Body) ? text : message.Body
                            }
                        },
                        HeroImage = new ToastGenericHeroImage()
                        {
                            Source = thumbnailpath
                        },
                        AppLogoOverride = new ToastGenericAppLogo()
                        {
                            Source   = information.ThumbnailPath,
                            HintCrop = ToastGenericAppLogoCrop.Circle
                        },
                        Attribution = new ToastGenericAttributionText()
                        {
                            Text = information.PhoneNumberKind //+ " " + extra
                        }
                    }
                },
                Actions = new ToastActionsCustom()
                {
                    Inputs =
                    {
                        new ToastTextBox("textBox")
                        {
                            PlaceholderContent = "Send a message"
                        }
                    },
                    Buttons =
                    {
                        new ToastButton("Send", "action=reply" + "&from=" + message.From + "&deviceid=" + deviceid)
                        {
                            ActivationType = ToastActivationType.Background,
                            ImageUri       = "Assets/ToastIcons/Send.png",
                            TextBoxId      = "textBox"
                        }
                    }
                },
                Launch = "action=openThread" + "&from=" + message.From + "&deviceid=" + deviceid,
                Audio  = new ToastAudio()
                {
                    Src = new Uri("ms-winsoundevent:Notification.SMS")
                }
            };

            var toastNotif = new ToastNotification(toastContent.GetXml());

            ToastNotificationManager.CreateToastNotifier().Show(toastNotif);
        }
Ejemplo n.º 7
0
        // Clicking of the Start App button
        private async void Send_Click(object sender, RoutedEventArgs e)
        {
            // This event occurs when the user clicks the Start App button and is distinguished from the event where
            // this button click is triggered by a high acceleration event.
            if (HighAccEvent == false)
            {
                // If the device does have an accelerometer
                if (_accelerometer != null)
                {
                    // Set the report interval to enable the sensor for polling
                    _accelerometer.ReportInterval = _desiredReportInterval;
                    _dispatcherTimer.Start();
                }

                // No Accelerometer found. Notify the user.
                else
                {
                    rootPage.NotifyUser("No accelerometer found. This app is not compatible with your device.", NotifyType.ErrorMessage);
                }
            }


            // This sections corresponds to the the event in which the clicking of the start app button is triggered by HighAccEvent
            else
            {
                // Geolocation based on bing API
                Geolocator geolocator = new Geolocator();
                geolocator.DesiredAccuracyInMeters = 500;
                try
                {
                    Geoposition geoposition = await geolocator.GetGeopositionAsync(
                        maximumAge : TimeSpan.FromMinutes(5),
                        timeout : TimeSpan.FromSeconds(10)
                        );

                    // 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 = SmsDevice2.GetDefault();
                        }
                        catch (Exception ex)
                        {
                            rootPage.NotifyUser(ex.Message, NotifyType.ErrorMessage);
                            return;
                        }
                    }

                    string msgStr = "";
                    if (device != null)
                    {
                        try
                        {
                            // Create a text message - set the entered destination number and message text.
                            SmsTextMessage2 msg = new SmsTextMessage2();

                            // If the user has not yet entered the number
                            if (Mnumber == null)
                            {
                                rootPage.NotifyUser("You have not yet entered your emergency contact number!", NotifyType.ErrorMessage);
                                return;
                            }

                            msg.To   = Mnumber;
                            msg.Body = "Emergency alert! I have been in a possible accident at " + geoposition.Coordinate.Latitude.ToString("0.00") + ", " + geoposition.Coordinate.Longitude.ToString("0.00") + ". Please help.";

                            // Send the message asynchronously
                            rootPage.NotifyUser("Sending Message to emergency contact and rescue services.", NotifyType.StatusMessage);
                            SmsSendMessageResult result = await device.SendMessageAndGetResultAsync(msg);

                            if (result.IsSuccessful)
                            {
                                msgStr  = "";
                                msgStr += "Message sent to: " + Mnumber + "(Predefined Emergency Number)" + System.Environment.NewLine;
                                msgStr += "Emergency alert! I have been in a possible accident at Map Coordinates:" + geoposition.Coordinate.Latitude.ToString("0.00") + ", " + geoposition.Coordinate.Longitude.ToString("0.00") + ". Please help." + System.Environment.NewLine;

                                IReadOnlyList <Int32> messageReferenceNumbers = result.MessageReferenceNumbers;
                                rootPage.NotifyUser(msgStr, NotifyType.StatusMessage);
                            }

                            else
                            {
                                msgStr  = "";
                                msgStr += "ModemErrorCode: " + result.ModemErrorCode.ToString();
                                msgStr += "\nIsErrorTransient: " + result.IsErrorTransient.ToString();
                                if (result.ModemErrorCode == SmsModemErrorCode.MessagingNetworkError)
                                {
                                    msgStr += "\n\tNetworkCauseCode: " + result.NetworkCauseCode.ToString();

                                    if (result.CellularClass == CellularClass.Cdma)
                                    {
                                        msgStr += "\n\tTransportFailureCause: " + result.TransportFailureCause.ToString();
                                    }
                                    rootPage.NotifyUser(msgStr, NotifyType.ErrorMessage);
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            rootPage.NotifyUser(ex.Message, NotifyType.ErrorMessage);
                        }
                    }
                    else
                    {
                        if (Mnumber != "default")
                        {
                            rootPage.NotifyUser("Could not connect to network. SMS could not be sent", NotifyType.ErrorMessage);
                        }
                        else
                        {
                            rootPage.NotifyUser("You have not yet entered your emergency contact number!", NotifyType.ErrorMessage);
                        }
                    }
                }
                catch (Exception ex)
                { // No action needed
                }
            }
        }
        private async Task Send_Message(bool Emergency = true)
        {
            if (device == null)
            {
                try
                {
                    device = SmsDevice2.GetDefault();
                }
                catch (Exception ex)
                {
                    NotifyUser(ex.Message, NotifyType.ErrorMessage);
                    return;
                }
            }

            string msgStr = "";

            if (device != null)
            {
                try
                {
                    // Create a text message - set the entered destination number and message text.
                    SmsTextMessage2 msg = new SmsTextMessage2();

                    // TODO : What to do below?
                    msg.To = Mnum.countryCode + Mnum.tenDigits;
                    // msg.To = Mnum.tenDigits;

                    try
                    {
                        if (Emergency)
                        {
                            msg.Body = "The Be-Safe app on this user's device has encountered a high acceleration event. Since your number has been listed as the emergency contact by the user, it is highly recommended that an immediate call is made to assure their safety. User's Location Coordinates : " + pos.Coordinate.Latitude.ToString("0.00") + ", " + pos.Coordinate.Longitude.ToString("0.00");
                        }
                        else
                        {
                            msg.Body = "The Be-Safe app is extremely sorry. The user of this number is completely safe, the previous message was a false alarm!";
                        }
                    }
                    catch (Exception)
                    {
                        msg.Body = "Emergency alert! I have been in a possible accident detected automatically by a high acceleration event. Please help.";
                    }
                    // Send the message asynchronously
                    if (Emergency)
                    {
                        NotifyUser("Sending Message to emergency contact.", NotifyType.StatusMessage);
                    }

                    SmsSendMessageResult result = await device.SendMessageAndGetResultAsync(msg);

                    if (result.IsSuccessful)
                    {
                        msgStr  = "";
                        msgStr += "Message sent to: " + Mnum.countryCode + Mnum.tenDigits + " (Predefined Emergency Number)" + System.Environment.NewLine;
                        try
                        {
                            if (Emergency)
                            {
                                msgStr += "The Be-Safe app on this user's device has encountered a high acceleration event. Since your number has been listed as the emergency contact by the user, it is highly recommended that an immediate call is made to assure their safety. User's Location Coordinates : " + pos.Coordinate.Latitude.ToString("0.00") + ", " + pos.Coordinate.Longitude.ToString("0.00") + System.Environment.NewLine;
                            }
                            else
                            {
                                msgStr += "The Be-Safe app is extremely sorry. The user of this number is completely safe, the previous message was a false alarm!";
                            }
                        }
                        catch (Exception)
                        {
                            msgStr += "Emergency alert! I have been in a possible accident detected automatically by a high acceleration event. Please help.";
                        }
                        if (Emergency)
                        {
                            MainPage.DisplayToast("Emergency Text Sent to " + Mnum.countryCode + Mnum.tenDigits);
                        }
                        else
                        {
                            MainPage.DisplayToast("False Alarm Message Sent");
                        }

                        IReadOnlyList <Int32> messageReferenceNumbers = result.MessageReferenceNumbers;
                        NotifyUser(msgStr, NotifyType.StatusMessage);
                        if (!Emergency)
                        {
                            FalseAlarmButton.Visibility = Visibility.Collapsed;
                            textBlock3.Text             = "False Alarm Message Sent. Sorry for the inconvenience.";
                        }
                        else
                        {
                            FalseAlarmButton.Visibility = Visibility.Visible;
                        }
                    }

                    else
                    {
                        msgStr  = "";
                        msgStr += "ModemErrorCode: " + result.ModemErrorCode.ToString();
                        msgStr += "\nIsErrorTransient: " + result.IsErrorTransient.ToString();
                        if (result.ModemErrorCode == SmsModemErrorCode.MessagingNetworkError)
                        {
                            msgStr += "\n\tNetworkCauseCode: " + result.NetworkCauseCode.ToString();

                            if (result.CellularClass == CellularClass.Cdma)
                            {
                                msgStr += "\n\tTransportFailureCause: " + result.TransportFailureCause.ToString();
                            }
                            NotifyUser(msgStr, NotifyType.ErrorMessage);
                        }
                    }
                }
                catch (Exception ex)
                {
                    NotifyUser(ex.Message, NotifyType.ErrorMessage);
                }
            }
            else
            {
                if (Mnum != null)
                {
                    NotifyUser("Could not connect to network. SMS could not be sent", NotifyType.ErrorMessage);
                }
                else
                {
                    NotifyUser("You have not yet entered your emergency contact number!", NotifyType.ErrorMessage);
                }
            }
        }
Ejemplo n.º 9
0
        public static async void sendSMS(String to, String text)
        {
            if (device == null)
            {
                try
                {
                    //DialogBox.ShowOk("Notification", "Getting default SMS device ...");
                    device = SmsDevice2.GetDefault();
                }
                catch (Exception ex)
                {
                    //rootPage.NotifyUser(ex.Message, NotifyType.ErrorMessage);
                    DialogBox.ShowOk("Error", ex.Message);
                    return;
                }
            }

            string msgStr = "";

            if (device != null)
            {
                try
                {
                    // Create a text message - set the entered destination number and message text.
                    SmsTextMessage2 msg = new SmsTextMessage2();
                    msg.To   = to;
                    msg.Body = text;

                    // Send the message asynchronously
                    //rootPage.NotifyUser("Sending message ...", NotifyType.StatusMessage);
                    SmsSendMessageResult result = await device.SendMessageAndGetResultAsync(msg);

                    if (result.IsSuccessful)
                    {
                        msgStr  = "";
                        msgStr += "Text message sent, cellularClass: " + result.CellularClass.ToString();
                        IReadOnlyList <Int32> messageReferenceNumbers = result.MessageReferenceNumbers;

                        for (int i = 0; i < messageReferenceNumbers.Count; i++)
                        {
                            msgStr += "\n\t\tMessageReferenceNumber[" + i.ToString() + "]: " + messageReferenceNumbers[i].ToString();
                        }
                        DialogBox.ShowOk("Success", msgStr);//change??
                    }
                    else
                    {
                        msgStr  = "";
                        msgStr += "ModemErrorCode: " + result.ModemErrorCode.ToString();
                        msgStr += "\nIsErrorTransient: " + result.IsErrorTransient.ToString();
                        if (result.ModemErrorCode == SmsModemErrorCode.MessagingNetworkError)
                        {
                            msgStr += "\n\tNetworkCauseCode: " + result.NetworkCauseCode.ToString();

                            if (result.CellularClass == CellularClass.Cdma)
                            {
                                msgStr += "\n\tTransportFailureCause: " + result.TransportFailureCause.ToString();
                            }
                            DialogBox.ShowOk("Error", msgStr);
                        }
                    }
                }
                catch (Exception ex)
                {
                    DialogBox.ShowOk("Error", ex.Message);
                }
            }
            else
            {
                DialogBox.ShowOk("Error", "Failed to find SMS device");
            }
        }