mercredi 13 mai 2015

Android SendMultipartTextMessage with pendingIntents with Extra values same for all parts

I am successfully sending multi-part (concatenated) messages through android using Xamarin Studio and the SMS.Telephony.SmsManager Android library.

To send a message I am doing the following:

var longMessage = "This is a cØncatenated message sent to you through an android.   This should appear as a single message.  Hopefully it's as easy as that.  It even has a function to break the message up. It probably took me longer to install Xamarin then it did to write the code and send the actual message";
var smsMgr = Android.Telephony.SmsManager.Default;
System.Collections.Generic.IList<string> parts = smsMgr.DivideMessage(longMessage);
IList<PendingIntent> pendingIntents = new List<PendingIntent>(parts.Count);
for (int i = 0; i < parts.Count; i++)
{
  var intent = new Intent(DeliveryIntentAction);
  intent.PutExtra("MessagePartText", parts[i]);
  intent.PutExtra("MessagePartId", i.ToString());
  PendingIntent pi = PendingIntent.GetBroadcast(this, 0, intent, 0);
  pendingIntents.Add(pi);
}

smsMgr.SendMultipartTextMessage("17057178131",null, parts, pendingIntents, null); 

I then have a receiver for the pending Intents that looks like this:

[BroadcastReceiver(Enabled = true)] //(Exported = true, Permission = "//receiver/@android:android.permission.SEND_SMS")]
[IntentFilter(new[] { DeliveryIntentAction }, Priority = int.MaxValue)]
public class SMSSentReceiver : BroadcastReceiver
{
  public override void OnReceive(Context context, Intent intent)
  {
    if (intent.GetStringExtra ("MessagePartId") != null)
      lbl.Text += " Sent Response " + intent.GetStringExtra ("MessagePartId") + System.Environment.NewLine;
...

However, each time OnRecieve is called, intent.GetStringExtra("MessagePartId") has a value of "0" and the MessagePartText is the first part, not the part number/text that belongs to the message part sent.

Can anyone see why this may be the case?

Thank you

Aucun commentaire:

Enregistrer un commentaire