0

I have problem with broadcast receiver in xamarin.android. Can't get it to work.

I have notification in my app working and I want to change some things in my app after I get notification (e.g. Toast message or change icon of a button) But it doesn't work. I don't know what am I doing wrong and I can't find solution because all the topics are Java related. I need something, event or broadcastreceiver to fire when user gets notification and then I want to do some stuff in my MainActivity.

So, this is the code.

BroadcastReceiver class:

  [BroadcastReceiver(Enabled = true, Exported = false)]
public class MyMessageReceiver : BroadcastReceiver
{
    public override void OnReceive(Context context, Intent intent)
    {
        bool messageReceived = intent.GetBooleanExtra("messageReceived", false);
    }
}

OnMessageReceived method:

 {
            base.OnMessageReceived(message);
            SendNotification(message.GetNotification().Body);

            LocalBroadcastManager broadcaster = LocalBroadcastManager.GetInstance(this);

            Intent intent = new Intent("message");
            intent.PutExtra("messageReceived", true);
            broadcaster.SendBroadcast(intent);
        }

And OnResume and OnPause methods:

 protected override void OnResume()
    {
        base.OnResume();

        LocalBroadcastManager.GetInstance(this).RegisterReceiver(myReceiver, new IntentFilter("message"));
        RegisterReceiver(myReceiver, new IntentFilter("message"));
    }

    protected override void OnPause()
    {
        base.OnPause();
        LocalBroadcastManager.GetInstance(this).UnregisterReceiver(myReceiver);
    }

I don't know how to receive that info for example in my OnCreate method in MainActivity? I tried with

messageReceived = Intent.GetBooleanExtra("messageReceived", false);
        if (messageReceived)
        {
            Toast.MakeText(this, "new notification", ToastLength.Long).Show();
        }

But that doesn't work, messageReceived is null.

KENdi
  • 7,576
  • 2
  • 16
  • 31
cordas
  • 53
  • 1
  • 1
  • 9

1 Answers1

0

I know it is a bit too late but better late than never :

After analysing the firebase messaging I have done a suitable workaround for this purpose :

When your application is in the background the handle intent method is called by default on receiving push notification :

 public override void HandleIntent(Intent p0)
{
  base.HandleIntent(intent);
  //Your code to know that you received a notification (backgrounnd)
  // Use shared preference for this 
}

Don't know how to use shared preferences check this.

For more information on how handle intent works check my answer out here.

When the application is in the foreground you can simply use on message received method as such :

 public override void OnMessageReceived(Context context, Intent intent)
{
     //Your code to know that you received a notification (backgrounnd)
     // Use shared preference for this 
}

Then, wherever you need to use this you can get a flag or count or whatever using shared preferences.

In case of any queries revert!

FreakyAli
  • 13,349
  • 3
  • 23
  • 63
  • But how do you use any of that in MainActivity? So, we have MainActivity active at this moment and we receive new message. How to change button text from "no messages" to "you have new messages" (in MainAcitivty) at that time when message is received? – cordas Jul 16 '18 at 16:00
  • For that check this discussion out https://forums.xamarin.com/discussion/1147/updating-activity-using-a-background-service – FreakyAli Jul 17 '18 at 05:25
  • Ok, so that discussion leads to using Broadcast Receivers which I already found out it's the best possible solution. Which leads to the original question I posted - why the broadcast receiver does not work the way I described in first post. – cordas Jul 17 '18 at 07:15