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.

8
  • 2
    You should use the Xamarin.Forms DependencyService: developer.xamarin.com/guides/xamarin-forms/… 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 Commented Mar 9, 2018 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. Commented Mar 9, 2018 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<ISmsReceiver>().ReceiveSms();" and implement the ISmsReceiver inside the non-pcl projects (android, ios, uwp, whatever) Commented Mar 9, 2018 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. Commented Mar 9, 2018 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! Commented Mar 9, 2018 at 15:26

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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; 
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.