Skip to content

Capacitor — Setup

Installation

npm install @transistorsoft/capacitor-background-fetch
npx cap sync
yarn add @transistorsoft/capacitor-background-fetch
npx cap sync

iOS Setup

Background Modes

In Xcode, select your project root → Signing & Capabilities+ CapabilityBackground Modes. Enable:

  • [x] Background fetch
  • [x] Background processing (only if you intend to use BackgroundFetch.scheduleTask)

Background Modes

Info.plist

Open your Info.plist and add the key "Permitted background task scheduler identifiers":

Add the required identifier com.transistorsoft.fetch:

If you intend to execute your own custom tasks via BackgroundFetch.scheduleTask, you must add those custom identifiers as well. For example, if you intend to execute a custom taskId: 'com.transistorsoft.customtask', you must add the identifier com.transistorsoft.customtask to your "Permitted background task scheduler identifiers", as well.

Warning

A task identifier can be any string you wish, but it must be prefixed with com.transistorsoft..

AppDelegate.swift

import UIKit
import Capacitor
import TSBackgroundFetch

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication,
                     didFinishLaunchingWithOptions launchOptions:
                       [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // [REQUIRED] Register BackgroundFetch
        let fetchManager = TSBackgroundFetch.sharedInstance()
        fetchManager?.didFinishLaunching()

        return true
    }

    // [REQUIRED] Background fetch delegate
    func application(_ application: UIApplication,
                     performFetchWithCompletionHandler completionHandler:
                       @escaping (UIBackgroundFetchResult) -> Void) {
        let fetchManager = TSBackgroundFetch.sharedInstance()
        fetchManager?.perform(completionHandler: completionHandler,
                              applicationState: application.applicationState)
    }
}

Android Setup

No additional Android setup is required.