1

This is my first post on this forum so any tips on how to make the question more understandable/readable and so on is appreciated.

What am I doing?

I am making my first app using Xamarin Forms, and I have two projects, PCL (Portable Class Library) and Android. My Android project receives incoming SMS from a specific number and saves it to a string. What I am trying to achieve is that by using MessagingCenter, send the string from my Android project to my PCL.

My problem:

I have seen a lot of threads regarding this, but there is something I am missing. And because I am new to this forum I can't write comments so I have to create my own question. Let me show you some of the code. (parsedsms is the string containing the SMS)

SmsReceiver.cs (In my Android project)

MessagingCenter.Send<SmsReceiver, string> (this, "ParsedSmsReceived", parsedsms);

MissionPage.xaml.cs (In my PCL project)

MessagingCenter.Subscribe<SmsReceiver, string> (this, "ParsedSmsReceived",
(sender, arg) => 
{ 
    string message = arg; 
});

This is an example that I found on another thread here on Stackoverflow. My problem is that parsedsms can't be accessed from the PCL. How can I access the SmsReceiver class from my PCL? You can't add a reference from PCL (because it is a library I guess) to Android, only the other way around.

Razze
  • 11
  • 1
  • 7
  • 2
    You should use the Xamarin.Forms DependencyService: https://developer.xamarin.com/guides/xamarin-forms/application-fundamentals/dependency-service/introduction/ to reach into the android project. I dont think you need the MessagingCenter (I would always try to avoid using it, because I think its not performant, might result in memoryleaks and also indicates that your whole approach might be going the wrong way) Also you should post the SmsReceiver.cs class and explain how the pcl and the android project work together in this case – Csharpest Mar 09 '18 at 13:49
  • Wow, that was fast. Alright, I will give DependencyService a go and see if I can figure it out. I'll post an update when I've tried it. The reason that I didn't add the whole class is that there is a lot of code that wouldn't be necessary for understanding the question. I will add it later if need be. – Razze Mar 09 '18 at 14:00
  • Yeah, just from a basic perspective, I think your code should be mainly in the pcl, and you would there call something like "DepencyService.Get().ReceiveSms();" and implement the ISmsReceiver inside the non-pcl projects (android, ios, uwp, whatever) – Csharpest Mar 09 '18 at 14:15
  • This should work. You can use Object instead of SmsReceiver in the signature of the Message to avoid the unknown type problem. – Jason Mar 09 '18 at 14:37
  • @Jason, you are absolutely right, it did work! Earlier today i tried the same thing but I realize I used "object" instead of "Object". Duh... I will edit the question to include the solution when I get the time. Anyway, thank you for your help! – Razze Mar 09 '18 at 15:26
  • @Razze Add your answer in the answer box below, **not as part of the question**. Follow the Q & A format. You can then, after some time has passed, accept your own answer. –  Mar 09 '18 at 19:20
  • @Razze Also, please see ["Is it okay to add SOLVED to the title of a question](https://meta.stackexchange.com/questions/116101/is-it-ok-to-add-solved-to-the-title-of-a-question). –  Mar 09 '18 at 19:28
  • @Amy, I see, that makes sense. Hopefully it is correct now. Thanks! – Razze Mar 10 '18 at 18:26

2 Answers2

3

As @Jason wrote in the comments, the solution is to use Object instead of SmsReceiver like this:

SmsReceiver.cs

MessagingCenter.Send<Object, string> (this, "ParsedSmsReceived", parsedsms);

MissionPage.xaml.cs

MessagingCenter.Subscribe<Object, string> (this, "ParsedSmsReceived",
(sender, arg) => 
{ 
    string message = arg; 
});

This works fine, but if MessagingCenter actually is the right way to go is another question. As @Csharpest commented using DependencyService might be a better solution.

0

The interface makes it possible to better manage the message.

ISmsReceiver.cs in PCL

public interface ISmsReceiver {}

SmsReceiver.cs in Android

[assembly: Dependency(typeof(SmsReceiver ))]
namespace App1.MobileApp.Droid
{
    public class SmsReceiver : BroadcastReceiver, ISmsReceiver
    {
        public override void OnReceive(Context context, Intent intent)
        {
            MessagingCenter.Send<ISmsReceiver, string> (this, "ParsedSmsReceived", parsedsms);
        }
    }
}

MissionPage.xaml.cs in PCL

MessagingCenter.Subscribe<ISmsReceiver, string> (this, "ParsedSmsReceived",
(sender, arg) => 
{ 
    string message = arg; 
});
lsaudon
  • 1,263
  • 11
  • 24