Make sure firebase is always subscribed to

This commit is contained in:
Philipp Heckel 2021-11-14 17:05:35 -05:00
parent 1616b27816
commit 8be6201ea2
2 changed files with 13 additions and 10 deletions

View file

@ -50,6 +50,7 @@ class MainActivity : AppCompatActivity(), ActionMode.Callback {
private var workManager: WorkManager? = null // Context-dependent private var workManager: WorkManager? = null // Context-dependent
private var notifier: NotificationService? = null // Context-dependent private var notifier: NotificationService? = null // Context-dependent
private var subscriberManager: SubscriberManager? = null // Context-dependent private var subscriberManager: SubscriberManager? = null // Context-dependent
private var appBaseUrl: String? = null // Context-dependent
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
@ -61,6 +62,7 @@ class MainActivity : AppCompatActivity(), ActionMode.Callback {
workManager = WorkManager.getInstance(this) workManager = WorkManager.getInstance(this)
notifier = NotificationService(this) notifier = NotificationService(this)
subscriberManager = SubscriberManager(this) subscriberManager = SubscriberManager(this)
appBaseUrl = getString(R.string.app_base_url)
// Action bar // Action bar
title = getString(R.string.main_action_bar_title) title = getString(R.string.main_action_bar_title)
@ -142,7 +144,7 @@ class MainActivity : AppCompatActivity(), ActionMode.Callback {
true true
} }
R.id.main_menu_website -> { R.id.main_menu_website -> {
startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(getString(R.string.app_base_url)))) startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(appBaseUrl)))
true true
} }
else -> super.onOptionsItemSelected(item) else -> super.onOptionsItemSelected(item)
@ -168,8 +170,8 @@ class MainActivity : AppCompatActivity(), ActionMode.Callback {
) )
viewModel.add(subscription) viewModel.add(subscription)
// Subscribe to Firebase topic (instant subscriptions are triggered in observe()) // Subscribe to Firebase topic if ntfy.sh (even if instant, just to be sure!)
if (!instant) { if (baseUrl == appBaseUrl) {
Log.d(TAG, "Subscribing to Firebase") Log.d(TAG, "Subscribing to Firebase")
FirebaseMessaging FirebaseMessaging
.getInstance() .getInstance()
@ -258,13 +260,14 @@ class MainActivity : AppCompatActivity(), ActionMode.Callback {
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if (requestCode == REQUEST_CODE_DELETE_SUBSCRIPTION && resultCode == RESULT_OK) { if (requestCode == REQUEST_CODE_DELETE_SUBSCRIPTION && resultCode == RESULT_OK) {
val subscriptionId = data?.getLongExtra(EXTRA_SUBSCRIPTION_ID, 0) val subscriptionId = data?.getLongExtra(EXTRA_SUBSCRIPTION_ID, 0)
val subscriptionBaseUrl = data?.getStringExtra(EXTRA_SUBSCRIPTION_BASE_URL)
val subscriptionTopic = data?.getStringExtra(EXTRA_SUBSCRIPTION_TOPIC) val subscriptionTopic = data?.getStringExtra(EXTRA_SUBSCRIPTION_TOPIC)
val subscriptionInstant = data?.getBooleanExtra(EXTRA_SUBSCRIPTION_INSTANT, false) val subscriptionInstant = data?.getBooleanExtra(EXTRA_SUBSCRIPTION_INSTANT, false)
Log.d(TAG, "Deleting subscription with subscription ID $subscriptionId (topic: $subscriptionTopic)") Log.d(TAG, "Deleting subscription with subscription ID $subscriptionId (topic: $subscriptionTopic)")
subscriptionId?.let { id -> viewModel.remove(id) } subscriptionId?.let { id -> viewModel.remove(id) }
subscriptionInstant?.let { instant -> subscriptionBaseUrl?.let { baseUrl ->
if (!instant) { if (baseUrl == appBaseUrl) {
Log.d(TAG, "Unsubscribing from Firebase") Log.d(TAG, "Unsubscribing from Firebase")
subscriptionTopic?.let { topic -> FirebaseMessaging.getInstance().unsubscribeFromTopic(topic) } subscriptionTopic?.let { topic -> FirebaseMessaging.getInstance().unsubscribeFromTopic(topic) }
} }

View file

@ -9,10 +9,10 @@
android:focusable="true"> android:focusable="true">
<ImageView <ImageView
android:layout_width="37dp" android:layout_width="37dp"
android:layout_height="37dp" app:srcCompat="@drawable/ic_sms_gray_48dp" android:layout_height="37dp" app:srcCompat="@drawable/ic_sms_gray_24dp"
android:id="@+id/main_item_image" app:layout_constraintTop_toTopOf="parent" android:id="@+id/main_item_image" app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="15dp" android:layout_marginTop="12dp"/> android:layout_marginStart="15dp" android:layout_marginTop="14dp"/>
<TextView <TextView
android:text="ntfy.sh/example" android:text="ntfy.sh/example"
android:layout_width="0dp" android:layout_width="0dp"
@ -32,16 +32,16 @@
android:layout_marginBottom="10dp"/> android:layout_marginBottom="10dp"/>
<ImageView <ImageView
android:layout_width="20dp" android:layout_width="20dp"
android:layout_height="20dp" app:srcCompat="@drawable/ic_bolt_black_24dp" android:layout_height="24dp" app:srcCompat="@drawable/ic_bolt_black_24dp"
android:id="@+id/main_item_instant_image" android:id="@+id/main_item_instant_image"
app:layout_constraintTop_toTopOf="@+id/main_item_text" app:layout_constraintTop_toTopOf="@+id/main_item_text"
app:layout_constraintEnd_toStartOf="@+id/main_item_date"/> app:layout_constraintEnd_toStartOf="@+id/main_item_date" android:paddingTop="3dp"/>
<TextView <TextView
android:text="10:13" android:text="10:13"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/main_item_date" android:layout_height="wrap_content" android:id="@+id/main_item_date"
app:layout_constraintTop_toTopOf="@+id/main_item_instant_image" app:layout_constraintTop_toTopOf="@+id/main_item_instant_image"
app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="15dp" app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="15dp"
/> android:paddingTop="2dp"/>
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>