I am new to xamarin and Android so this question might be simple. However, I am trying to create a Broadcast receiver and an Intent Service following this article
I never seem to hit any of my debug points in the receiver or the intent service. I am not sure what is it that I am missing. This is my manifest:
<application android:label="gcc">
<receiver android:name=".TrialBR" android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="gcc.gcc" />
</intent-filter>
</receiver>
<service android:name=".MyIntentService" />
</application>
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<permission android:name="gcc.gcc.permission.C2D_MESSAGE" android:protectionLevel="signature" />
<uses-permission android:name="gcc.gcc.permission.C2D_MESSAGE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
I am not entirely sure about the package name. I am just following whatever is the package name in the android settings of the project. This is my receiver:
[BroadcastReceiver]
[IntentFilter(new[] { Android.Content.Intent.ActionBootCompleted })]
public class TrialBR : BroadcastReceiver
{
public override void OnReceive (Context context, Intent intent)
{
MyIntentService.RunIntentInService(context, intent);
SetResult(Result.Ok, null, null);
Toast.MakeText (context, "Received intent!", ToastLength.Short).Show ();
}
}
and this is my intent service:
[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();
}
}
}
void HandleRegistration (Context context, Intent intent)
{
string senders = "123456789";
Intent intent1 = new Intent("com.google.android.c2dm.intent.REGISTER");
intent1.SetPackage("com.google.android.gsf");
intent1.PutExtra("app", PendingIntent.GetBroadcast(context, 0, new Intent(), 0));
intent1.PutExtra("sender", senders);
context.StartService(intent1);
}
void HandleMessage (Context context, Intent intent)
{
string registrationId = intent.GetStringExtra("registration_id");
string error = intent.GetStringExtra("error");
string unregistration = intent.GetStringExtra("unregistered");
Console.WriteLine ("Registration ID: " + registrationId);
}
}
I would really appreciate if someone can point me in the right direction of what it is that I am doing wrong. I will really appreciate it.