I am using a service for an AlarmManager which every 30 minutes triggers my "Service Receiver" which is used for scheduling notification.For the purpose of testing I have lowered this interval.My app and its service runs perfectly fine and is able to deliver Notifications.However after a reboot my Service stops abruptly and message is delivered saying "App Stopped Working".
I have written this code inspired from here, here and many other SO questions and have been trying to solve this issue for the past 12 Hours.I suspect the issue is in RebootReceiver or my Manifest. but then the code is simple and works for others.
My App Manifest :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="Namaz_Pro.Namaz_Pro" android:versionCode="1" android:versionName="1.0" android:installLocation="preferExternal">
<uses-sdk android:minSdkVersion="17" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application android:label="Namaz Pro">
<receiver android:name=".ServiceReceiver" />
<receiver android:name=".NotificationPublisher" />
<receiver android:name=".RebootReceiver"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<category android:name="android.intent.category.DEFAULT" />
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON"/>
</intent-filter>
</receiver>
<service android:name=".NotificationService"/>
</application>
</manifest>
My RebootReceiver :
[BroadcastReceiver]
class RebootReceiver : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
if ("android.intent.action.BOOT_COMPLETED".Equals(intent.Action))
{
Intent startServiceIntent = new Intent(context, typeof(NotificationService));
context.StartService(startServiceIntent);
}
}
}
My Service :
[Service]
class NotificationService : Android.App.Service
{
public override StartCommandResult OnStartCommand(Intent intent, [GeneratedEnum] StartCommandFlags flags, int startId)
{
var alarmIntent = new Intent(this, typeof(ServiceReceiver));
var pending = PendingIntent.GetBroadcast(this, 1234567, alarmIntent, PendingIntentFlags.UpdateCurrent);
var alarmManager = GetSystemService(AlarmService) as AlarmManager;
alarmManager.SetRepeating(AlarmType.RtcWakeup, App_Code.helperMethods.CurrentTimeMillis() + 10000, 60000 , pending);
return StartCommandResult.NotSticky;
}
public override void OnDestroy()
{
base.OnDestroy();
//Log.Debug(TAG, "Simple Service Destroyed at {0}.", DateTime.UtcNow);
}
public override IBinder OnBind(Intent intent)
{
return null;
}
}
My Service Receiver :
[BroadcastReceiver]
public class ServiceReceiver : BroadcastReceiver
{
notificationHelper.setUpNotifications(Android.App.Application.Context);
}
}