Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

mobile: fix crash in background on boot

Ammar Ahmed48e5c4d90869
diff --git a/apps/mobile/app/services/background-sync.ts b/apps/mobile/app/services/background-sync.ts
index a02a444cd..ddd1a718b 100644
--- a/apps/mobile/app/services/background-sync.ts
+++ b/apps/mobile/app/services/background-sync.ts
@@ -137,6 +137,7 @@ const onBoot = async () => {
try {
if (!SettingsService.getProperty("backgroundSync")) {
DatabaseLogger.info("BACKGROUND SYNC ON BOOT DISABLED");
+
return;
}
diff --git a/apps/mobile/native/android/app/src/main/java/com/streetwriters/notesnook/BootRecieverService.java b/apps/mobile/native/android/app/src/main/java/com/streetwriters/notesnook/BootRecieverService.java
index e5cc77435..519f289d0 100644
--- a/apps/mobile/native/android/app/src/main/java/com/streetwriters/notesnook/BootRecieverService.java
+++ b/apps/mobile/native/android/app/src/main/java/com/streetwriters/notesnook/BootRecieverService.java
@@ -3,6 +3,7 @@ package com.streetwriters.notesnook;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
+import android.os.Build;
import android.util.Log;
import android.app.ActivityManager;
import com.facebook.react.HeadlessJsTaskService;
@@ -19,8 +20,12 @@ public class BootRecieverService extends BroadcastReceiver {
network connections
**/
Intent serviceIntent = new Intent(context, BootTaskService.class);
- context.startService(serviceIntent);
- HeadlessJsTaskService.acquireWakeLockNow(context);
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
+ context.startForegroundService(serviceIntent);
+ } else {
+ context.startService(serviceIntent);
+ }
+ HeadlessJsTaskService.acquireWakeLockNow(context);
}
}
diff --git a/apps/mobile/native/android/app/src/main/java/com/streetwriters/notesnook/BootTaskService.java b/apps/mobile/native/android/app/src/main/java/com/streetwriters/notesnook/BootTaskService.java
index 3e1c30533..60edfe16f 100644
--- a/apps/mobile/native/android/app/src/main/java/com/streetwriters/notesnook/BootTaskService.java
+++ b/apps/mobile/native/android/app/src/main/java/com/streetwriters/notesnook/BootTaskService.java
@@ -1,15 +1,24 @@
package com.streetwriters.notesnook;
+import android.app.Notification;
+import android.app.NotificationChannel;
+import android.app.NotificationManager;
+import android.content.Context;
import android.content.Intent;
+import android.util.Log;
+
+import androidx.core.app.NotificationCompat;
+
import com.facebook.react.HeadlessJsTaskService;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.jstasks.HeadlessJsTaskConfig;
import javax.annotation.Nullable;
public class BootTaskService extends HeadlessJsTaskService {
-
+ Notification notification;
@Override
protected @Nullable HeadlessJsTaskConfig getTaskConfig(Intent intent) {
+ Log.d("BootTask", "Task Started");
return new HeadlessJsTaskConfig(
"com.streetwriters.notesnook.BOOT_TASK",
Arguments.createMap(),
@@ -17,4 +26,31 @@ public class BootTaskService extends HeadlessJsTaskService {
false // optional: defines whether or not the task is allowed in foreground. Default is false
);
}
+
+ @Override
+ public void onHeadlessJsTaskFinish(int taskId) {
+ super.onHeadlessJsTaskFinish(taskId);
+ Log.d("BootTask", "Task completed");
+ stopSelf();
+ }
+
+ @Override
+ public int onStartCommand(Intent intent, int flags, int startId) {
+
+ if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
+ NotificationChannel channel = new NotificationChannel("com.streetwriters.notesnook",
+ "Default",
+ NotificationManager.IMPORTANCE_DEFAULT);
+
+ ((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).createNotificationChannel(channel);
+ notification = new NotificationCompat.Builder(this, channel.getId())
+ .setContentTitle("Sync on boot")
+ .setContentText("")
+ .setSmallIcon(R.drawable.ic_stat_name)
+ .build();
+ startForeground(1, notification);
+ }
+
+ return super.onStartCommand(intent, flags, startId);
+ }
}
\ No newline at end of file
diff --git a/apps/mobile/native/index.js b/apps/mobile/native/index.js
index f5fb76966..6243ad99d 100644
--- a/apps/mobile/native/index.js
+++ b/apps/mobile/native/index.js
@@ -11,6 +11,10 @@ import NetInfo from "@react-native-community/netinfo";
import { enableFreeze } from "react-native-screens";
import {BackgroundSync} from '../app/services/background-sync';
+BackgroundSync.registerHeadlessTask();
+BackgroundSync.start();
+Notifications.init();
+
enableFreeze(true);
NetInfo.configure({
reachabilityUrl: "https://notesnook.com",
@@ -19,8 +23,7 @@ NetInfo.configure({
return response?.status >= 200 && response?.status < 300;
}
});
-BackgroundSync.start();
-Notifications.init();
+
const appName = appJson.name;
if (Config.isTesting) {