Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by nevzatbol · Mar 03, 2020 at 06:41 AM · androidiosserverbackgroundnotification

Run a script in background when the app is open or closed

Hi,

I want to run a script in background when the app is open or closed. This background script sends a request to the local server and then sends a response as a notification to the user. I can communicate the local server and send a notification but I don't know how running this script in app background. This background script should be running independently of scenes. This background script will be run in android and ios.

Please help me with my this problem.

     using System.Collections;
     using UnityEngine;
     using UnityEngine.Networking;
     using Unity.Notifications.Android;
     
     public class Notification : MonoBehaviour
     {
         void Start()
         {
             InvokeRepeating("ErrorUpdate", 0, 0.1f);
         }
     
         void ErrorUpdate()
         {
             StartCoroutine(GetError());
         }
     
         IEnumerator GetError()
         {        
             string IpURL = IPClass.url + IPClass.port + "/api/data";
             UnityWebRequest _www = UnityWebRequest.Get(IpURL);
             yield return _www.SendWebRequest();
             if (_www.error == null)
             {
                 ErrorData(_www.downloadHandler.text);
             }
         }
     
         private void ErrorData(string _url)
         {
             ErrorListDALClass errorDataJson = JsonUtility.FromJson<ErrorListDALClass>(_url);
     
             foreach (var item in errorDataJson.Value)
             {
                 if (item != null)
                 {
                     SendNotification();
                     break;
                 }
             }
         }
     
         public void CreateNotificationChannel()
         {
             var c = new AndroidNotificationChannel()
             {
                 Id = "channel_id",
                 Name = "Default Channel",
                 Importance = Importance.High,
                 Description = "Generic notifications",
             };
             AndroidNotificationCenter.RegisterNotificationChannel(c);
         }
     
         public void SendNotification()
         {
             var notification = new AndroidNotification();
             notification.Title = "Robot Says";
             notification.Text = "System Error";
             notification.Color = Color.red;
             notification.FireTime = System.DateTime.Now.AddMilliseconds(10);
     
             AndroidNotificationCenter.SendNotification(notification, "channel_id");        
         }        
     }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

5 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Bunny83 · Mar 24, 2020 at 06:44 PM

The term "App" on mobile devices is generally reserved for foreground applications. When the app is suspended / in background it is completely halted and at will of the operating system can be completely erased from RAM when needed. So there is nothing there that can reliable continue to run.


What you are looking for is implementing a native background service. This can not be done in Unity or the Unity engine. Such a service has to be programmed natively (i.e. In Java for Android and probably xCode for iOS). You would have to dig quite deep into the APIs of the targetplatform you want to build for.


Just to make that clear: A Unity made App is a foreground application / Activity and as such only works when in foreground. Note that creating a background service is not a simple process and you have to handle all background related thing in the native code outside Unity. When Unity comes back on / is restarted you have to communicate with the background servite to transfer relevant information.


There are little examples on the web how to implement a background service on Android. This is a quite advanced topic and is nothing for beginners. You should have a good understanding of the target OS when you want to attempt this. Also watch out for common practices and guidelines from Google / Apple. Always keep usability and non-intrusive architecture in mind. While foreground applications are relatively free in what they do since the user is actively using it at that point. Background services have to take special care to not cause unnecessary battery drain, network traffic, ...

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image nevzatbol · Mar 25, 2020 at 06:33 AM 0
Share

Thanks for your answer. So is there any way to run IEnumerator in a thread? Another thing I'm trying to do is to download the video when the user enters the video player screen from my application. Even if the user switches to different screens, it should continue to download.

avatar image
0

Answer by ShadyProductions · Mar 04, 2020 at 08:38 AM

Take a look at this thread:

https://answers.unity.com/questions/357033/unity3d-and-c-coroutines-vs-threading.html

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image nevzatbol · Mar 24, 2020 at 10:52 AM 0
Share

Thanks for your answer. I've tried this code but doesn't give a solution running IEnumerator in the background.

avatar image
0

Answer by nevzatbol · Mar 26, 2020 at 10:53 AM

I was able to send the web request in the background by trying the code below. But Android doesn't allow this code to run in the background.

     private BackgroundWorker worker = new BackgroundWorker();
 
     private void Awake()
     {
         worker.DoWork += worker_DoWork;
         worker.RunWorkerCompleted += worker_RunWorkerCompleted;
         worker.RunWorkerAsync();
     }
     
     private void worker_DoWork(object sender, DoWorkEventArgs e)
     {
         GetRequest();
 
         e.Result = "all done";
     }
 
     private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
     {
         // will output "all done" to the console
         Debug.Log((string)e.Result);        
     }
 
     async void GetRequest()
     {
         string IpURL = IPClass.url + IPClass.port + "/api/data";
         using (HttpClient client = new HttpClient())
         {
             using (HttpResponseMessage response = await client.GetAsync(IpURL))
             {
                 Debug.Log(response.IsSuccessStatusCode);
                 if (response.IsSuccessStatusCode)
                 {
                     using (HttpContent content = response.Content)
                     {
                         ErrorData(content.ReadAsStringAsync().Result);
                     }
                 }
             }
         }
     }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Tony-Lovell · Jun 22, 2021 at 12:14 PM

Unity is missing an opportunity by failing to recognize that it offers 80% of what is needed for cross-platform phone app (not necessarily games) for Android and iOS. Rather than spending more time developing the N+Mth generation particle generating system, some time spent abstracting interfaces to SMS, phone calling, cameras, contact data stores, etc would create a much-needed IDE.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Rachan · Feb 23 at 08:44 PM

There is no way to do in Unity right?

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

284 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Is there a Tutorial on Running Android, iOS in background? 1 Answer

Background Networking Activity and Unity Run Loop 0 Answers

Build Server, both Android and iOS 0 Answers

Reading notifications from Unity or knowing if notifications are enabled 0 Answers

Disable/enable local notifications on Android and iOS 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges