@capacitor/background-runner 1.0.6-dev-20251126T155137.0 → 1.0.6-dev-20251209T171016.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -137,7 +137,7 @@ class CapacitorNotifications: NSObject, CapacitorNotificationsExports {
137
137
  let content = UNMutableNotificationContent()
138
138
  content.title = NSString.localizedUserNotificationString(forKey: notificationOption.title, arguments: nil)
139
139
  content.body = NSString.localizedUserNotificationString(forKey: notificationOption.body, arguments: nil)
140
- content.userInfo = [:]
140
+ content.userInfo = ["source": "capacitor-background-runner"]
141
141
 
142
142
  if let extra = notificationOption.extra {
143
143
  content.userInfo["cap_extra"] = extra
@@ -15,6 +15,7 @@ public class BackgroundRunnerPlugin: CAPPlugin, CAPBridgedPlugin {
15
15
  CAPPluginMethod(name: "removeNotificationListeners", returnType: CAPPluginReturnPromise)
16
16
  ]
17
17
  private let impl = BackgroundRunner()
18
+ private weak var originalDelegate: UNUserNotificationCenterDelegate?
18
19
 
19
20
  override public func load() {
20
21
  NotificationCenter.default.addObserver(
@@ -197,6 +198,11 @@ public class BackgroundRunnerPlugin: CAPPlugin, CAPBridgedPlugin {
197
198
  @objc func registerNotificationCategories() {
198
199
  let notificationCenter = UNUserNotificationCenter.current()
199
200
 
201
+ // Capture the original delegate if it's not self
202
+ if notificationCenter.delegate !== self {
203
+ originalDelegate = notificationCenter.delegate
204
+ }
205
+
200
206
  // Set the plugin as the notification delegate
201
207
  notificationCenter.delegate = self
202
208
  }
@@ -208,30 +214,68 @@ extension BackgroundRunnerPlugin: UNUserNotificationCenterDelegate {
208
214
  public func userNotificationCenter(_ center: UNUserNotificationCenter,
209
215
  didReceive response: UNNotificationResponse,
210
216
  withCompletionHandler completionHandler: @escaping () -> Void) {
217
+ let userInfo = response.notification.request.content.userInfo
218
+
219
+ if isBackgroundRunnerNotification(userInfo) {
220
+ handleBackgroundRunnerNotification(response, completionHandler: completionHandler)
221
+ } else {
222
+ forwardToOriginalDelegate(center, didReceive: response, withCompletionHandler: completionHandler)
223
+ }
224
+ }
225
+
226
+ // This is needed to present notifications when the app is in the foreground
227
+ public func userNotificationCenter(_ center: UNUserNotificationCenter,
228
+ willPresent notification: UNNotification,
229
+ withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
230
+ let userInfo = notification.request.content.userInfo
231
+
232
+ if isBackgroundRunnerNotification(userInfo) {
233
+ handleBackgroundRunnerPresentation(completionHandler: completionHandler)
234
+ } else {
235
+ forwardToOriginalDelegate(center, willPresent: notification, withCompletionHandler: completionHandler)
236
+ }
237
+ }
238
+
239
+ // MARK: - Helper Methods
240
+
241
+ private func isBackgroundRunnerNotification(_ userInfo: [AnyHashable: Any]) -> Bool {
242
+ return (userInfo["source"] as? String) == "capacitor-background-runner"
243
+ }
244
+
245
+ private func handleBackgroundRunnerNotification(_ response: UNNotificationResponse, completionHandler: @escaping () -> Void) {
211
246
  let actionTypeId = response.notification.request.content.categoryIdentifier
212
247
  let notificationId = Int(response.notification.request.identifier) ?? -1
213
248
 
214
- // Create event data
215
249
  let eventData: [String: Any] = [
216
250
  "actionTypeId": actionTypeId,
217
251
  "notificationId": notificationId
218
252
  ]
219
253
 
220
- // Notify listeners with the action data - the 'true' parameter is for retaining the event
221
254
  notifyListeners("backgroundRunnerNotificationReceived", data: eventData, retainUntilConsumed: true)
222
-
223
255
  completionHandler()
224
256
  }
225
257
 
226
- // This is needed to present notifications when the app is in the foreground
227
- public func userNotificationCenter(_ center: UNUserNotificationCenter,
228
- willPresent notification: UNNotification,
229
- withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
230
- // Show the notification even when the app is in foreground
258
+ private func forwardToOriginalDelegate(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
259
+ if let originalDelegate = originalDelegate, originalDelegate.responds(to: #selector(userNotificationCenter(_:didReceive:withCompletionHandler:))) {
260
+ originalDelegate.userNotificationCenter?(center, didReceive: response, withCompletionHandler: completionHandler)
261
+ } else {
262
+ completionHandler()
263
+ }
264
+ }
265
+
266
+ private func handleBackgroundRunnerPresentation(completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
231
267
  if #available(iOS 14.0, *) {
232
268
  completionHandler([.banner, .sound, .badge])
233
269
  } else {
234
270
  completionHandler([.alert, .sound, .badge])
235
271
  }
236
272
  }
273
+
274
+ private func forwardToOriginalDelegate(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
275
+ if let originalDelegate = originalDelegate, originalDelegate.responds(to: #selector(userNotificationCenter(_:willPresent:withCompletionHandler:))) {
276
+ originalDelegate.userNotificationCenter?(center, willPresent: notification, withCompletionHandler: completionHandler)
277
+ } else {
278
+ handleBackgroundRunnerPresentation(completionHandler: completionHandler)
279
+ }
280
+ }
237
281
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@capacitor/background-runner",
3
- "version": "1.0.6-dev-20251126T155137.0",
3
+ "version": "1.0.6-dev-20251209T171016.0",
4
4
  "description": "Capacitor Background Runner",
5
5
  "main": "dist/plugin.cjs.js",
6
6
  "module": "dist/esm/index.js",
@@ -83,5 +83,5 @@
83
83
  "src": "android"
84
84
  }
85
85
  },
86
- "gitHead": "4cf2dd072a40a0717e9c28cbbcfbcc05b332b4de"
86
+ "gitHead": "3be5e99878ad8533a4f4bfbf0ee8bdacbd7f419a"
87
87
  }