{"id":24959,"date":"2016-03-22T10:43:14","date_gmt":"2016-03-22T17:43:14","guid":{"rendered":"https:\/\/blog.xamarin.com\/?p=24959"},"modified":"2016-03-22T10:43:14","modified_gmt":"2016-03-22T17:43:14","slug":"cross-platform-messaging-for-ios-android-and-windows","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/xamarin\/cross-platform-messaging-for-ios-android-and-windows\/","title":{"rendered":"Cross-Platform Messaging for iOS, Android, and Windows"},"content":{"rendered":"<p>\t\t\t\t<img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/Messaging.png\" alt=\"Cross-platform messaging for iOS, Android, and UWP.\" width=\"128\" height=\"128\" class=\"alignright size-full wp-image-24970\" \/>Messaging is a central component to the mobile experience and it&#8217;s not unusual for apps to want to take advantage of this in, for example, an employee directory app with employee&#8217;s contact information or a shopping application that wants to make it easy for customers to get in touch. However, these functionalities are all specific to each platform, making the code difficult to share. In this blog post, you&#8217;ll learn how to send texts, make phone calls, and send emails with a common API for iOS, Android, and Windows using the <a href=\"https:\/\/www.nuget.org\/packages\/Xam.Plugins.Messaging\/\"><em>Messaging Plugin for Xamarin and Windows<\/em><\/a>.<\/p>\n<h2>Introducing the <em>Messaging Plugin for Xamarin and Windows<\/em><\/h2>\n<p><a href=\"https:\/\/xamarin.com\/plugins\">Plugins for Xamarin<\/a> expose different aspects of platform-specific functionality via an API that can be called from a Portable Class Library (PCL) or Shared Project, such as <a href=\"https:\/\/components.xamarin.com\/view\/mediaplugin\">taking a photo<\/a>, <a href=\"https:\/\/blog.xamarin.com\/geolocation-for-ios-android-and-windows-made-easy\/\">using device geolocation<\/a>, or <a href=\"https:\/\/components.xamarin.com\/view\/SettingsPlugin\">storing app settings<\/a>. The <em>Messaging Plugin for Xamarin and Windows<\/em> allows you to send SMS messages, call a phone number, and send emails, allowing you to share even more code, increase developer productivity, and reduce costs.<\/p>\n<p>Plugin functionality can be accessed via the <code>MessagingPlugin<\/code> class, with separate properties such as <code>SmsMessenger<\/code>, <code>PhoneDialer<\/code>, and <code>EmailMessenger<\/code> that expose specific functionality.<\/p>\n<h4>Sending a Text<\/h4>\n<p>To send a text message, first check if the device is capable of sending SMS messages with the <code>SmsMessenger.CanSendSms<\/code> boolean property. If this device capability is available, you can use the <code>SmsMessenger.SendSms<\/code> method, with the first parameter being the phone number and the second the SMS message to send. Note that the SMS will not send automatically&mdash;the OS requires the user to actually press the <em>Send<\/em> button to avoid abuse. Instead, the native user interface for sending SMS for that particular platform will be displayed.<\/p>\n<pre class=\"lang:csharp decode:true\">\nvar smsMessenger = MessagingPlugin.SmsMessenger;\nif (smsMessenger.CanSendSms)\n   smsMessenger.SendSms(\"+272193343499\", \"Hello from your friends at Xamarin!\");\n<\/pre>\n<h4>Making a Phone Call<\/h4>\n<p>Just as we did for sending a text, we must first check if the device is capable of making a phone call with the <code>PhoneDialer.CanMakePhoneCall<\/code> property. You can then use the <code>PhoneDialer.MakePhoneCall<\/code> method, which will launch the native phone app and begin calling the phone number passed in as a parameter.<\/p>\n<pre class=\"lang:csharp decode:true\">\n\/\/ Make Phone Call\nvar phoneCallTask = MessagingPlugin.PhoneDialer;\nif (phoneCallTask.CanMakePhoneCall) \n    phoneCallTask.MakePhoneCall(\"+272193343499\");\n<\/pre>\n<h4>Sending an Email<\/h4>\n<p>Just as we did for sending SMS messages and making phone calls, we must first check that the device supports email with the <code>EmailMessenger.CanSendEmail<\/code> property. If the device supports it, you can send an email using the <code>EmailMessenger.SendEmail<\/code> method, or use the <code>EmailMessageBuilder<\/code> class&#8217; fluent interface to construct more complex email messages.<\/p>\n<pre class=\"lang:csharp decode:true\">\nvar emailTask = MessagingPlugin.EmailMessenger;\nif (emailTask.CanSendEmail)\n{\n    \/\/ Send simple e-mail to single receiver without attachments, CC, or BCC.\n    emailTask.SendEmail(\"plugins@xamarin.com\", \"Xamarin Messaging Plugin\", \"Hello from your friends at Xamarin!\");\n\n    \/\/ Send a more complex email with the EmailMessageBuilder fluent interface.\n    var email = new EmailMessageBuilder()\n      .To(\"plugins@xamarin.com\")\n      .Cc(\"plugins.cc@xamarin.com\")\n      .Bcc(new[] { \"plugins.bcc@xamarin.com\", \"plugins.bcc2@xamarin.com\" })\n      .Subject(\"Xamarin Messaging Plugin\")\n      .Body(\"Hello from your friends at Xamarin!\")\n      .Build();\n\n    emailTask.SendEmail(email);\n}   \n<\/pre>\n<p>The <em>Messaging Plugin for Xamarin and Windows<\/em> also supports more complex email scenarios, such as sending an email with an HTML body or sending an attachment.<\/p>\n<pre class=\"lang:csharp decode:true\">\n\/\/ Construct HTML email (iOS and Android only)\nvar email = new EmailMessageBuilder()\n  .To(\"plugins@xamarin.com\")\n  .Subject(\"Xamarin\")\n  .BodyAsHtml(\"Hello from your friends at <b>Xamarin<\/b>!\")\n  .Build();\n\n\/\/ Construct email with attachment on Android.\nvar email = new EmailMessageBuilder()\n  .To(\"plugins@xamarin.com\")\n  .Subject(\"Xamarin Messaging Plugin\")\n  .Body(\"Hello from your friends at Xamarin!\")\n  .WithAttachment(\"\/storage\/emulated\/0\/Android\/data\/MyApp\/files\/Pictures\/temp\/IMG_20141224_114954.jpg\");\n  .Build();\n<\/pre>\n<h2>Conclusion<\/h2>\n<p>Plugins for Xamarin and Windows allow you to share more code and save even more time when working with certain pieces of platform-specific functionality, such as taking a photo, using device geolocation, or storing app settings. In this blog post, we covered how to send SMS messages, make phone calls, and send emails using the the <em>Messaging Plugin for Xamarin and Windows<\/em>. For more information, check out the <a href=\"https:\/\/components.xamarin.com\/view\/xam.plugins.messaging\">documentation and sample app<\/a>.\t\t<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Messaging is a central component to the mobile experience and it&#8217;s not unusual for apps to want to take advantage of this in, for example, an employee directory app with employee&#8217;s contact information or a shopping application that wants to make it easy for customers to get in touch. However, these functionalities are all specific [&hellip;]<\/p>\n","protected":false},"author":546,"featured_media":24970,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2],"tags":[4],"class_list":["post-24959","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-developers","tag-xamarin-platform"],"acf":[],"blog_post_summary":"<p>Messaging is a central component to the mobile experience and it&#8217;s not unusual for apps to want to take advantage of this in, for example, an employee directory app with employee&#8217;s contact information or a shopping application that wants to make it easy for customers to get in touch. However, these functionalities are all specific [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/24959","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/users\/546"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/comments?post=24959"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/24959\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/media?parent=24959"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/categories?post=24959"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/tags?post=24959"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}