Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Notification preferences update #15

Merged
merged 5 commits into from
Apr 26, 2024
Merged

Conversation

stigi
Copy link
Member

@stigi stigi commented Apr 19, 2024

In this PR:

Similar to magicbell/magicbell-swift#30 and magicbell/magicbell-swift#34 this PR introduces the new notification preferences response shape in the Android SDK.

Test Plan

I've tested the in the example app by calling user.preferences.fetch() and user.preferences.updateChannel("liked-a-post", "email", false) and logging the notifications preference response afterwards.

I've also observed the traffic that went over the network with ProxyMan.

Patch with test code
diff --git a/example/src/main/java/com/magicbell/example/MainActivity.kt b/example/src/main/java/com/magicbell/example/MainActivity.kt
index 2e70a28..dc54a70 100644
--- a/example/src/main/java/com/magicbell/example/MainActivity.kt
+++ b/example/src/main/java/com/magicbell/example/MainActivity.kt
@@ -2,6 +2,7 @@ package com.magicbell.example

 import android.annotation.SuppressLint
 import android.os.Bundle
+import android.util.Log
 import android.view.Menu
 import android.view.MenuItem
 import androidx.appcompat.app.AlertDialog
@@ -18,6 +19,7 @@ import com.magicbell.example.databinding.ActivityMainBinding
 import com.magicbell.example.modal.NotificationActionsSheetFragment
 import com.magicbell.sdk.User
 import com.magicbell.sdk.feature.notification.Notification
+import com.magicbell.sdk.feature.notificationpreferences.NotificationPreferences
 import com.magicbell.sdk.feature.store.NotificationStore
 import com.magicbell.sdk.feature.store.NotificationStoreContentObserver
 import com.magicbell.sdk.feature.store.NotificationStoreCountObserver
@@ -32,6 +34,7 @@ import com.magicbell.sdk.feature.store.markAsRead
 import com.magicbell.sdk.feature.store.markAsUnread
 import com.magicbell.sdk.feature.store.refresh
 import com.magicbell.sdk.feature.store.unarchive
+import kotlinx.coroutines.runBlocking

 class MainActivity : AppCompatActivity(), NotificationActionsSheetFragment.ActionListener, NotificationStoreContentObserver, NotificationStoreCountObserver {

@@ -95,11 +98,33 @@ class MainActivity : AppCompatActivity(), NotificationActionsSheetFragment.Actio
     recyclerView.addOnScrollListener(scrollListener)
   }

+  private fun logPrefs(prefs: NotificationPreferences) {
+    val tag = "Notification Preferences"
+    prefs.categories.forEach { cat ->
+      Log.i(tag, "Category: ${cat.label} - ${cat.slug}")
+      cat.channels.forEach { ch ->
+        Log.i(tag, "Channel: ${ch.label} - ${ch.slug} - ${ch.enabled}")
+      }
+    }
+  }
+
   private fun initMagicBell() {
-    user = (application as ExampleApplication).magicBellClient.connectUserEmail("john@doe.com")
+    user = (application as ExampleApplication).magicBellClient.connectUserEmailHmac("hi@ullrich.is", "8WBH7CafWZiCPVQTdLREZ87TfbM0J2psvgiMHadMp6c=")
     store = user.store.forAll()
     store.addContentObserver(this)
     store.addCountObserver(this)
+
+    runBlocking {
+      val result = user.preferences.fetch()
+      runOnUiThread {
+        logPrefs(result.getOrThrow())
+      }
+
+      val updateResult = user.preferences.updateChannel("liked-a-post", "email", false)
+      runOnUiThread {
+        logPrefs(updateResult.getOrThrow())
+      }
+    }
   }

   override fun onCreateOptionsMenu(menu: Menu): Boolean {

Todo before moving this out of draft state

  • Skip the 'label' keys when encoding NotificationPreferenceEntity objects, as PUT on the notifications_preferences endpoint does not expect those keys. The API calls work though, as the key is gracefully ignored, but for correctness I still want to address this I decided against investing more time into this. The Kotlin Serialization framework is cumbersome tbh.
  • ✅ Update the docs

Base automatically changed from ullrich/updating-hmac to main April 22, 2024 08:52
@stigi stigi force-pushed the ullrich/notification-preferences-update branch from d7deefe to 65bee52 Compare April 24, 2024 14:43
@stigi stigi marked this pull request as ready for review April 24, 2024 15:52
@stigi stigi merged commit 0f10f3f into main Apr 26, 2024
@stigi stigi deleted the ullrich/notification-preferences-update branch April 26, 2024 10:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants