Reputation: 3
G'day,
My question is fairly straight forward:
Should I expect SMSManager.Default.SendTextMessage()
to automatically add the sent message to the message store, or do I need to add it myself? If I need to add it myself, should I do that in the BroadcastReceiver
or in the dependency service after sending?
For context:
I'm trying to recreate an SMS app as practice for myself.
I've managed to create a dependency service that gets messages from the message store and have a BroadcastReceiver
to notify when a message is received.
My issue arises when I try to use the below code to send a message.
public Task SendSMS(string message, string destination)
{
piSent = PendingIntent.GetBroadcast(context, destination.GetHashCode(), new Intent("SMS_SENT"), PendingIntentFlags.UpdateCurrent);
//var piDelivered = PendingIntent.GetBroadcast(context, 0, new Intent("SMS_DELIVERED"), 0);
try
{
manager.SendTextMessage(destination, null, message, piSent, null);
}
catch (Exception e)
{
Toast.Make(e.Message, ToastDuration.Long).Show();
}
return Task.CompletedTask;
}
So the BroadcastReceiver
for SMS_SENT gets the intent and has a Result.Ok
code, but the message itself isn't added to the message store. I also haven't live tested this to see if the message actually sends.
This is the code I use to get all messages from the store.
public async Task<ObservableCollection<Message>> GetMessages(string thread_id)
{
var inbox = App.Current.Resources["smsInbox"] as string;
var cols = new string[] { "address", "_id", "thread_id", "body", "date" };
var projection = "thread_id=?";
string[] selection = { thread_id };
var results = await ExecuteQuery(inbox, cols, projection, selection);
var messages = new ObservableCollection<Message>();
foreach(var result in results)
{
messages.Add(new Message
{
Address = result[0],
Id = result[1],
ThreadId = result[2],
Body = result[3],
Date = DateTimeOffset.FromUnixTimeMilliseconds(long.Parse(result[4]))
});
}
return messages;
}
private async Task<List<List<string>>> ExecuteQuery(string uriString, string[] cols, string projection, string[] projectionValue = null)
{
await init();
var queryData = new List<List<string>>();
var uri = Android.Net.Uri.Parse(uriString);
var cursor = context.ContentResolver.Query(uri, cols, projection, projectionValue, null);
if (cursor.MoveToFirst())
{
do
{
var tuple = new List<string>(cursor.ColumnCount);
for (int i = 0; i < cursor.ColumnCount; i++)
{
tuple.Add(cursor.GetString(i));
}
queryData.Add(tuple);
} while (cursor.MoveToNext());
}
cursor.Close();
return queryData;
}
And the broadcastreceiver
[BroadcastReceiver(Exported = true)]
public class MessageSentReceiver : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
switch ((int)ResultCode)
{
case (int)Result.Ok:
Toast.Make("SMS Sent", ToastDuration.Short).Show();
break;
case (int)SmsResultError.GenericFailure:
Toast.Make("Generic Error", ToastDuration.Short).Show();
break;
case (int)SmsResultError.NoService:
Toast.Make("No Service", ToastDuration.Short).Show();
break;
case (int)SmsResultError.NullPdu:
Toast.Make("Null PDU", ToastDuration.Short).Show();
break;
case (int)SmsResultError.RadioOff:
Toast.Make("Radio Off", ToastDuration.Short).Show();
break;
default:
Toast.Make("Default Message", ToastDuration.Short).Show();
break;
}
}
}
Thanks in advance, and apologies if this isn't enough information to answer the question.
Upvotes: 0
Views: 136
Reputation: 3
Well, after double-triple checking I wasn't making a fool of myself, turns out I was.
The messages are saved in the sent table, so weren't showing up when I pulled from the inbox.
Lesson learnt!
I guess I'll leave this here to save someone else that's having a brain fade.
Upvotes: 0