Skip to content
This repository has been archived by the owner on May 15, 2024. It is now read-only.

Commit

Permalink
GH-383: Multiple sms recipients (#484)
Browse files Browse the repository at this point in the history
* GH-383: Multiple sms recipients (#415)

* Docs

* Added multiple Recipient capabilities

* Updated docs

* PR requests

* SMS docs and comments

* Fixed potential nullreferenceexception

* Updated SMS message as requested

* Updated Samples for SMS

* Updated sms docs via copying files to right directory.

* Updated label in sample

* Fixed comment slash

* Code cleanup.

* Update sample for iOS support

* Null checks for @Redth
  • Loading branch information
jamesmontemagno authored and Redth committed Aug 24, 2018
1 parent c10ccc1 commit 5def00a
Show file tree
Hide file tree
Showing 11 changed files with 61 additions and 50 deletions.
2 changes: 1 addition & 1 deletion Samples/Samples/View/SMSPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

<ScrollView>
<StackLayout Padding="12,0,12,12" Spacing="6">
<Label Text="Recipent (optional):" />
<Label Text="Recipents (optional, separate multiple with ',' or '*'):" />
<Entry Text="{Binding Recipient}" Keyboard="Telephone" />
<Label Text="Message Text (optional):" />
<Entry Text="{Binding MessageText}" />
Expand Down
2 changes: 1 addition & 1 deletion Samples/Samples/ViewModel/SmsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ async void OnSendSms()

try
{
var message = new SmsMessage(MessageText, Recipient);
var message = new SmsMessage(MessageText, Recipient.Split(',', '*'));
await Sms.ComposeAsync(message);
}
catch (FeatureNotSupportedException)
Expand Down
22 changes: 11 additions & 11 deletions Xamarin.Essentials/Sms/Sms.android.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Android.Content;
using Android.OS;
using Android.Provider;
Expand All @@ -9,12 +11,14 @@ namespace Xamarin.Essentials
{
public static partial class Sms
{
static readonly string smsRecipientSeparator = ";";

internal static bool IsComposeSupported
=> Platform.IsIntentSupported(CreateIntent("0000000000"));
=> Platform.IsIntentSupported(CreateIntent(null, new List<string> { "0000000000" }));

static Task PlatformComposeAsync(SmsMessage message)
{
var intent = CreateIntent(message)
var intent = CreateIntent(message?.Body, message?.Recipients)
.SetFlags(ActivityFlags.ClearTop)
.SetFlags(ActivityFlags.NewTask);

Expand All @@ -23,17 +27,13 @@ static Task PlatformComposeAsync(SmsMessage message)
return Task.FromResult(true);
}

static Intent CreateIntent(SmsMessage message)
=> CreateIntent(message?.Recipient, message?.Body);

static Intent CreateIntent(string recipient, string body = null)
static Intent CreateIntent(string body, List<string> recipients)
{
Intent intent = null;

body = body ?? string.Empty;
recipient = recipient ?? string.Empty;

if (string.IsNullOrWhiteSpace(recipient) && Platform.HasApiLevel(BuildVersionCodes.Kitkat))
if (Platform.HasApiLevel(BuildVersionCodes.Kitkat) && recipients.All(x => string.IsNullOrWhiteSpace(x)))
{
var packageName = Telephony.Sms.GetDefaultSmsPackage(Platform.AppContext);
if (!string.IsNullOrWhiteSpace(packageName))
Expand All @@ -53,9 +53,9 @@ static Intent CreateIntent(string recipient, string body = null)
if (!string.IsNullOrWhiteSpace(body))
intent.PutExtra("sms_body", body);

intent.PutExtra("address", recipient);
var recipienturi = string.Join(smsRecipientSeparator, recipients.Select(r => AndroidUri.Encode(r)));

var uri = AndroidUri.Parse("smsto:" + AndroidUri.Encode(recipient));
var uri = AndroidUri.Parse($"smsto:{recipienturi}");
intent.SetData(uri);

return intent;
Expand Down
7 changes: 4 additions & 3 deletions Xamarin.Essentials/Sms/Sms.ios.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Threading.Tasks;
using System.Linq;
using System.Threading.Tasks;
using MessageUI;

namespace Xamarin.Essentials
Expand All @@ -17,8 +18,8 @@ static Task PlatformComposeAsync(SmsMessage message)
var messageController = new MFMessageComposeViewController();
if (!string.IsNullOrWhiteSpace(message?.Body))
messageController.Body = message.Body;
if (!string.IsNullOrWhiteSpace(message?.Recipient))
messageController.Recipients = new[] { message.Recipient };

messageController.Recipients = message?.Recipients?.ToArray() ?? new string[] { };

// show the controller
var tcs = new TaskCompletionSource<bool>();
Expand Down
17 changes: 13 additions & 4 deletions Xamarin.Essentials/Sms/Sms.shared.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using System.Threading.Tasks;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Xamarin.Essentials
{
Expand All @@ -12,6 +15,12 @@ public static Task ComposeAsync(SmsMessage message)
if (!IsComposeSupported)
throw new FeatureNotSupportedException();

if (message == null)
message = new SmsMessage();

if (message?.Recipients == null)
message.Recipients = new List<string>();

return PlatformComposeAsync(message);
}
}
Expand All @@ -22,14 +31,14 @@ public SmsMessage()
{
}

public SmsMessage(string body, string recipient)
public SmsMessage(string body, IEnumerable<string> recipients)
{
Body = body;
Recipient = recipient;
Recipients = recipients?.Where(x => !string.IsNullOrWhiteSpace(x)).ToList();
}

public string Body { get; set; }

public string Recipient { get; set; }
public List<string> Recipients { get; set; }
}
}
5 changes: 3 additions & 2 deletions Xamarin.Essentials/Sms/Sms.uwp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ static Task PlatformComposeAsync(SmsMessage message)
var chat = new ChatMessage();
if (!string.IsNullOrWhiteSpace(message?.Body))
chat.Body = message.Body;
if (!string.IsNullOrWhiteSpace(message?.Recipient))
chat.Recipients.Add(message.Recipient);

foreach (var recipient in message?.Recipients)
chat.Recipients.Add(recipient);

return ChatMessageManager.ShowComposeSmsMessageAsync(chat).AsTask();
}
Expand Down
4 changes: 2 additions & 2 deletions docs/en/FrameworksIndex/xamarin-essentials-android.xml
Original file line number Diff line number Diff line change
Expand Up @@ -477,9 +477,9 @@
</Type>
<Type Name="Xamarin.Essentials.SmsMessage" Id="T:Xamarin.Essentials.SmsMessage">
<Member Id="M:Xamarin.Essentials.SmsMessage.#ctor" />
<Member Id="M:Xamarin.Essentials.SmsMessage.#ctor(System.String,System.String)" />
<Member Id="M:Xamarin.Essentials.SmsMessage.#ctor(System.String,System.Collections.Generic.IEnumerable{System.String})" />
<Member Id="P:Xamarin.Essentials.SmsMessage.Body" />
<Member Id="P:Xamarin.Essentials.SmsMessage.Recipient" />
<Member Id="P:Xamarin.Essentials.SmsMessage.Recipients" />
</Type>
<Type Name="Xamarin.Essentials.SpeakSettings" Id="T:Xamarin.Essentials.SpeakSettings">
<Member Id="M:Xamarin.Essentials.SpeakSettings.#ctor" />
Expand Down
4 changes: 2 additions & 2 deletions docs/en/FrameworksIndex/xamarin-essentials-ios.xml
Original file line number Diff line number Diff line change
Expand Up @@ -476,9 +476,9 @@
</Type>
<Type Name="Xamarin.Essentials.SmsMessage" Id="T:Xamarin.Essentials.SmsMessage">
<Member Id="M:Xamarin.Essentials.SmsMessage.#ctor" />
<Member Id="M:Xamarin.Essentials.SmsMessage.#ctor(System.String,System.String)" />
<Member Id="M:Xamarin.Essentials.SmsMessage.#ctor(System.String,System.Collections.Generic.IEnumerable{System.String})" />
<Member Id="P:Xamarin.Essentials.SmsMessage.Body" />
<Member Id="P:Xamarin.Essentials.SmsMessage.Recipient" />
<Member Id="P:Xamarin.Essentials.SmsMessage.Recipients" />
</Type>
<Type Name="Xamarin.Essentials.SpeakSettings" Id="T:Xamarin.Essentials.SpeakSettings">
<Member Id="M:Xamarin.Essentials.SpeakSettings.#ctor" />
Expand Down
4 changes: 2 additions & 2 deletions docs/en/FrameworksIndex/xamarin-essentials-uwp.xml
Original file line number Diff line number Diff line change
Expand Up @@ -473,9 +473,9 @@
</Type>
<Type Name="Xamarin.Essentials.SmsMessage" Id="T:Xamarin.Essentials.SmsMessage">
<Member Id="M:Xamarin.Essentials.SmsMessage.#ctor" />
<Member Id="M:Xamarin.Essentials.SmsMessage.#ctor(System.String,System.String)" />
<Member Id="M:Xamarin.Essentials.SmsMessage.#ctor(System.String,System.Collections.Generic.IEnumerable{System.String})" />
<Member Id="P:Xamarin.Essentials.SmsMessage.Body" />
<Member Id="P:Xamarin.Essentials.SmsMessage.Recipient" />
<Member Id="P:Xamarin.Essentials.SmsMessage.Recipients" />
</Type>
<Type Name="Xamarin.Essentials.SpeakSettings" Id="T:Xamarin.Essentials.SpeakSettings">
<Member Id="M:Xamarin.Essentials.SpeakSettings.#ctor" />
Expand Down
4 changes: 2 additions & 2 deletions docs/en/FrameworksIndex/xamarin-essentials.xml
Original file line number Diff line number Diff line change
Expand Up @@ -473,9 +473,9 @@
</Type>
<Type Name="Xamarin.Essentials.SmsMessage" Id="T:Xamarin.Essentials.SmsMessage">
<Member Id="M:Xamarin.Essentials.SmsMessage.#ctor" />
<Member Id="M:Xamarin.Essentials.SmsMessage.#ctor(System.String,System.String)" />
<Member Id="M:Xamarin.Essentials.SmsMessage.#ctor(System.String,System.Collections.Generic.IEnumerable{System.String})" />
<Member Id="P:Xamarin.Essentials.SmsMessage.Body" />
<Member Id="P:Xamarin.Essentials.SmsMessage.Recipient" />
<Member Id="P:Xamarin.Essentials.SmsMessage.Recipients" />
</Type>
<Type Name="Xamarin.Essentials.SpeakSettings" Id="T:Xamarin.Essentials.SpeakSettings">
<Member Id="M:Xamarin.Essentials.SpeakSettings.#ctor" />
Expand Down
40 changes: 20 additions & 20 deletions docs/en/Xamarin.Essentials/SmsMessage.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,33 +21,33 @@
<MemberSignature Language="DocId" Value="M:Xamarin.Essentials.SmsMessage.#ctor" />
<MemberType>Constructor</MemberType>
<AssemblyInfo>
<AssemblyVersion>1.0.0.0</AssemblyVersion>
<AssemblyName>Xamarin.Essentials</AssemblyName>
<AssemblyVersion>1.0.0.0</AssemblyVersion>
</AssemblyInfo>
<Parameters />
<Docs>
<summary>Creates a new instance of SmsMessage.</summary>
<remarks></remarks>
<param name="body">Content of the message</param>
<param name="recipients">Recipients to receive the message.</param>
</Docs>
</Member>
<Member MemberName=".ctor">
<MemberSignature Language="C#" Value="public SmsMessage (string body, string recipient);" />
<MemberSignature Language="ILAsm" Value=".method public hidebysig specialname rtspecialname instance void .ctor(string body, string recipient) cil managed" />
<MemberSignature Language="DocId" Value="M:Xamarin.Essentials.SmsMessage.#ctor(System.String,System.String)" />
<MemberSignature Language="C#" Value="public SmsMessage (string body, System.Collections.Generic.IEnumerable&lt;string&gt; recipients);" />
<MemberSignature Language="ILAsm" Value=".method public hidebysig specialname rtspecialname instance void .ctor(string body, class System.Collections.Generic.IEnumerable`1&lt;string&gt; recipients) cil managed" />
<MemberSignature Language="DocId" Value="M:Xamarin.Essentials.SmsMessage.#ctor(System.String,System.Collections.Generic.IEnumerable{System.String})" />
<MemberType>Constructor</MemberType>
<AssemblyInfo>
<AssemblyVersion>1.0.0.0</AssemblyVersion>
<AssemblyName>Xamarin.Essentials</AssemblyName>
<AssemblyVersion>1.0.0.0</AssemblyVersion>
</AssemblyInfo>
<Parameters>
<Parameter Name="body" Type="System.String" />
<Parameter Name="recipient" Type="System.String" />
<Parameter Name="recipients" Type="System.Collections.Generic.IEnumerable&lt;System.String&gt;" />
</Parameters>
<Docs>
<param name="body">The body of the message.</param>
<param name="recipient">The recipient of the message.</param>
<summary>Creates a new instance of SmsMessage with the specified body and recipient.</summary>
<remarks></remarks>
<param name="body">Content of the message</param>
<param name="recipients">Recipients to receive the message.</param>
<summary>Creates a new instance of SmsMessage</summary>
<remarks><para /></remarks>
</Docs>
</Member>
<Member MemberName="Body">
Expand All @@ -63,25 +63,25 @@
<ReturnType>System.String</ReturnType>
</ReturnValue>
<Docs>
<summary>Gets or sets the body of the message.</summary>
<summary>Gets the body of the message.</summary>
<value></value>
<remarks></remarks>
</Docs>
</Member>
<Member MemberName="Recipient">
<MemberSignature Language="C#" Value="public string Recipient { get; set; }" />
<MemberSignature Language="ILAsm" Value=".property instance string Recipient" />
<MemberSignature Language="DocId" Value="P:Xamarin.Essentials.SmsMessage.Recipient" />
<Member MemberName="Recipients">
<MemberSignature Language="C#" Value="public System.Collections.Generic.List&lt;string&gt; Recipients { get; set; }" />
<MemberSignature Language="ILAsm" Value=".property instance class System.Collections.Generic.List`1&lt;string&gt; Recipients" />
<MemberSignature Language="DocId" Value="P:Xamarin.Essentials.SmsMessage.Recipients" />
<MemberType>Property</MemberType>
<AssemblyInfo>
<AssemblyVersion>1.0.0.0</AssemblyVersion>
<AssemblyName>Xamarin.Essentials</AssemblyName>
<AssemblyVersion>1.0.0.0</AssemblyVersion>
</AssemblyInfo>
<ReturnValue>
<ReturnType>System.String</ReturnType>
<ReturnType>System.Collections.Generic.List&lt;System.String&gt;</ReturnType>
</ReturnValue>
<Docs>
<summary>Gets or sets the recipient of the message.</summary>
<summary>Gets the recipient of the message.</summary>
<value></value>
<remarks></remarks>
</Docs>
Expand Down

0 comments on commit 5def00a

Please sign in to comment.