add functions to android
This commit is contained in:
+2
-2
@@ -2,7 +2,7 @@ package expo.modules.backgroundnotificationhandler
|
||||
|
||||
import android.content.Context
|
||||
import com.google.firebase.messaging.RemoteMessage
|
||||
import expo.modules.blueskyswissarmy.sharedprefs.Preferences
|
||||
import expo.modules.blueskyswissarmy.sharedprefs.SharedPrefs
|
||||
|
||||
enum class NotificationType(
|
||||
val type: String,
|
||||
@@ -39,7 +39,7 @@ class BackgroundNotificationHandler(
|
||||
}
|
||||
|
||||
private fun mutateWithChatMessage(remoteMessage: RemoteMessage) {
|
||||
if (Preferences(context).getBoolean("playSoundChat") == true) {
|
||||
if (SharedPrefs(context).getBoolean("playSoundChat") == true) {
|
||||
// If oreo or higher
|
||||
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
|
||||
remoteMessage.data["channelId"] = "chat-messages"
|
||||
|
||||
+31
-10
@@ -1,6 +1,7 @@
|
||||
package expo.modules.backgroundnotificationhandler
|
||||
|
||||
import expo.modules.blueskyswissarmy.sharedprefs.Preferences
|
||||
import android.content.Context
|
||||
import expo.modules.blueskyswissarmy.sharedprefs.SharedPrefs
|
||||
import expo.modules.kotlin.modules.Module
|
||||
import expo.modules.kotlin.modules.ModuleDefinition
|
||||
|
||||
@@ -16,11 +17,22 @@ val DEFAULTS =
|
||||
"mutedThreads" to mapOf<String, List<String>>(),
|
||||
)
|
||||
|
||||
enum class BadgeType(val rawValue: String) {
|
||||
Generic("badgeCountGeneric"),
|
||||
Messages("badgeCountMessages"),
|
||||
}
|
||||
|
||||
const val INCREMENTED_FOR_KEY = "incremented-for-convos"
|
||||
|
||||
class ExpoBackgroundNotificationHandlerModule : Module() {
|
||||
companion object {
|
||||
var isForegrounded = false
|
||||
}
|
||||
|
||||
fun getContext(): Context {
|
||||
return appContext.reactContext ?: throw Error("Context is null")
|
||||
}
|
||||
|
||||
override fun definition() =
|
||||
ModuleDefinition {
|
||||
Name("ExpoBackgroundNotificationHandler")
|
||||
@@ -28,10 +40,10 @@ class ExpoBackgroundNotificationHandlerModule : Module() {
|
||||
OnCreate {
|
||||
val context = appContext.reactContext ?: throw Error("Context is null")
|
||||
DEFAULTS.forEach { (key, value) ->
|
||||
if (Preferences(context).hasValue(key)) {
|
||||
if (SharedPrefs(context).hasValue(key)) {
|
||||
return@forEach
|
||||
}
|
||||
Preferences(context)._setAnyValue(key, value)
|
||||
SharedPrefs(context)._setAnyValue(key, value)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -44,21 +56,30 @@ class ExpoBackgroundNotificationHandlerModule : Module() {
|
||||
}
|
||||
|
||||
AsyncFunction("getPrefsAsync") {
|
||||
val context = appContext.reactContext ?: throw Error("Context is null")
|
||||
val keys = DEFAULTS.keys
|
||||
return@AsyncFunction Preferences(context).getValues(keys)
|
||||
return@AsyncFunction SharedPrefs(getContext()).getValues(keys)
|
||||
}
|
||||
|
||||
AsyncFunction("resetGenericCountAsync") {
|
||||
// Not implemented
|
||||
SharedPrefs(getContext()).setValue(BadgeType.Generic.rawValue, 0f)
|
||||
}
|
||||
|
||||
AsyncFunction("maybeIncrementMessagesCountAsync") {
|
||||
// Not implemented
|
||||
AsyncFunction("maybeIncrementMessagesCountAsync") { convoId: String ->
|
||||
val prefs = SharedPrefs(getContext())
|
||||
if (!prefs.setContains(INCREMENTED_FOR_KEY, convoId)) {
|
||||
val curr = prefs.getFloat(BadgeType.Messages.rawValue) ?: 0f
|
||||
prefs.setValue(BadgeType.Messages.rawValue, curr + 1)
|
||||
}
|
||||
}
|
||||
|
||||
AsyncFunction("maybeDecrementMessagesCountAsync") {
|
||||
// Not implemented
|
||||
AsyncFunction("maybeDecrementMessagesCountAsync") { convoId: String ->
|
||||
val prefs = SharedPrefs(getContext())
|
||||
if (prefs.setContains(INCREMENTED_FOR_KEY, convoId)) {
|
||||
val curr = prefs.getFloat(BadgeType.Messages.rawValue) ?: 0f
|
||||
if (curr != 0f) {
|
||||
prefs.setValue(BadgeType.Messages.rawValue, curr - 1)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user