From 0e6ae63ac413d1493a26736117f94f2cbde8aa30 Mon Sep 17 00:00:00 2001 From: KoalaSat Date: Fri, 7 Jun 2024 14:10:56 +0200 Subject: [PATCH] Android notification system --- mobile/android/app/build.gradle | 2 + .../android/app/src/main/AndroidManifest.xml | 2 + .../main/java/com/robosats/MainActivity.java | 57 ++++++++ .../robosats/workers/NotificationWorker.java | 48 +++++++ .../react-native-tor/android/build.gradle | 132 ------------------ 5 files changed, 109 insertions(+), 132 deletions(-) create mode 100644 mobile/android/app/src/main/java/com/robosats/workers/NotificationWorker.java delete mode 100644 mobile/patch_modules/react-native-tor/android/build.gradle diff --git a/mobile/android/app/build.gradle b/mobile/android/app/build.gradle index 42b7c578..f4c949cf 100644 --- a/mobile/android/app/build.gradle +++ b/mobile/android/app/build.gradle @@ -300,6 +300,8 @@ dependencies { implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.2" implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.2" + implementation "androidx.work:work-runtime:2.7.1" + if (enableHermes) { //noinspection GradleDynamicVersion implementation("com.facebook.react:hermes-engine:+") { // From node_modules diff --git a/mobile/android/app/src/main/AndroidManifest.xml b/mobile/android/app/src/main/AndroidManifest.xml index f76cd699..0a9777a3 100644 --- a/mobile/android/app/src/main/AndroidManifest.xml +++ b/mobile/android/app/src/main/AndroidManifest.xml @@ -3,6 +3,8 @@ + + 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { + schedulePeriodicTask(); + } else { + // Permission denied, handle accordingly + // Maybe show a message to the user explaining why the permission is necessary + } + } + } + + private void schedulePeriodicTask() { +// // Trigger the WorkManager setup and enqueueing here +// PeriodicWorkRequest periodicWorkRequest = +// new PeriodicWorkRequest.Builder(NotificationWorker.class, 15, TimeUnit.MINUTES) +// .build(); +// +// WorkManager.getInstance(getApplicationContext()) +// .enqueueUniquePeriodicWork("RobosatsNotificationsWork", +// ExistingPeriodicWorkPolicy.KEEP, periodicWorkRequest); + OneTimeWorkRequest workRequest = + new OneTimeWorkRequest.Builder(NotificationWorker.class) + .setInitialDelay(5, TimeUnit.SECONDS) + .build(); + + WorkManager.getInstance(getApplicationContext()) + .enqueue(workRequest); + } + public static class MainActivityDelegate extends ReactActivityDelegate { public MainActivityDelegate(ReactActivity activity, String mainComponentName) { super(activity, mainComponentName); diff --git a/mobile/android/app/src/main/java/com/robosats/workers/NotificationWorker.java b/mobile/android/app/src/main/java/com/robosats/workers/NotificationWorker.java new file mode 100644 index 00000000..22b2a7f8 --- /dev/null +++ b/mobile/android/app/src/main/java/com/robosats/workers/NotificationWorker.java @@ -0,0 +1,48 @@ +package com.robosats.workers; + +import android.app.NotificationChannel; +import android.app.NotificationManager; +import android.content.Context; + +import android.os.Build; +import android.util.Log; + +import androidx.core.app.NotificationCompat; +import androidx.work.Worker; +import androidx.work.WorkerParameters; + +import com.robosats.R; + +public class NotificationWorker extends Worker { + private static final String CHANNEL_ID = "robosats_notifications"; + private static final int NOTIFICATION_ID = 123; + public NotificationWorker(Context context, WorkerParameters params) { + super(context, params); + } + + @Override + public Result doWork() { + displayNotification("Order #1111", "Test from the app"); + return Result.success(); + } + + private void displayNotification(String title, String message) { + NotificationManager notificationManager = (NotificationManager) + getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE); + + NotificationChannel channel = new NotificationChannel(CHANNEL_ID, + "Robosats", + NotificationManager.IMPORTANCE_HIGH); + notificationManager.createNotificationChannel(channel); + + NotificationCompat.Builder builder = + new NotificationCompat.Builder(getApplicationContext(), CHANNEL_ID) + .setContentTitle(title) + .setContentText(message) + .setSmallIcon(R.mipmap.ic_launcher_round) + .setPriority(NotificationCompat.PRIORITY_DEFAULT); + + notificationManager.notify(NOTIFICATION_ID, builder.build()); + } +} + diff --git a/mobile/patch_modules/react-native-tor/android/build.gradle b/mobile/patch_modules/react-native-tor/android/build.gradle deleted file mode 100644 index f3d97dd9..00000000 --- a/mobile/patch_modules/react-native-tor/android/build.gradle +++ /dev/null @@ -1,132 +0,0 @@ -buildscript { - // Buildscript is evaluated before everything else so we can't use getExtOrDefault - def kotlin_version = rootProject.ext.has('kotlinVersion') ? rootProject.ext.get('kotlinVersion') : project.properties['Tor_kotlinVersion'] - - repositories { - google() - mavenCentral() - gradlePluginPortal() - } - - dependencies { - classpath 'com.android.tools.build:gradle:3.2.1' - // noinspection DifferentKotlinGradleVersion - classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" - } -} - -apply plugin: 'com.android.library' -apply plugin: 'kotlin-android' - -def getExtOrDefault(name) { - return rootProject.ext.has(name) ? rootProject.ext.get(name) : project.properties['Tor_' + name] -} - -def getExtOrIntegerDefault(name) { - return rootProject.ext.has(name) ? rootProject.ext.get(name) : (project.properties['Tor_' + name]).toInteger() -} - -android { - compileSdkVersion getExtOrIntegerDefault('compileSdkVersion') - buildToolsVersion getExtOrDefault('buildToolsVersion') - defaultConfig { - minSdkVersion rootProject.ext.has('minSdkVersion') ? rootProject.ext.minSdkVersion : 16 - targetSdkVersion getExtOrIntegerDefault('targetSdkVersion') - versionCode 1 - versionName "1.0" - - } - - buildTypes { - release { - minifyEnabled false - } - } - lintOptions { - disable 'GradleCompatible' - } - compileOptions { - sourceCompatibility JavaVersion.VERSION_1_8 - targetCompatibility JavaVersion.VERSION_1_8 - } -} - -repositories { - google() - mavenCentral() - gradlePluginPortal() - - def found = false - def defaultDir = null - def androidSourcesName = 'React Native sources' - - if (rootProject.ext.has('reactNativeAndroidRoot')) { - defaultDir = rootProject.ext.get('reactNativeAndroidRoot') - } else { - defaultDir = new File( - projectDir, - '/../../../node_modules/react-native/android' - ) - } - - if (defaultDir.exists()) { - maven { - url defaultDir.toString() - name androidSourcesName - } - - logger.info(":${project.name}:reactNativeAndroidRoot ${defaultDir.canonicalPath}") - found = true - } else { - def parentDir = rootProject.projectDir - - 1.upto(5, { - if (found) return true - parentDir = parentDir.parentFile - - def androidSourcesDir = new File( - parentDir, - 'node_modules/react-native' - ) - - def androidPrebuiltBinaryDir = new File( - parentDir, - 'node_modules/react-native/android' - ) - - if (androidPrebuiltBinaryDir.exists()) { - maven { - url androidPrebuiltBinaryDir.toString() - name androidSourcesName - } - - logger.info(":${project.name}:reactNativeAndroidRoot ${androidPrebuiltBinaryDir.canonicalPath}") - found = true - } else if (androidSourcesDir.exists()) { - maven { - url androidSourcesDir.toString() - name androidSourcesName - } - - logger.info(":${project.name}:reactNativeAndroidRoot ${androidSourcesDir.canonicalPath}") - found = true - } - }) - } - - if (!found) { - throw new GradleException( - "${project.name}: unable to locate React Native android sources. " + - "Ensure you have you installed React Native as a dependency in your project and try again." - ) - } -} - -def kotlin_version = getExtOrDefault('kotlinVersion') - -dependencies { - // noinspection GradleDynamicVersion - api 'com.facebook.react:react-native:+' - implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" - compileOnly files('libs/sifir_android.aar') -}