Skip to content

Commit

Permalink
Always use FirebaseMessaging instance to get fcmToken
Browse files Browse the repository at this point in the history
  • Loading branch information
p1gp1g committed Dec 26, 2023
1 parent d28b099 commit 3ffe096
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.util.Log
import org.unifiedpush.android.foss_embedded_fcm_distributor.Utils.getFCMToken
import org.unifiedpush.android.foss_embedded_fcm_distributor.Utils.removeToken
import org.unifiedpush.android.foss_embedded_fcm_distributor.Utils.saveToken
import org.unifiedpush.android.foss_embedded_fcm_distributor.Utils.sendNewEndpoint

private const val TAG = "UP-Embedded_distributor"

Expand All @@ -27,10 +25,6 @@ open class EmbeddedDistributorReceiver : BroadcastReceiver() {
Log.d(TAG, "Registering to the embedded distributor")
saveGetEndpoint(context)
saveToken(context, token)
getFCMToken(context)?.let {
sendNewEndpoint(context, it, token)
return
}
registerFCM(context)
}
ACTION_UNREGISTER -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,6 @@ import android.content.Context
import android.content.Intent

object Utils {
fun getFCMToken(context: Context): String? {
val prefs = context.getSharedPreferences(PREF_MASTER, Context.MODE_PRIVATE)
return prefs.getString(EXTRA_FCM_TOKEN, null)
}

fun saveFCMToken(context: Context, token: String) {
val prefs = context.getSharedPreferences(PREF_MASTER, Context.MODE_PRIVATE)
prefs.edit().putString(EXTRA_FCM_TOKEN, token).commit()
}

fun getTokens(context: Context): MutableSet<String> {
val prefs = context.getSharedPreferences(PREF_MASTER, Context.MODE_PRIVATE)
return prefs.getStringSet(PREF_MASTER_TOKENS, null)
Expand All @@ -25,7 +15,7 @@ object Utils {
if (!tokens.contains(token)) {
tokens.add(token)
val prefs = context.getSharedPreferences(PREF_MASTER, Context.MODE_PRIVATE)
prefs.edit().putStringSet(PREF_MASTER_TOKENS, tokens).commit()
prefs.edit().putStringSet(PREF_MASTER_TOKENS, tokens).apply()
}
}

Expand All @@ -34,7 +24,7 @@ object Utils {
if (tokens.contains(token)) {
tokens.remove(token)
val prefs = context.getSharedPreferences(PREF_MASTER, Context.MODE_PRIVATE)
prefs.edit().putStringSet(PREF_MASTER_TOKENS, tokens).commit()
prefs.edit().putStringSet(PREF_MASTER_TOKENS, tokens).apply()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import android.util.Log
import org.json.JSONObject
import org.unifiedpush.android.foss_embedded_fcm_distributor.*
import org.unifiedpush.android.foss_embedded_fcm_distributor.Utils.getTokens
import org.unifiedpush.android.foss_embedded_fcm_distributor.Utils.saveFCMToken
import org.unifiedpush.android.foss_embedded_fcm_distributor.Utils.sendNewEndpoint
import java.util.Timer
import kotlin.collections.HashMap
Expand Down Expand Up @@ -38,11 +37,10 @@ class FirebaseReceiver : BroadcastReceiver() {
return map
}

private fun onNewToken(context: Context, FCMToken: String) {
Log.d(TAG, "New FCM token: $FCMToken")
saveFCMToken(context, FCMToken)
private fun onNewToken(context: Context, fcmToken: String) {
Log.d(TAG, "New FCM token: $fcmToken")
getTokens(context).forEach {
sendNewEndpoint(context, FCMToken, it)
sendNewEndpoint(context, fcmToken, it)
}
}

Expand Down

0 comments on commit 3ffe096

Please sign in to comment.