As I said in the heading, this is my problem. Why can't I access the context in my class? Is there any way I could get the instance of the context in this class?
[Service(Exported = false), IntentFilter(new[] { "com.google.android.c2dm.intent.RECEIVE" })]
class MyGcmListenerService : GcmListenerService
{
public override void OnMessageReceived(string from, Bundle data)
{
string msg = data.GetString("message");
// the app breaks here
ShowPopup(msg);
Log.Info("GcmLstnrService", "From: " + from);
Log.Info("GcmLstnrService", "Msg: " + msg);
}
private void ShowPopup(string message)
{
// the app breaks here
AlertDialog.Builder builder = new AlertDialog.Builder(Application.Context);
AlertDialog dialog = builder.Create();
dialog.SetTitle("Push message");
dialog.SetMessage(message);
dialog.SetButton("OK", (sender, e) => { });
dialog.Show();
}
}
The reason that you do not have an Activity context available is this is within a service and there is no UI associated to it.
If you really want to show a dialog, you have two options:
Use a System-based alert:
var dialog = new AlertDialog.Builder(this).Create();
dialog.Window.SetType(Android.Views.WindowManagerTypes.SystemAlert);
dialog.SetTitle("Push message");
dialog.SetMessage(message);
dialog.SetButton("OK", (sender, e) => { });
dialog.Show();
Note: This type of alert gets displayed on top of EVERYTHING and thus requires that you add manifest permissions.
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
The other way is to create a new Activity
subclass and in its OnCreate
create your AlertDialog
and use this
as your context since it will be an Activity. Then when you want your GcmListenerService
to display a message create an instance of this Activity
.
Use Forms.Context
instead Application.Context
.
I resolved this issue with Xamarin.Forms and MessagingCenter.
Here's my service:
[Service(Exported = false), IntentFilter(new[] { "com.google.android.c2dm.intent.RECEIVE" })]
class MyGcmListenerService : GcmListenerService
{
public override void OnMessageReceived(string from, Bundle data)
{
string msg = data.GetString("message");
// send a string via Xamarin MessagingCenter
MessagingCenter.Send<object, string>(this, "ShowAlert", msg);
}
}
And here's part of my PCL App class constructor:
// subscribe to the messages
MessagingCenter.Subscribe<object, string>(this, "ShowAlert", (s, msg) =>
{
// run on UI thread
Device.BeginInvokeOnMainThread(() =>
{
MainPage.DisplayAlert("Push message", msg, "OK");
});
});