Plugin to start application as foreground service to run in background continously even if the app is killed or device is rebooted
npm install capacitor-plugin-backgroundserviceThis plugin is designed to start the application as a foreground service, allowing it to run continuously in the background even if the app is killed. It also ensures the service restarts automatically when the Android device is booted or restarted.
This plugin does not expose any explicit methods for use in JavaScript. All functionality is handled natively through Java code and configurations in the Android manifest file. You will need to set up the required native components to enable and customize the background service behavior.
``bash`
npm install capacitor-plugin-backgroundservice
npx cap sync
* StartBackgroundService(...)
* requestNotificationPermission()
* connectBleDevice(...)
* isMqttClientConnected()
* connectMqtt(...)
* publishMessage(...)
* subscribeToNormalDLTopic(...)
* subscribeToOtherDLTopic(...)
* subscribeToAuthTopic(...)
* disconnectMqttBroker()
* addListener(string, ...)
* Interfaces
`typescript`
StartBackgroundService(options: { baseURL: string; basicAUTH: string; apiSuffix: string; deviceUUID: string; deviceType: string | null; macAddress: string | null; authPayload: { parameters: { header: string; }; }; }) => Promise
Start Background Service.
| Param | Type |
| ------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| options | { baseURL: string; basicAUTH: string; apiSuffix: string; deviceUUID: string; deviceType: string \| null; macAddress: string \| null; authPayload: { parameters: { header: string; }; }; } |
--------------------
`typescript`
requestNotificationPermission() => Promise<{ granted: boolean; }>
Request notification permissions.
Returns: Promise<{ granted: boolean; }>
--------------------
`typescript`
connectBleDevice(options: { macAddress: string | null; }) => Promise
| Param | Type |
| ------------- | -------------------------------------------- |
| options | { macAddress: string \| null; } |
--------------------
`typescript`
isMqttClientConnected() => Promise
Returns: Promise<boolean>
--------------------
`typescript`
connectMqtt(options: { BrokerUrl: string; iOSBrokerUrl: string; username: string; password: string; }) => Promise<{ isMqttConnected: boolean; }>
| Param | Type |
| ------------- | --------------------------------------------------------------------------------------------- |
| options | { BrokerUrl: string; iOSBrokerUrl: string; username: string; password: string; } |
Returns: Promise<{ isMqttConnected: boolean; }>
--------------------
`typescript`
publishMessage(options: { topicToPublish: string; payload: string; }) => Promise<{ isMessagePublished: boolean; }>
| Param | Type |
| ------------- | --------------------------------------------------------- |
| options | { topicToPublish: string; payload: string; } |
Returns: Promise<{ isMessagePublished: boolean; }>
--------------------
`typescript`
subscribeToNormalDLTopic(options: { topicTOSubscribe: string; }) => Promise<{ isSubscriptionSuccess: boolean; }>
| Param | Type |
| ------------- | ------------------------------------------ |
| options | { topicTOSubscribe: string; } |
Returns: Promise<{ isSubscriptionSuccess: boolean; }>
--------------------
`typescript`
subscribeToOtherDLTopic(options: { topicTOSubscribe: string; }) => Promise<{ isSubscriptionSuccess: boolean; }>
| Param | Type |
| ------------- | ------------------------------------------ |
| options | { topicTOSubscribe: string; } |
Returns: Promise<{ isSubscriptionSuccess: boolean; }>
--------------------
`typescript`
subscribeToAuthTopic(options: { authTopicToSubscribe: string; }) => Promise<{ isSubscriptionSuccess: boolean; }>
| Param | Type |
| ------------- | ---------------------------------------------- |
| options | { authTopicToSubscribe: string; } |
Returns: Promise<{ isSubscriptionSuccess: boolean; }>
--------------------
`typescript`
disconnectMqttBroker() => Promise<{ isMqttBrokerDisconnected: boolean; }>
Returns: Promise<{ isMqttBrokerDisconnected: boolean; }>
--------------------
`typescript`
addListener(eventName: string, listenerFunc: (data: any) => void) => Promise
Listen for MQTT messages from native background service.
| Param | Type |
| ------------------ | ----------------------------------- |
| eventName | string |
| listenerFunc | (data: any) => void |
Returns: Promise<PluginListenerHandle>
--------------------
#### PluginListenerHandle
| Prop | Type |
| ------------ | ----------------------------------------- |
| remove` | () => Promise<void> |