@capawesome-team/capacitor-android-foreground-service

中文文档
8.1.0发布于 2026/03/17MITTypeScriptESM + CJS零依赖构建来源已验证近 30 天下载 7.1万

Capacitor plugin to run a foreground service on Android.

原始使用文档
文档目录28 节
  1. Newsletter
  2. Compatibility
  3. Installation
  4. Android
  5. Configuration
  6. Demo
  7. Usage
  8. API
  9. moveToForeground()
  10. startForegroundService(...)
  11. updateForegroundService(...)
  12. stopForegroundService()
  13. checkPermissions()
  14. requestPermissions()
  15. checkManageOverlayPermission()
  16. requestManageOverlayPermission()
  17. createNotificationChannel(...)
  18. deleteNotificationChannel(...)
  19. addListener('buttonClicked', ...)
  20. addListener('notificationTapped', ...)
  21. removeAllListeners()
  22. Interfaces
  23. Type Aliases
  24. Enums
  25. FAQ
  26. Why can the user dismiss the notification?
  27. Changelog
  28. License

@capawesome-team/capacitor-android-foreground-service

Capacitor plugin to run a foreground service on Android.

Newsletter

Stay up to date with the latest news and updates about the Capawesome, Capacitor, and Ionic ecosystem by subscribing to our Capawesome Newsletter.

Compatibility

Plugin VersionCapacitor VersionStatus
8.x.x>=8.x.xActive support

Installation

You can use our AI-Assisted Setup to install the plugin. Add the Capawesome Skills to your AI tool using the following command:

npx skills add capawesome-team/skills

Then use the following prompt:

Use the `capacitor-plugins` skill from `capawesome-team/skills` to install the `@capawesome-team/capacitor-android-foreground-service` plugin in my project.

If you prefer Manual Setup, install the plugin by running the following commands and follow the platform-specific instructions below:

npm install @capawesome-team/capacitor-android-foreground-service
npx cap sync

Android

This API requires the following permissions be added to your AndroidManifest.xml before or after the application tag:

<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_LOCATION" />
<!-- Required to keep the app running in the background without any ressource limitations -->
<uses-permission android:name="android.permission.WAKE_LOCK" />
<!-- Required to request the manage overlay permission -->
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

Attention: Replace FOREGROUND_SERVICE_LOCATION with the foreground service types you want to use (see Foreground service types). See ServiceType for the available types.

You also need to add the following receiver and service inside the application tag in your AndroidManifest.xml:

<receiver android:name="io.capawesome.capacitorjs.plugins.foregroundservice.NotificationActionBroadcastReceiver" />
<service android:name="io.capawesome.capacitorjs.plugins.foregroundservice.AndroidForegroundService" android:foregroundServiceType="location" />

Attention: Replace location with the foreground service types you want to use (see Foreground service types). See ServiceType for the available types.

Configuration

No configuration required for this plugin.

Demo

A working example can be found here: robingenz/capacitor-plugin-demo

Android
Android Demo

Usage

import { Capacitor } from '@capacitor/core';
import { ForegroundService } from '@capawesome-team/capacitor-android-foreground-service';

const startForegroundService = async () => {
  await ForegroundService.startForegroundService({
    id: 1,
    title: 'Title',
    body: 'Body',
    smallIcon: 'ic_stat_icon_config_sample',
    buttons: [
      {
        title: 'Button 1',
        id: 1,
      },
      {
        title: 'Button 2',
        id: 2,
      },
    ],
    silent: false,
    notificationChannelId: 'default',
  });
};

const updateForegroundService = async () => {
  await ForegroundService.updateForegroundService({
    id: 1,
    title: 'Title',
    body: 'Body',
    smallIcon: 'ic_stat_icon_config_sample',
  });
};

const stopForegroundService = async () => {
  await ForegroundService.stopForegroundService();
};

const createNotificationChannel = async () => {
  await ForegroundService.createNotificationChannel({
    id: 'default',
    name: 'Default',
    description: 'Default channel',
    importance: Importance.Default,
  });
};

