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