0

Xamarin forms android application targeting api level 26 crashes with java.lang.IllegalStateException: Not allowed to start service Intent when receiving notification in background.

From Notification in oreo, what i understood is only during the app whitelisted duration only the app can make services. How to properly handle this situvation, i call and update my databases when i receive notification, how can i properly manage this.

One answer shows

if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N_MR1) {
        context.startForegroundService(new Intent(context, ServedService.class));
    } else {
        context.startService(new Intent(context, ServedService.class));
    }

But how can i implement this in a xamarin forms application for properly receiving push notifications.

George Thomas
  • 4,566
  • 5
  • 30
  • 65
  • Do you want to implement the code in Xamarin forms? If so, where do you want to call it? PCl? Android Plarform? – Robbit Jan 12 '18 at 07:01

1 Answers1

1

Starting from Android 8.0 (API level 26) .

The startService() method now throws an IllegalStateException if an app targeting Android 8.0 tries to use that method in a situation when it isn't permitted to create background services.

Read Android 8.0 Behavior Changes for way around to make work in android Oreo. You can use Job scheduler on notification receive to perform database task .

ADM
  • 20,406
  • 11
  • 52
  • 83