const deleteNotificationChannel = async () => {
  await ForegroundService.deleteNotificationChannel({
    id: 'default',
  });
};

API

moveToForeground()

moveToForeground() => Promise<void>

Moves the app to the foreground.

On Android SDK 23+, the user must grant the manage overlay permission. You can use the requestManageOverlayPermission() method to request the permission and the checkManageOverlayPermission() method to check if the permission is granted.

Only available on Android.

Since: 0.3.0


startForegroundService(...)

startForegroundService(options: StartForegroundServiceOptions) => Promise<void>

Starts the foreground service.

Only available on Android.

ParamType
optionsStartForegroundServiceOptions

Since: 0.0.1


updateForegroundService(...)

updateForegroundService(options: UpdateForegroundServiceOptions) => Promise<void>

Updates the notification details of the running foreground service.

Only available on Android.

ParamType
optionsStartForegroundServiceOptions

Since: 6.1.0


stopForegroundService()

stopForegroundService() => Promise<void>

Stops the foreground service.

Only available on Android.

Since: 0.0.1


checkPermissions()

checkPermissions() => Promise<PermissionStatus>

Check permission to display notifications.

On Android, this method only needs to be called on Android 13+.

Only available on Android.

Returns: Promise<PermissionStatus>

Since: 5.0.0


requestPermissions()

requestPermissions() => Promise<PermissionStatus>

Request permission to display notifications.

On Android, this method only needs to be called on Android 13+.

Only available on Android.

Returns: Promise<PermissionStatus>

Since: 5.0.0


checkManageOverlayPermission()

checkManageOverlayPermission() => Promise<ManageOverlayPermissionResult>

Check if the overlay permission is granted.

Only available on Android.

Returns: Promise<ManageOverlayPermissionResult>

Since: 0.3.0


requestManageOverlayPermission()

requestManageOverlayPermission() => Promise<ManageOverlayPermissionResult>

Request the manage overlay permission.

Only available on Android.

Returns: Promise<ManageOverlayPermissionResult>

Since: 0.3.0


createNotificationChannel(...)

createNotificationChannel(options: CreateNotificationChannelOptions) => Promise<void>

Create a notification channel. If not invoked, the plugin creates a channel with name and description set to "Default".

Only available for Android (SDK 26+).

ParamType
optionsCreateNotificationChannelOptions

Since: 6.1.0


deleteNotificationChannel(...)

deleteNotificationChannel(options: DeleteNotificationChannelOptions) => Promise<void>

Delete a notification channel.

Only available for Android (SDK 26+).

ParamType
optionsDeleteNotificationChannelOptions

Since: 6.1.0


addListener('buttonClicked', ...)

addListener(eventName: 'buttonClicked', listenerFunc: ButtonClickedEventListener) => Promise<PluginListenerHandle>

Called when a notification button is clicked.

Only available on iOS.

ParamType
eventName'buttonClicked'
listenerFuncButtonClickedEventListener

Returns: Promise<PluginListenerHandle>

Since: 0.2.0


addListener('notificationTapped', ...)

addListener(eventName: 'notificationTapped', listenerFunc: NotificationTappedEventListener) => Promise<PluginListenerHandle>

Called when the foreground service notification is tapped.

Only available on Android.

ParamType
eventName'notificationTapped'
listenerFuncNotificationTappedEventListener

Returns: Promise<PluginListenerHandle>

Since: 8.1.0


removeAllListeners()

removeAllListeners() => Promise<void>

Remove all listeners for this plugin.

Since: 0.2.0


Interfaces

StartForegroundServiceOptions

PropTypeDescriptionDefaultSince
bodystringThe body of the notification, shown below the title.0.0.1
buttonsNotificationButton[]The buttons to show on the notification. Only available on Android (SDK 24+).0.2.0
idnumberThe notification identifier.0.0.1
serviceTypeServiceTypeThe foreground service type. Only available on Android (SDK 29+).6.2.0
smallIconstringThe status bar icon for the notification. Icons should be placed in your app's res/drawable folder. The value for this option should be the drawable resource ID, which is the filename without an extension.0.0.1
titlestringThe title of the notification.0.0.1
silentbooleanIf true, will only alert (sound/vibration) on the first notification. Subsequent updates will be silent.false6.1.0
notificationChannelIdstringThe notification channel identifier.6.1.0

