I have followed the Xamarin documentation here, http://docs.xamarin.com/guides/cross-platform/application_fundamentals/notifications/android/remote_notifications_in_android/, and my app still does not receive notifications. In my MainActivity, I create an intent to register for CCM notifications, but nothing happens . Just a note, I have been heavily engaged w/Xamarin.IOS and am now getting into the Android platform. Any assistance would be greatly appreciated. Thanks in advance to all that assist. Below is code from each of the required classes (as specified by documentation) to enable remote notifications:
********Permissions in the AndroidManafest.xml ' <?xml version="1.0" encoding="utf-8"?> '
********Intent to Register for GCM in MainActivity `string senders = "mysenderid"; Intent intent = new Intent("com.google.android.c2dm.intent.REGISTER");
intent.SetPackage("com.google.android.gsf");
intent.PutExtra("app", PendingIntent.GetBroadcast(this, 0, new Intent(), 0));
intent.PutExtra("sender", senders);
this.StartService(intent);`
''
*******Broadcast Receiver Implementation '[BroadcastReceiver(Permission= "com.google.android.c2dm.permission.SEND")] [IntentFilter(new string[] { "com.google.android.c2dm.intent.RECEIVE" }, Categories = new string[] {"@mypackagename@" })] [IntentFilter(new string[] { "com.google.android.c2dm.intent.REGISTRATION" }, Categories = new string[] {"@mypackagename@" })] [IntentFilter(new string[] { "com.google.android.gcm.intent.RETRY" }, Categories = new string[] { "@mypackagename@"})]
public class MyGCMBroadcastReceiver : BroadcastReceiver
{
const string TAG = "PushHandlerBroadcastReceiver";
public override void OnReceive(Context context, Intent intent)
{
MyIntentService.RunIntentInService(context, intent);
SetResult(Result.Ok, null, null);
}
}
[BroadcastReceiver]
[IntentFilter(new[] { Android.Content.Intent.ActionBootCompleted })]
public class MyGCMBootReceiver : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
MyIntentService.RunIntentInService(context, intent);
SetResult(Result.Ok, null, null);
}
}`
*****Intent Service 'public class MyIntentService : IntentService { static PowerManager.WakeLock sWakeLock; static object LOCK = new object();
public static void RunIntentInService(Context context, Intent intent)
{
lock (LOCK)
{
if (sWakeLock == null)
{
// This is called from BroadcastReceiver, there is no init.
var pm = PowerManager.FromContext(context);
sWakeLock = pm.NewWakeLock(
WakeLockFlags.Partial, "My WakeLock Tag");
}
}
sWakeLock.Acquire();
intent.SetClass(context, typeof(MyIntentService));
context.StartService(intent);
}
protected override void OnHandleIntent(Intent intent)
{
try
{
Context context = this.ApplicationContext;
string action = intent.Action;
if (action.Equals("com.google.android.c2dm.intent.REGISTRATION"))
{
HandleRegistration(context, intent);
}
else if (action.Equals("com.google.android.c2dm.intent.RECEIVE"))
{
HandleMessage(context, intent);
}
}
finally
{
lock (LOCK)
{
//Sanity check for null as this is a public method
if (sWakeLock != null)
sWakeLock.Release();
}
}
}
private void HandleRegistration(Context context, Intent intent) {
String registrationId = intent.GetStringExtra("registration_id");
String error = intent.GetStringExtra("error");
String unregistered = intent.GetStringExtra("unregistered");
//Store the Registration ID
Device device = new Device ();
device.DeviceID = registrationId;
device.Type = "android";
//Service call to store device registration id to database
}
private void HandleMessage(Context context, Intent intent) {
String action = intent.GetStringExtra("C2DM_DATA_ACTION");
// Do something in the program to let the User know the notification has been received.
}
}`