i want to make app that is play sound on schedule with ionic 5

366 Views Asked by At

Ionic 5 I want to make an app that is play sound for 10 minutes in any way I just want sound for 10 minutes but the problem is when the app is terminated. I tried local notification with alarm. I am success to play sound but the problem is I was not able to play sound for 10 minutes repeatedly

1

There are 1 best solutions below

0
On

You can do it as mentioned here if you are using capacitor: https://capacitorjs.com/docs/apis/background-task

import { Plugins } from '@capacitor/core';

const { App, BackgroundTask } = Plugins;

App.addListener('appStateChange', (state) => {

  if (!state.isActive) {
    // The app has become inactive. We should check if we have some work left to do, and, if so,
    // execute a background task that will allow us to finish that work before the OS
    // suspends or terminates our app:

    let taskId = BackgroundTask.beforeExit(async () => {
      // In this function We might finish an upload, let a network request
      // finish, persist some data, or perform some other task

      // play your sound

    });
  }
})

However, this will risk the app being terminated, and being labeled as impacting battery life as you won't finish the task.

The best option is to have a Backend service that will send push notifications every 10 minutes. In this way, OS will handle the notification instead of the app in the background.

You can have more info on how to integrate Push Notifications here: https://www.freecodecamp.org/news/how-to-get-push-notifications-working-with-ionic-4-and-firebase-ad87cc92394e/