1

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);

    }
}
Community
  • 1
  • 1
Rahul Jha
  • 1,131
  • 1
  • 10
  • 25
  • 1
    The attributes `BroadcastReceiver` and `Service` are used from Xamarin to generate the App Manifest. They are a different way to configure your manifest (like the `Activity` attribute). I assume, that you have posted your manualy edited one. Can you check the generated one? Maybe your service configuration looks not as you expect. You can find the generated App Manifest in `/obj/Debug/android/Appmanifest.xml` – Sven-Michael Stübe May 28 '16 at 16:13
  • @Sven-MichaelStübe my Generated Manifest http://pastebin.com/QdjiPAyJ – Rahul Jha May 28 '16 at 16:29
  • You see? The receivers and the service are included twice. Maybe try to get rid of these dupes and try again. – Sven-Michael Stübe May 28 '16 at 16:32
  • @Sven-MichaelStübe but this is auto generated code at build time how can I edit ! Are you suggesting a manual edit? – Rahul Jha May 28 '16 at 16:34
  • no. either keep your manual one and remove the attributes, or configure it completely with attributes. They have properties and a `IntentFilterAttribute` exists as well. It's up to you. – Sven-Michael Stübe May 28 '16 at 16:36
  • have you check this plugin https://github.com/edsnider/LocalNotificationsPlugin and make the notifications to be sent again after restart according to this post? https://forums.xamarin.com/discussion/122868/broadcastreceiver-cant-start-service-on-boot-reboot – CDrosos May 22 '18 at 23:24

0 Answers0