How to call C# method from android service in background

Hi. I try to call my Unity C# method from an android service.

My service is running (i can see it through LogCat) and i user UnitySendMessage:
UnityPlayer.UnitySendMessage(“Main Camera”, “TestMessage”, “Some string”);

This works perfectly if my app is active. But if my app is running in background UnitySendMessage doesn’t work, C# code isn’t executed.

That’s not possible at all. When your Unity app / activity is not in the foreground, it’s not running at all. Unity is a foreground application. So you can not make it run in the background. That’s why you have a background service.

A background service should not start any activities on it’s own. Such a behaviour is very intrusive. Such an app on iOS would most likely not pass the review ^^. Have a look at this SO question how to start an activity from a background service.

Note that when a normal foreground application is suspended (so you switched to another app or just to the home screen) your Unity can be killed at any time by the OS when necessary. So there’s no guarantee that your app is still there. So you may even restart your application if it was in the background for too long. I highly recommend reading this answer on the question I’ve linked above.