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

View file

@ -9,10 +9,10 @@
android:focusable="true">
<ImageView
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"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="15dp" android:layout_marginTop="12dp"/>
android:layout_marginStart="15dp" android:layout_marginTop="14dp"/>
<TextView
android:text="ntfy.sh/example"
android:layout_width="0dp"
@ -32,16 +32,16 @@
android:layout_marginBottom="10dp"/>
<ImageView
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"
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
android:text="10:13"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/main_item_date"
app:layout_constraintTop_toTopOf="@+id/main_item_instant_image"
app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="15dp"
/>
android:paddingTop="2dp"/>
</androidx.constraintlayout.widget.ConstraintLayout>