sangram parmar sangram parmar - 1 year ago 73
C# Question

Not calling method OnMessageReceived in xamarin

I am implementing remote notification on android using xamarin.

I am doing POC using Walkthrough - Using Remote Notifications in Xamarin.Android

I am not getting notification on mobile, after registering mobile and send notification through sender using GCM.

Is there any mistake in code? or Can I track my notification to get detail why it is not come in mobile?


[Service(Exported = false), IntentFilter(new[] { "" })]
public class MyGcmListenerService : GcmListenerService
public override void OnMessageReceived(string from, Bundle data)
var message = data.GetString("message");
Log.Debug("MyGcmListenerService", "From: " + from);
Log.Debug("MyGcmListenerService", "Message: " + message);


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="" package="com.yourcompany.LeaveApplication" android:installLocation="auto" android:versionCode="1" android:versionName="1.0">
<uses-sdk android:minSdkVersion="15" />

<uses-permission android:name="" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.yourcompany.LeaveApplication.permission.C2D_MESSAGE" />
<permission android:name="com.yourcompany.LeaveApplication.permission.C2D_MESSAGE" android:protectionLevel="signature" />
<application android:label="XamarinLeaveApp" >
<receiver android:name="" android:exported="true" android:permission="">
<action android:name="" />
<action android:name="" />
<category android:name="com.yourcompany.LeaveApplication" />


Message Sender's code

var jGcmData = new JObject();
var jData = new JObject();

jData.Add("message", MESSAGE);
jGcmData.Add("to", "/topics/global");
jGcmData.Add("data", jData);

var url = new Uri("");
using (var client = new HttpClient())
new MediaTypeWithQualityHeaderValue("application/json"));

"Authorization", "key=" + API_KEY);

new StringContent(jGcmData.ToString(), Encoding.Default, "application/json"))
.ContinueWith(response =>
var response1 = response;
// Console.WriteLine(response);
//Console.WriteLine("Message sent: check the client device notification tray.");


[Service(Exported = false)]
class RegistrationIntentService : IntentService
static object locker = new object();

public RegistrationIntentService() : base("RegistrationIntentService") { }

protected override void OnHandleIntent(Intent intent)
Log.Info("RegistrationIntentService", "Calling InstanceID.GetToken");
lock (locker)
var instanceID = InstanceID.GetInstance(Application.Context);

var token = instanceID.GetToken(
"<project number", GoogleCloudMessaging.InstanceIdScope, null);

Log.Info("RegistrationIntentService", "GCM Registration Token: " + token);
catch (Exception e)
Log.Debug("RegistrationIntentService", "Failed to get a registration token");

void SendRegistrationToAppServer(string token)
// Add custom implementation here as needed.

void Subscribe(string token)
var pubSub = GcmPubSub.GetInstance(Application.Context);
pubSub.Subscribe(token, "/topics/global", null);

Answer Source

in the RegistrationIntentService.cs file, you use Application.Context instead of this. It should be equivalent, but can you try?

Also, I do not see in your code the protected override void OnCreate method that registers the RegistrationIntentService intent. Didn't you paste it here or did you forget to implement it?