From d52382ca5ba3f6f0148814933a8751506613f602 Mon Sep 17 00:00:00 2001 From: ua1zbe Date: Wed, 23 Sep 2026 09:17:43 +0300 Subject: [PATCH] Add RuStore Push integration (project_id placeholder) --- app/build.gradle.kts | 4 ++ app/src/main/AndroidManifest.xml | 21 ++++++- app/src/main/java/ru/ua1zbe/aprsmap/App.kt | 60 +++++++++++++++++++ .../java/ru/ua1zbe/aprsmap/MainActivity.kt | 17 ++++++ .../ru/ua1zbe/aprsmap/PushMessagingService.kt | 55 +++++++++++++++++ app/src/main/res/values/strings.xml | 2 + gradle.properties | 2 +- settings.gradle.kts | 1 + 8 files changed, 160 insertions(+), 2 deletions(-) create mode 100644 app/src/main/java/ru/ua1zbe/aprsmap/App.kt create mode 100644 app/src/main/java/ru/ua1zbe/aprsmap/PushMessagingService.kt diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 5eace8d..05697c1 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -55,4 +55,8 @@ kotlin { compilerOptions { jvmTarget.set(org.jetbrains.kotlin.gradle.dsl.JvmTarget.JVM_17) } +} + +dependencies { + implementation("ru.rustore.sdk:pushclient:7.4.0") } \ No newline at end of file diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 9de5042..3360576 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -1,10 +1,13 @@ - + + + + + + + + + + + + Log.i(TAG, "push token: $token") + saveToken(token) + RuStorePushClient.subscribeToTopic(PUSH_TOPIC) + .addOnSuccessListener { Log.i(TAG, "subscribed to $PUSH_TOPIC") } + .addOnFailureListener { e -> Log.w(TAG, "subscribe failed", e) } + } + .addOnFailureListener { e -> Log.w(TAG, "getToken failed", e) } + } catch (e: Exception) { + Log.w(TAG, "init failed", e) + } + } + + private fun saveToken(token: String) { + getSharedPreferences(PREFS, MODE_PRIVATE) + .edit().putString("push_token", token).apply() + } + + companion object { + private const val TAG = "RuStorePush" + private const val RUSTORE_PROJECT_ID = "" + private const val PUSH_TOPIC = "aprsmap" + const val PREFS = "aprsmap" + } +} \ No newline at end of file diff --git a/app/src/main/java/ru/ua1zbe/aprsmap/MainActivity.kt b/app/src/main/java/ru/ua1zbe/aprsmap/MainActivity.kt index 8f895ff..54ef46c 100644 --- a/app/src/main/java/ru/ua1zbe/aprsmap/MainActivity.kt +++ b/app/src/main/java/ru/ua1zbe/aprsmap/MainActivity.kt @@ -1,9 +1,11 @@ package ru.ua1zbe.aprsmap import android.annotation.SuppressLint +import android.Manifest import android.app.Activity import android.app.AlertDialog import android.content.Intent +import android.content.pm.PackageManager import android.graphics.Color import android.graphics.PorterDuff import android.graphics.PorterDuffColorFilter @@ -104,10 +106,24 @@ class MainActivity : Activity() { root.addView(progress) setContentView(root) + requestNotificationPermission() + webView.loadUrl(getString(R.string.map_url)) checkForUpdates() } + private fun requestNotificationPermission() { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU && + checkSelfPermission(Manifest.permission.POST_NOTIFICATIONS) != + PackageManager.PERMISSION_GRANTED + ) { + requestPermissions( + arrayOf(Manifest.permission.POST_NOTIFICATIONS), + REQ_NOTIFICATIONS + ) + } + } + private fun checkForUpdates() { Thread { val version = try { @@ -237,5 +253,6 @@ class MainActivity : Activity() { companion object { private const val TAG = "APRSmap" + private const val REQ_NOTIFICATIONS = 100 } } \ No newline at end of file diff --git a/app/src/main/java/ru/ua1zbe/aprsmap/PushMessagingService.kt b/app/src/main/java/ru/ua1zbe/aprsmap/PushMessagingService.kt new file mode 100644 index 0000000..71aea8e --- /dev/null +++ b/app/src/main/java/ru/ua1zbe/aprsmap/PushMessagingService.kt @@ -0,0 +1,55 @@ +package ru.ua1zbe.aprsmap + +import android.app.Notification +import android.app.NotificationManager +import android.content.Context +import android.util.Log +import ru.rustore.sdk.pushclient.messaging.exception.RuStorePushClientException +import ru.rustore.sdk.pushclient.messaging.model.RemoteMessage +import ru.rustore.sdk.pushclient.messaging.service.RuStoreMessagingService + +class PushMessagingService : RuStoreMessagingService() { + + override fun onNewToken(token: String) { + Log.i(TAG, "onNewToken: $token") + getSharedPreferences(App.PREFS, Context.MODE_PRIVATE) + .edit().putString("push_token", token).apply() + } + + override fun onMessageReceived(message: RemoteMessage) { + if (message.notification != null) { + Log.i(TAG, "push displayed by SDK: ${message.data}") + return + } + val title = message.data["title"] ?: getString(R.string.app_name) + val body = message.data["body"] ?: "" + if (title.isNotEmpty() || body.isNotEmpty()) { + showPush(title, body) + } + } + + override fun onDeletedMessages() { + Log.i(TAG, "onDeletedMessages") + } + + override fun onError(errors: List) { + errors.forEach { Log.w(TAG, "error", it) } + } + + private fun showPush(title: String, body: String) { + val nm = getSystemService(NotificationManager::class.java) + val n = Notification.Builder(this, CHANNEL_ID) + .setContentTitle(title) + .setContentText(body) + .setSmallIcon(R.drawable.ic_launcher_foreground) + .setAutoCancel(true) + .build() + nm.notify(NOTIFICATION_ID, n) + } + + companion object { + private const val TAG = "RuStorePush" + const val CHANNEL_ID = "aprs_push" + private const val NOTIFICATION_ID = 2 + } +} \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 81bfca5..7948ad5 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -2,6 +2,8 @@ APRS RadioRubka https://aprs.radiorubka.org/?center=67.5096,32.8271&zoom=8 + aprs_push + Пуш-уведомления Доступно обновление Версия %1$s уже вышла. Обновить приложение? Обновить diff --git a/gradle.properties b/gradle.properties index 295c9e9..bed1f61 100644 --- a/gradle.properties +++ b/gradle.properties @@ -6,4 +6,4 @@ org.gradle.parallel=false org.gradle.caching=false kotlin.compiler.execution.strategy=in-process android.nonTransitiveRClass=true -android.useAndroidX=false \ No newline at end of file +android.useAndroidX=true \ No newline at end of file diff --git a/settings.gradle.kts b/settings.gradle.kts index 5ff56b0..89f28d2 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -11,6 +11,7 @@ dependencyResolutionManagement { repositories { google() mavenCentral() + maven("https://nexus-external.rustore.ru/repository/maven-rustore-exposed/") } }