NotificationButton

PropTypeDescriptionSince
titlestringThe button title.0.2.0
idnumberThe button identifier. This is used to identify the button when the buttonClicked event is emitted.0.2.0

PermissionStatus

PropTypeDescriptionSince
displayPermissionStatePermission state of displaying notifications.5.0.0

ManageOverlayPermissionResult

PropTypeDescriptionSince
grantedbooleanWhether the permission is granted. This is always true on Android SDK < 23.0.3.0

CreateNotificationChannelOptions

PropTypeDescriptionSince
descriptionstringThe description of this channel (presented to the user).6.1.0
idstringThe channel identifier.6.1.0
importanceImportanceThe level of interruption for notifications posted to this channel.6.1.0
namestringThe name of this channel (presented to the user).6.1.0

DeleteNotificationChannelOptions

PropTypeDescriptionSince
idstringThe channel identifier.6.1.0

PluginListenerHandle

PropType
remove() => Promise<void>

ButtonClickedEvent

PropTypeDescriptionSince
buttonIdnumberThe button identifier.0.2.0

NotificationTappedEvent

PropTypeDescriptionSince
notificationIdnumberThe notification identifier.8.1.0

Type Aliases

UpdateForegroundServiceOptions

StartForegroundServiceOptions

PermissionState

'prompt' | 'prompt-with-rationale' | 'granted' | 'denied'

ButtonClickedEventListener

(event: ButtonClickedEvent): void

NotificationTappedEventListener

(event: NotificationTappedEvent): void

Enums

ServiceType

MembersValueDescriptionSince
Location8Long-running use cases that require location access, such as navigation and location sharing.6.2.0
Microphone128Continue microphone capture from the background, such as voice recorders or communication apps.6.2.0

Importance

MembersValueSince
Min16.1.0
Low26.1.0
Default36.1.0
High46.1.0
Max56.1.0

FAQ

Why can the user dismiss the notification?

Android 14 has changed the behavior to allow users to dismiss such notifications, see Changes to how users experience non-dismissible notifications.

Changelog

See CHANGELOG.md.

License

See LICENSE.

版本与发布记录

@capawesome-team/capacitor-android-foreground-service 共发布 10 个版本,首次发布于 2023/10/21;过去 12 个月发布了 3 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
8.1.0latest2026/03/177 个月前
8.0.12026/02/198 个月前
8.0.02025/12/1710 个月前
7.0.12025/02/061.8 年前
7.0.02025/01/301.8 年前
6.2.02024/12/271.9 年前

关于 @capawesome-team/capacitor-android-foreground-service 的常见问题

@capawesome-team/capacitor-android-foreground-service 是什么?

Capacitor plugin to run a foreground service on Android.

如何安装 @capawesome-team/capacitor-android-foreground-service?

在项目目录运行 npm install @capawesome-team/capacitor-android-foreground-service,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@capacitor/core(>=8.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@capawesome-team/capacitor-android-foreground-service 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@capawesome-team/capacitor-android-foreground-service 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@capawesome-team/capacitor-android-foreground-service 可以商用吗?

@capawesome-team/capacitor-android-foreground-service 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@capawesome-team/capacitor-android-foreground-service 的下载量有多少?

根据 npm 官方下载统计,@capawesome-team/capacitor-android-foreground-service 在 2026/10/09 – 2026/11/07 这 30 天内被下载 71,493 次,日均约 2,383 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@capawesome-team/capacitor-android-foreground-service 还在维护吗?

最新版本 8.1.0 发布于 2026/03/17(7 个月前);至今共发布 10 个版本,其中过去 12 个月发布了 3 个;首次发布于 2023/10/21。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。