# Background sync in android

**URL:** <https://discourse.joplinapp.org/t/background-sync-in-android/28889>\
**Category:** Features\
**Created:** [31 December 2022 15:40 UTC](https://discourse.joplinapp.org/t/background-sync-in-android/28889 "2022-12-31T15:40:25Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chen](https://avatars.discourse-cdn.com/v4/letter/c/3d9bf3/32.png) [@Chen](https://discourse.joplinapp.org/u/Chen)\
**Post date:** [31 December 2022 15:40 UTC](https://discourse.joplinapp.org/t/background-sync-in-android/28889/1 "2022-12-31T15:40:25Z")

</div>

Hi,

I am a joplin user. Plan to make a patch to let joplin do background sync in android. I plan to use HeadlessJS of React Native to achive this purpose. The code size will be quite big.

Per guild in source, I need to start discussion in this forum. So I created this topic. If you have any question about it, please ask it.

Regards,  
Chen Yang

---

<div class="post-metadata">

**Author:** ![Daeraxa](https://yyz2.discourse-cdn.com/flex028/user_avatar/discourse.joplinapp.org/daeraxa/32/6172_2.png) [@Daeraxa](https://discourse.joplinapp.org/u/Daeraxa)\
**Post date:** [31 December 2022 17:16 UTC](https://discourse.joplinapp.org/t/background-sync-in-android/28889/2 "2022-12-31T17:16:13Z")

</div>

Have you looked at the previous attempts to implement this and made sure that your solution won't fall foul of the same things?

---

<div class="post-metadata">

**Author:** ![Chen](https://avatars.discourse-cdn.com/v4/letter/c/3d9bf3/32.png) [@Chen](https://discourse.joplinapp.org/u/Chen)\
**Post date:** [31 December 2022 18:36 UTC](https://discourse.joplinapp.org/t/background-sync-in-android/28889/3 "2022-12-31T18:36:45Z")

</div>

I don't know there are previous attempts at all. Would you mind help to provide some info about previous attempts? I guess this info is hidden in some place and simply googling is not able to find it.

---

<div class="post-metadata">

**Author:** ![Daeraxa](https://yyz2.discourse-cdn.com/flex028/user_avatar/discourse.joplinapp.org/daeraxa/32/6172_2.png) [@Daeraxa](https://discourse.joplinapp.org/u/Daeraxa)\
**Post date:** [31 December 2022 18:39 UTC](https://discourse.joplinapp.org/t/background-sync-in-android/28889/4 "2022-12-31T18:39:24Z")

</div>

Just search the PRs or issues for "background sync"

> <https://github.com/laurent22/joplin/issues/3872>
>
> There are various issues (\[1\](https://github.com/laurent22/joplin/issues/727), \[…2\](https://github.com/laurent22/joplin/issues/1029), \[3\](https://github.com/laurent22/joplin/issues/1501), \[4\](https://github.com/laurent22/joplin/issues/1645), \[5\](https://github.com/laurent22/joplin/issues/3671), …) and threads (\[1\](https://discourse.joplinapp.org/t/android-background-sync-interval/1746), \[2\](https://discourse.joplinapp.org/t/automatic-sync-on-phone-app/3056), \[3\](https://www.reddit.com/r/joplinapp/comments/yicyx1/rant\_i\_really\_like\_the\_app\_but\_my\_goodness\_is\_the/)) on it but it's still not clear to me what exactly is technically not possible to implement.
> 
> \[This comment from September 2018 says\](https://github.com/laurent22/joplin/issues/727#issuecomment-418877957):
> 
> \> Cannot currently be fixed due a limitation of the underlying framework
> 
> Which might be true but that's what libraries are for, right? Therefore this is a \_very\_ misleading comment.
> 
> And \[this comment from October 2019\](https://discourse.joplinapp.org/t/automatic-sync-on-phone-app/3056/8) claims:
> 
> \> we rely on existing libs, which for now don’t exist on React Native
> 
> But it doesn't mention \_which\_ exact libraries would be needed. The logic for syncing is obviously already there. So in theory only the support for doing exactly this sync in a background task would be needed.
> And there are libraries for react-native that are doing exactly this since a few years. For example
> \* \[\`react-native-background-fetch\`\](https://github.com/transistorsoft/react-native-background-fetch) for Android & iOS (first version from August 2016)
> \* \[\`react-native-sync-adapter\`\](https://github.com/ferrannp/react-native-sync-adapter) using the Android SyncAdapter (first version from April 2017)
> \* \[\`react-native-background-task\`\](https://github.com/jamesisaac/react-native-background-task) again for Android & iOS (first version from February 2017)
> \* \[\`react-native-background-job\`\](https://github.com/vikeri/react-native-background-job) for Android (available since December 2016)
> 
> As you can see, these libraries are far older than these comments. So again this would be wrong and a false statement in case this comment refers to syncing libraries.
> 
> Also there's \[this comment from June 2019\](https://github.com/laurent22/joplin/issues/1645#issuecomment-500384473)
> 
> \> Most likely not possible. Android phones these days have services that kill off apps that aren't used, whether you want to keep them running or not. https://dontkillmyapp.com/
> 
> Well,
> 
> 1. it only affects certain vendors in an extreme way. Not every phone is affected.
> 2. \_The exact same website\_ you mentioned shows some workarounds for this very issue.
> 3. Apps are usually killed after a while — not immediately. So Joplin could probably still sync for a while until it would be killed.
> 4. Some FOSS apps which are showing notifications while they are running doesn't seem to be affected (or at least doesn't be to seem affected so often). For instance \[DNS66\](https://github.com/julian-klode/dns66) or \[personalDNSfilter\](https://github.com/IngoZenz/personaldnsfilter).
> 
> So also this comment is false / misleading since the already mentioned website also explicitly mentions workarounds.
> 
> I would obviously understand if the issue would only be the lack of people working on this. In that case this issue could be kept open for tracking, avoiding duplicates and having the possibility to show this issue for possible contributors, so that they know what's still open.
> 
> But currently the given comment are at least somehow lacking some information (or even not relevant / true).
> 
> Thus it would be great if someone could clarify what's really missing here.
> 
> \*\*PS:\*\* Thank you for your work!

> <https://github.com/laurent22/joplin/pull/4925>
>
> \<!--
> 
> Please prefix the title with the platform you are targetting:
> 
> Here ar…e some examples of good titles:
> 
> \- Desktop: Resolves #123: Added new setting to change font
> \- Mobile, Desktop: Fixes #456: Fixed config screen error
> \- All: Resolves #777: Made synchronisation faster
> 
> And here's an explanation of the title format:
> 
> \- "Desktop" for the Windows/macOS/Linux app (Electron app)
> \- "Mobile" for the mobile app (or "Android" / "iOS" if the pull request only applies to one of the mobile platforms)
> \- "CLI" for the CLI app
> 
> If it's two platforms, separate them with commas - "Desktop, Mobile" or if it's for all platforms, prefix with "All".
> 
> If it's not related to any platform (such as a translation, change to the documentation, etc.), simply don't add a platform.
> 
> Then please append the issue that you've addressed or fixed. Use "Resolves #123" for new features or improvements and "Fixes #123" for bug fixes.
> 
> AND PLEASE READ THE GUIDE: https://github.com/laurent22/joplin/blob/dev/CONTRIBUTING.md
> 
> \--\>
> 
> \## Overview
> I recently started using Joplin and am loving it so far. However, the lack of background sync bothered me and seemingly a few others as well, see:
> \#3872 
> https://discourse.joplinapp.org/t/android-background-sync-interval/1746/43
> https://discourse.joplinapp.org/t/background-synchronization-sync-for-ios/10472
> 
> This PR aims to get a working implementation of background sync on both Android (working) and iOS (wip, see below).
> 
> \## Solution Details
> I used \[\`react-native-background-fetch\`\](https://github.com/transistorsoft/react-native-background-fetch) to handle scheduling the recurrent background syncs. NOTE: there is a limitation that these background jobs can only run at best every 15 mins and are limited to ~30 seconds of execution time. So, for long running syncs this still might not be adequate. 
> 
> Even with this library, a challenge is that \*\*callbacks to \`setTimeout\` and \`setInterval\` do not run reliably if the app is in the background\*\*. The callbacks will not be run until the app is brought back to the foreground.
> 
> Conveniently \`setTimeout\` and \`setInterval\` are \[already in the \`shim\`\](https://github.com/laurent22/joplin/blob/ebf92605aeabbd686d57952ab6b946d2db8dcf27/packages/lib/shim.ts#L293-L299). So, we can override these functions to check if the app is currently in the background and if so, just run the callback immediately instead.
> 
> \## Testing
> For testing this in an emulator, there are \[instructions in the README of \`react-native-background-fetch\`\](https://github.com/transistorsoft/react-native-background-fetch#debugging-1). 
> 
> I have tested on Android and \*\*this is working in the emulator and on a real device\*\*. I have noticed that the background sync does not always run at the scheduled intervals though. Sometimes, I will only get a sync every 30mins or 1hr instead of 15 mins. I suspect this is due to Android killing the app in the background, but am still looking into it more.
> 
> For iOS, the only task remaining is to \[link \`react-native-background-fetch\`\](https://github.com/transistorsoft/react-native-background-fetch/blob/master/docs/INSTALL-AUTO-IOS.md). However, I have no way to test on iOS, so if someone else wants to handle this let me know! Otherwise, I can try to find the time to set up my work Macbook for testing.
> 
> Also, if anyone has suggestions for how to set up some automated tests, that would be great. I'm new to mobile development, so feedback is always appreciated!

---

<div class="post-metadata">

**Author:** ![Chen](https://avatars.discourse-cdn.com/v4/letter/c/3d9bf3/32.png) [@Chen](https://discourse.joplinapp.org/u/Chen)\
**Post date:** [31 December 2022 19:11 UTC](https://discourse.joplinapp.org/t/background-sync-in-android/28889/5 "2022-12-31T19:11:09Z")

</div>

Thanks a lot. Let me take a look first.

---

<div class="post-metadata">

**Author:** ![Chen](https://avatars.discourse-cdn.com/v4/letter/c/3d9bf3/32.png) [@Chen](https://discourse.joplinapp.org/u/Chen)\
**Post date:** [31 December 2022 19:32 UTC](https://discourse.joplinapp.org/t/background-sync-in-android/28889/6 "2022-12-31T19:32:25Z")

</div>

After some researching, I am interesting on existing solution provide by @camgraff. Basicly, we use same underly tech: HeadlessJS. The difference is that he uses react-native-background-fetch and my plan is to use headlessjs directly + java code to handle background.

There is a difference is that he aims for android/ios together. And I only works for android. By using native java code, I believe I can achieve the state of art in background sync which I mean I can make background sync as good as other app ( none google app ).

---

<div class="post-metadata">

**Author:** ![Daeraxa](https://yyz2.discourse-cdn.com/flex028/user_avatar/discourse.joplinapp.org/daeraxa/32/6172_2.png) [@Daeraxa](https://discourse.joplinapp.org/u/Daeraxa)\
**Post date:** [31 December 2022 19:34 UTC](https://discourse.joplinapp.org/t/background-sync-in-android/28889/7 "2022-12-31T19:34:29Z")

</div>

> [@Chen](#):
>
> And I only works for android

I think this might be a problem in itself, we would want a solution that works for all platforms and wouldn't want to split functionality by OS unless it is literally impossible to do so on that platform.

---

<div class="post-metadata">

**Author:** ![Chen](https://avatars.discourse-cdn.com/v4/letter/c/3d9bf3/32.png) [@Chen](https://discourse.joplinapp.org/u/Chen)\
**Post date:** [31 December 2022 19:47 UTC](https://discourse.joplinapp.org/t/background-sync-in-android/28889/8 "2022-12-31T19:47:04Z")

</div>

I am understanding your consent. But let me be honest, you can hardly to find a person can handle both andorid and ios at same time. If you are only interested in total solution, I think you'd wait more time than you expected.

For me, it is ok. I am android user and developer, so I am satisfied with android solution only and willing to spend some time on it. I am writing this post because I hope I can contribute it back to community. However, if it is not possible, I am ok with it.

Thanks,  
Chen Yang

---

<div class="post-metadata">

**Author:** ![Daeraxa](https://yyz2.discourse-cdn.com/flex028/user_avatar/discourse.joplinapp.org/daeraxa/32/6172_2.png) [@Daeraxa](https://discourse.joplinapp.org/u/Daeraxa)\
**Post date:** [31 December 2022 20:12 UTC](https://discourse.joplinapp.org/t/background-sync-in-android/28889/9 "2022-12-31T20:12:47Z")

</div>

I would have to defer to the experts here, it is quite common we get people proposing fixes for things that only target one platform and quite often the answer is that we would prefer to wait for a complete solution than doing half of it.

I'll leave anything else for comment by people who know more about the subject than I do.

---

<div class="post-metadata">

**Author:** ![Chen](https://avatars.discourse-cdn.com/v4/letter/c/3d9bf3/32.png) [@Chen](https://discourse.joplinapp.org/u/Chen)\
**Post date:** [31 December 2022 21:14 UTC](https://discourse.joplinapp.org/t/background-sync-in-android/28889/10 "2022-12-31T21:14:26Z")

</div>

Agree. @Daeraxa Thanks a lot. You have give really good info about previous codes.

---

<div class="post-metadata">

**Author:** ![Chen](https://avatars.discourse-cdn.com/v4/letter/c/3d9bf3/32.png) [@Chen](https://discourse.joplinapp.org/u/Chen)\
**Post date:** [3 January 2023 22:03 UTC](https://discourse.joplinapp.org/t/background-sync-in-android/28889/11 "2023-01-03T22:03:00Z")

</div>

I have complete my first prototype and push code to [GitHub - wao/joplin: Joplin - an open source note taking and to-do application with synchronization capabilities for Windows, macOS, Linux, Android and iOS. Forum: https://discourse.joplinapp.org/](https://github.com/wao/joplin) branch android\_sync. I also upload prebuilt apk to:

[Release Release for android background sync at 0103 · wao/joplin · GitHub](https://github.com/wao/joplin/releases/tag/android_sync_prototype_0103).

You can try it at your own risk or you can build from source by yourself. It works great for me. I think it is enough for my daily use.

---

<div class="post-metadata">

**Author:** ![laurent](https://avatars.discourse-cdn.com/v4/letter/l/ce7236/32.png) [@laurent](https://discourse.joplinapp.org/u/laurent)\
**Post date:** [3 January 2023 22:40 UTC](https://discourse.joplinapp.org/t/background-sync-in-android/28889/12 "2023-01-03T22:40:50Z")

</div>

How do you manage to trigger sync in the background? Does your native code somehow calls the js sync code?

As Daeraxa mentioned we generally prefer cross-platform solutions but there's so much demand for this feature that we'd consider it if it's well implemented and reliable.

---

<div class="post-metadata">

**Author:** ![Chen](https://avatars.discourse-cdn.com/v4/letter/c/3d9bf3/32.png) [@Chen](https://discourse.joplinapp.org/u/Chen)\
**Post date:** [3 January 2023 22:53 UTC](https://discourse.joplinapp.org/t/background-sync-in-android/28889/13 "2023-01-03T22:53:16Z")

</div>

React native support headless js [Headless JS · React Native](https://reactnative.dev/docs/headless-js-android). By using it, native code can invoke async js function.

Basic idea is that you can register an async function as headless js, then native code can tell react native to run it.

---

<div class="post-metadata">

**Author:** ![anon55947162](https://avatars.discourse-cdn.com/v4/letter/a/e274bd/32.png) [@anon55947162](https://discourse.joplinapp.org/u/anon55947162)\
**Post date:** [4 January 2023 17:36 UTC](https://discourse.joplinapp.org/t/background-sync-in-android/28889/14 "2023-01-04T17:36:20Z")

</div>

This background sync could be disabled by the user ?

---

<div class="post-metadata">

**Author:** ![Daeraxa](https://yyz2.discourse-cdn.com/flex028/user_avatar/discourse.joplinapp.org/daeraxa/32/6172_2.png) [@Daeraxa](https://discourse.joplinapp.org/u/Daeraxa)\
**Post date:** [4 January 2023 20:21 UTC](https://discourse.joplinapp.org/t/background-sync-in-android/28889/15 "2023-01-04T20:21:56Z")

</div>

Already can be, thats what the `disabled` sync interval does.

---

<div class="post-metadata">

**Author:** ![Chen](https://avatars.discourse-cdn.com/v4/letter/c/3d9bf3/32.png) [@Chen](https://discourse.joplinapp.org/u/Chen)\
**Post date:** [4 January 2023 20:46 UTC](https://discourse.joplinapp.org/t/background-sync-in-android/28889/16 "2023-01-04T20:46:18Z")

</div>

I think current native code in my branch, you can't disable background trigger because it is just a prototype. In finaly PR, I can add a native method to turn off background sync.

Although you can forbid in js code which means even native code trigger sync however it will be bannded due to some js level restriction.

But it is not good for battery.

---

<div class="post-metadata">

**Author:** ![anon55947162](https://avatars.discourse-cdn.com/v4/letter/a/e274bd/32.png) [@anon55947162](https://discourse.joplinapp.org/u/anon55947162)\
**Post date:** [4 January 2023 21:08 UTC](https://discourse.joplinapp.org/t/background-sync-in-android/28889/17 "2023-01-04T21:08:39Z")

</div>

> [@Chen](#):
>
> But it is not good for battery.

That would mean a button to completely quit the app will be needed to be sure the app stop working in the background ?

---

<div class="post-metadata">

**Author:** ![Chen](https://avatars.discourse-cdn.com/v4/letter/c/3d9bf3/32.png) [@Chen](https://discourse.joplinapp.org/u/Chen)\
**Post date:** [5 January 2023 14:12 UTC](https://discourse.joplinapp.org/t/background-sync-in-android/28889/18 "2023-01-05T14:12:34Z")

</div>

In android, as I understand, there is no such concept "quit the app". The lifecycle is manager by system/os not by app itself. However, for your question, I think when in settings UI, user choose no auto sync then js code can call the native module method to stop the app be triggerred future in background by andorid.

---

<div class="post-metadata">

**Author:** ![crtkugrrvjy](https://avatars.discourse-cdn.com/v4/letter/c/6bbea6/32.png) [@crtkugrrvjy](https://discourse.joplinapp.org/u/crtkugrrvjy)\
**Post date:** [22 October 2023 14:33 UTC](https://discourse.joplinapp.org/t/background-sync-in-android/28889/19 "2023-10-22T14:33:59Z")

</div>

Thanks for your work. Is this in a usable state for a novice to try? Don't have root and would like the ability to remove it.

---

<div class="post-metadata">

**Author:** ![SidMan](https://yyz2.discourse-cdn.com/flex028/user_avatar/discourse.joplinapp.org/sidman/32/19917_2.png) [@SidMan](https://discourse.joplinapp.org/u/SidMan)\
**Post date:** [6 February 2024 07:58 UTC](https://discourse.joplinapp.org/t/background-sync-in-android/28889/20 "2024-02-06T07:58:32Z")

</div>

This is a must have feature. After opening joplin I have to wait about an hour before sync completes. I don't use the android app often and it has to sync a lot of files.

File system sync using a different app would be faster but it has this issue : [Sync silently ignores new files with old timestamps · Issue #6517 · laurent22/joplin · GitHub](https://github.com/laurent22/joplin/issues/6517)

[Next page](https://discourse.joplinapp.org/t/background-sync-in-android/28889.md?page=2)
