Skip to content

Commit e72c2c7

Browse files
committed
[native] feat(notifications): Send 'sync in progress' and 'sync complete' notifications
fixes #1126 Signed-off-by: Marcel Klehr <[email protected]>
1 parent 167fef2 commit e72c2c7

File tree

10 files changed

+89
-0
lines changed

10 files changed

+89
-0
lines changed

_locales/en/messages.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -881,5 +881,20 @@
881881
},
882882
"LabelFaq":{
883883
"message": "Check the FAQ"
884+
},
885+
"StatusSyncingfailed": {
886+
"message": "Syncing failed"
887+
},
888+
"StatusSyncingcomplete": {
889+
"message": "Syncing complete"
890+
},
891+
"NotificationSyncingprofile": {
892+
"message": "Syncing profile {0}"
893+
},
894+
"NotificationSyncingsucceeded": {
895+
"message": "Syncing of profile {0} succeeded"
896+
},
897+
"NotificationSyncingfailed": {
898+
"message": "Failed to sync profile {0}"
884899
}
885900
}

android/app/capacitor.build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ dependencies {
1414
implementation project(':capacitor-browser')
1515
implementation project(':capacitor-device')
1616
implementation project(':capacitor-filesystem')
17+
implementation project(':capacitor-local-notifications')
1718
implementation project(':capacitor-network')
1819
implementation project(':capacitor-preferences')
1920
implementation project(':capacitor-share')

android/app/src/main/assets/capacitor.plugins.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
"pkg": "@capacitor/filesystem",
2020
"classpath": "com.capacitorjs.plugins.filesystem.FilesystemPlugin"
2121
},
22+
{
23+
"pkg": "@capacitor/local-notifications",
24+
"classpath": "com.capacitorjs.plugins.localnotifications.LocalNotificationsPlugin"
25+
},
2226
{
2327
"pkg": "@capacitor/network",
2428
"classpath": "com.capacitorjs.plugins.network.NetworkPlugin"
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
2+
android:width="108dp"
3+
android:height="108dp"
4+
android:viewportWidth="24"
5+
android:viewportHeight="24">
6+
<path
7+
android:fillColor="#08d"
8+
android:pathData="M19.35 10.04C18.67 6.59 15.64 4 12 4 9.11 4 6.6 5.64 5.35 8.04 2.34 8.36 0 10.91 0 14c0 3.31 2.69 6 6 6h13c2.76 0 5-2.24 5-5 0-2.64-2.05-4.78-4.65-4.96zM10 17l-3.5-3.5 1.41-1.41L10 14.17 15.18 9l1.41 1.41L10 17z" />
9+
</vector>

android/capacitor.settings.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ project(':capacitor-device').projectDir = new File('../node_modules/@capacitor/d
1717
include ':capacitor-filesystem'
1818
project(':capacitor-filesystem').projectDir = new File('../node_modules/@capacitor/filesystem/android')
1919

20+
include ':capacitor-local-notifications'
21+
project(':capacitor-local-notifications').projectDir = new File('../node_modules/@capacitor/local-notifications/android')
22+
2023
include ':capacitor-network'
2124
project(':capacitor-network').projectDir = new File('../node_modules/@capacitor/network/android')
2225

ios/App/App/capacitor.config.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"CAPBrowserPlugin",
3232
"DevicePlugin",
3333
"FilesystemPlugin",
34+
"LocalNotificationsPlugin",
3435
"CAPNetworkPlugin",
3536
"PreferencesPlugin",
3637
"SharePlugin",

ios/App/Podfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ def capacitor_pods
1616
pod 'CapacitorBrowser', :path => '../../node_modules/@capacitor/browser'
1717
pod 'CapacitorDevice', :path => '../../node_modules/@capacitor/device'
1818
pod 'CapacitorFilesystem', :path => '../../node_modules/@capacitor/filesystem'
19+
pod 'CapacitorLocalNotifications', :path => '../../node_modules/@capacitor/local-notifications'
1920
pod 'CapacitorNetwork', :path => '../../node_modules/@capacitor/network'
2021
pod 'CapacitorPreferences', :path => '../../node_modules/@capacitor/preferences'
2122
pod 'CapacitorShare', :path => '../../node_modules/@capacitor/share'

package-lock.json

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
"@capacitor/device": "7.x",
8585
"@capacitor/filesystem": "7.x",
8686
"@capacitor/ios": "7.x",
87+
"@capacitor/local-notifications": "^7.0.2",
8788
"@capacitor/network": "7.x",
8889
"@capacitor/preferences": "7.x",
8990
"@capacitor/share": "7.x",

src/lib/native/NativeController.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import NativeAccountStorage from './NativeAccountStorage'
55
import Account from '../Account'
66
import { STATUS_ALLGOOD, STATUS_DISABLED, STATUS_ERROR, STATUS_SYNCING } from '../interfaces/Controller'
77
import { initSharp } from '../sentry'
8+
import { LocalNotifications } from '@capacitor/local-notifications'
9+
import { i18n } from './I18n'
810

911
const INACTIVITY_TIMEOUT = 1000 * 7
1012
const MAX_BACKOFF_INTERVAL = 1000 * 60 * 60 // 1 hour
@@ -196,12 +198,50 @@ export default class NativeController {
196198
if (account.getData().syncing) {
197199
return
198200
}
201+
const startTime = Date.now()
199202
setTimeout(() => this.updateStatus(), 500)
203+
let notifications
204+
if ((await LocalNotifications.checkPermissions()).display !== 'denied') {
205+
({ notifications } = await LocalNotifications.schedule({
206+
notifications: [
207+
{
208+
id: Math.round((Math.random() * 2 - 1) * 2147483647),
209+
title: i18n.getMessage('StatusSyncing'),
210+
body: i18n.getMessage('NotificationSyncingprofile', [account.getLabel()]),
211+
ongoing: true,
212+
smallIcon: 'notification_icon',
213+
largeIcon: 'notification_icon',
214+
}
215+
]
216+
}))
217+
}
200218
try {
201219
await account.sync(strategy, forceSync)
202220
} catch (error) {
203221
console.error(error)
204222
}
223+
if ((await LocalNotifications.checkPermissions()).display !== 'denied') {
224+
// Cancel the ongoing notification
225+
await LocalNotifications.cancel({ notifications })
226+
// eslint-disable-next-line no-constant-condition
227+
if (Date.now() - startTime > 60 * 1000) {
228+
await LocalNotifications.schedule({
229+
notifications: [
230+
{
231+
id: Math.round((Math.random() * 2 - 1) * 2147483647),
232+
title: account.getData().error
233+
? i18n.getMessage('StatusSyncingfailed')
234+
: i18n.getMessage('StatusSyncingcomplete'),
235+
body: account.getData().error
236+
? i18n.getMessage('NotificationSyncingfailed', [account.getLabel()])
237+
: i18n.getMessage('NotificationSyncingsucceeded', [account.getLabel()]),
238+
smallIcon: 'notification_icon',
239+
largeIcon: 'notification_icon',
240+
}
241+
]
242+
})
243+
}
244+
}
205245
this.updateStatus()
206246
}
207247

@@ -249,6 +289,10 @@ export default class NativeController {
249289
initSharp()
250290
}
251291

292+
if ((await LocalNotifications.checkPermissions()).display !== 'denied') {
293+
LocalNotifications.requestPermissions()
294+
}
295+
252296
const accounts = await Account.getAllAccounts()
253297
await Promise.all(
254298
accounts.map(async acc => {

0 commit comments

Comments
 (0)