title: Sleepless process on Ionic5, Capacitor, Android
body: <p>I have an ionic5 app with Capacitor that I'd like to deploy on android. When the app starts I'm getting a list of reminders (from API) I'd like to schedule to be shown as local notifications during the day. After deploying it to my device (by .apk file) is working fine when it's open but after some time when the phone is not in use it's getting sleep and no notification appears. What would be the best way to solve that case? This is my last code with BackgroundTask but it doesn't work anyway.</p>
  <pre><code>import { Injectable } from &quot;@angular/core&quot;;
  import { Job } from &quot;src/entities/respons/_respons&quot;;
  import { Plugins } from '@capacitor/core';
  const {  LocalNotifications, BackgroundTask } = Plugins;
  
  @Injectable({
      providedIn: 'root',
  })
  export class NotificationsService {
  
      constructor() {
          LocalNotifications.requestPermission();
          console.log(` Initialized on ${new Date().toLocaleString()}`);
      }
  
  
      async setup(jobs: Job[]) {
          let id: number = 0;
          let taskId = BackgroundTask.beforeExit(async () =&gt; {
  
              let toSchedule = jobs.filter(e =&gt; (e.isActive || e.isFuture) &amp;&amp; !e.isNotified);
              id = toSchedule.length;
              console.log(`Setup, count ${id};`)
  
              let notificationInterval = setInterval(async () =&gt; {
  
                  let toNotify = jobs.filter(e =&gt; e.isActive &amp;&amp; !e.isNotified);
  
                  if (toNotify.length &gt; 0) {
                      let logger: string = '';
  
                      toNotify.forEach(async job =&gt; {
                          let d = new Date(job.since);
                          logger += `[${d.toLocaleTimeString()} ${job.name}],  `;
                          
                          await LocalNotifications.schedule({
                              notifications: [
                                  {
                                      id: job.id,
                                      title: `${job.name} ${d.toLocaleTimeString()}`,
                                      body: job.body,                                   
                                      iconColor: '#0081ca'
  
                                  }
                              ]
                          });
  
                          job.isNotified = true;
                          id--;
                      });
  
                      console.log(`Tick on ${new Date(Date.now()).toTimeString()} , count ${toNotify.length}; Scheduled:  ${logger}`)
                  }
  
  
                  if (id &lt;= 0) {
                      console.log(`Task finished ${id}; no.:${taskId}`);
                      clearInterval(notificationInterval);
                      BackgroundTask.finish({
                          taskId
                      });
  
                  } else {
                      console.log(`Task in progress ${id}; no.:${taskId}`,)
                  }
  
              }, 60000)
  
          });
      }
  }
  </code></pre>