cjlotz / Xamarin.Plugins

Cross platform Xamarin Plugins
MIT License
113 stars 56 forks source link

Is it possible to get the status result of a send operation? #105

Open primehalo opened 6 years ago

primehalo commented 6 years ago

I don't see any way to check the status of sending an SMS. If one exists, can you show how to use it? If one doesn't exist, could you add it?

When working directly on iOS with Objective C I would use this:

messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
    if (result == MessageComposeResultCancelled) {
        Log(@"Message cancelled");
    } else if (result == MessageComposeResultSent) {
        Log(@"Message sent");
    } else {
        Log(@"Message failed");
    }
}

When working direction on Android with Java I would use this:

import android.telephony.SmsManager;
getActivity().registerReceiver(new BroadcastReceiver() {
    @Override
    public void onReceive(Context ctx, Intent intent) {
        int resultCode = getResultCode();
        switch (resultCode) {
            case Activity.RESULT_OK:
                break;
            case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                break;
            case SmsManager.RESULT_ERROR_NO_SERVICE:
                break;
            case SmsManager.RESULT_ERROR_NULL_PDU:
                break;
            case SmsManager.RESULT_ERROR_RADIO_OFF:
                break;
        }
    }
}, new IntentFilter(intentAction));

Is there anything like this in this plugin?

Abesoddy commented 6 years ago

Hello,

I am in the same situation, it should be able to have the result of the operation after sending sms for example.

Do you have an answer to bring or not at all?

primehalo commented 6 years ago

In order to have this ability I couldn't use this plugin, I had to write my own dependency service. Take a look at the code in my questions on Xamarin Forums: iOS: https://forums.xamarin.com/discussion/128843/getting-a-xam-plugins-messaging-sent-message-finished-result

Android: https://forums.xamarin.com/discussion/129203/how-do-i-use-taskcompletionsource-to-return-a-result-from-a-pendingintent-broadcastreceiver

Abesoddy commented 6 years ago

Thank you very much for your answer, you have a sample to share to see more clearly or not at all? If you can not share code, I will try to do my own service ...

primehalo commented 6 years ago

Those two forum posts I linked to in my previous response have code. This one shows my interface and the iOS code I started out with: https://forums.xamarin.com/discussion/comment/337199/#Comment_337199

Then this one shows how to make the iOS version work: https://forums.xamarin.com/discussion/comment/339137/#Comment_339137

This one shows me trying to get the Android version working: https://forums.xamarin.com/discussion/129203/how-do-i-use-taskcompletionsource-to-return-a-result-from-a-pendingintent-broadcastreceiver#latest

And to actually get it working I generate a unique ID which I can pass to the pending intents so that they can use MessagingCenter to send a message when the BroadcastReceiver is finished:

var randomGenerator = new System.Random();
var uniqueId = (to ?? "") + ":" + (message ?? "") + DateTime.UtcNow + randomGenerator.Next(0, 999999).ToString();
var smsSentBroadcastReceiver = new SMSSentReceiver { UniqueId = uniqueId };
var smsDeliveredBroadcastReceiver = new SMSDeliveredReceiver { UniqueId = uniqueId };
var piSent = Android.App.PendingIntent.GetBroadcast(Android.App.Application.Context, 0, new Intent(MY_SMS_SENT_INTENT_ACTION), 0);
var piDelivered = Android.App.PendingIntent.GetBroadcast(Android.App.Application.Context, 0, new Intent(MY_SMS_DELIVERY_INTENT_ACTION), 0);
Android.App.Application.Context.RegisterReceiver(smsSentBroadcastReceiver, new IntentFilter(MY_SMS_SENT_INTENT_ACTION));
Android.App.Application.Context.RegisterReceiver(smsDeliveredBroadcastReceiver, new IntentFilter(MY_SMS_DELIVERY_INTENT_ACTION));
if (_smsManager == null)
{
    _smsManager = SmsManager.Default;
}
_smsManager.SendTextMessage(to, Models.Constants.VivaSMSFrom, message, piSent, piDelivered);
MessagingCenter.Unsubscribe<string, SmsResult>("MySmsAndroid", uniqueId);
MessagingCenter.Subscribe<string, SmsResult>("MySmsAndroid", uniqueId, (sender, smsResult) =>
{
    MessagingCenter.Unsubscribe<string, SmsResult>("MySmsAndroid", uniqueId);
    Models.Debug.KenLog("Setting the tcs result");
    tcs.SetResult(smsResult);
});