adds firebase token send

- firebase token is now sent when secret
  is updated.
This commit is contained in:
Raphael Maenle 2022-01-03 12:28:45 +01:00
parent 9ea64fb460
commit 14e583c164
4 changed files with 39 additions and 26 deletions

View File

@ -7,6 +7,8 @@ import androidx.core.content.ContextCompat
import com.maenle.bump.util.sendNotification import com.maenle.bump.util.sendNotification
import com.google.firebase.messaging.FirebaseMessagingService import com.google.firebase.messaging.FirebaseMessagingService
import com.google.firebase.messaging.RemoteMessage import com.google.firebase.messaging.RemoteMessage
import com.maenle.bump.util.BumpProcessor
import com.maenle.bump.util.RestSingleton
class MyFirebaseMessagingService : FirebaseMessagingService() { class MyFirebaseMessagingService : FirebaseMessagingService() {
@ -59,7 +61,6 @@ class MyFirebaseMessagingService : FirebaseMessagingService() {
* @param token The new token. * @param token The new token.
*/ */
private fun sendRegistrationToServer(token: String?) { private fun sendRegistrationToServer(token: String?) {
// TODO: Implement this method to send token to your app server.
} }
/** /**

View File

@ -64,14 +64,9 @@ class FirstFragment : Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState) super.onViewCreated(view, savedInstanceState)
if(bump.hasSecret()){ binding.buttonFirst.setOnClickListener {
binding.buttonFirst.visibility = View.GONE findNavController().navigate(R.id.action_FirstFragment_to_SecondFragment)
} else {
binding.buttonFirst.setOnClickListener {
findNavController().navigate(R.id.action_FirstFragment_to_SecondFragment)
}
} }
} }
private fun createChannel(channelId: String, channelName: String) { private fun createChannel(channelId: String, channelName: String) {

View File

@ -3,6 +3,9 @@ package com.maenle.bump.util
import android.content.Context import android.content.Context
import android.os.Handler import android.os.Handler
import android.os.Looper import android.os.Looper
import android.util.Log
import com.google.firebase.ktx.Firebase
import com.google.firebase.messaging.FirebaseMessaging
import org.json.JSONArray import org.json.JSONArray
import org.json.JSONObject import org.json.JSONObject
import java.io.File import java.io.File
@ -12,11 +15,12 @@ class BumpProcessor constructor(context: Context) {
private var rest: RestSingleton = RestSingleton.getInstance(context) private var rest: RestSingleton = RestSingleton.getInstance(context)
private var secret: String? = null private var secret: String? = null
private lateinit var log: JSONArray private lateinit var log: JSONArray
private lateinit var messenger: MessageProcessor private var messenger: MessageProcessor? = null
init { init {
secret = getSecret(context) secret = getSecret(context)
log = getLog(context) log = getLog(context)
messenger = secret?.let { MessageProcessor(it) }
} }
companion object { companion object {
@ -54,14 +58,8 @@ class BumpProcessor constructor(context: Context) {
} }
private fun getSecret(context: Context):String? { private fun getSecret(context: Context):String? {
val secretFile = File(context.filesDir, ".bump_secrets") val local = LocalData(context)
!secretFile.exists().let { return local.code
secretFile.createNewFile()
}
for(line in secretFile.readLines()) {
return line
}
return null
} }
fun startUpdateHandler(context: Context) { fun startUpdateHandler(context: Context) {
@ -78,7 +76,7 @@ class BumpProcessor constructor(context: Context) {
} }
private fun updateFromServer(context: Context) { private fun updateFromServer(context: Context) {
rest.list(messenger.sender) {list -> updateLog(context, list)} messenger?.let {rest.list(it.sender) {list -> updateLog(context, list)}}
} }
private fun addToLog(context: Context, line: JSONObject) { private fun addToLog(context: Context, line: JSONObject) {
@ -86,15 +84,12 @@ class BumpProcessor constructor(context: Context) {
logFile.appendText(line.toString()) logFile.appendText(line.toString())
} }
fun addSecret(context: Context, newSecret: String) { fun addSecret(context: Context, newCode: String) {
if(!hasSecret()) { val local = LocalData(context)
val secretFile = File(context.filesDir, ".bump_secrets") local.code = newCode
!secretFile.exists().let {
secretFile.createNewFile()
}
secretFile.appendText(newSecret)
}
secret = getSecret(context) secret = getSecret(context)
setFirebaseToken()
} }
private fun getLog(context: Context): JSONArray { private fun getLog(context: Context): JSONArray {
@ -109,4 +104,11 @@ class BumpProcessor constructor(context: Context) {
return log return log
} }
private fun setFirebaseToken() {
val tokenTask = FirebaseMessaging.getInstance().token
tokenTask.addOnSuccessListener { token -> (
messenger?.let { rest.firebase(it.sender, token) {result -> Log.d("result", result.toString())}})
}
}
} }

View File

@ -84,6 +84,21 @@ class RestSingleton constructor(context: Context){
requestQueue.add(stringRequest) requestQueue.add(stringRequest)
} }
fun firebase(sender: String, firebase_token: String, callback: (JSONObject) -> Unit) {
val url = URL + "firebase/"
val data = JSONObject()
data.put("sender", sender)
data.put("token", firebase_token)
val stringRequest = JsonObjectRequest(Request.Method.POST, url,
data,
{ response -> callback(JSONObject(response.toString())) },
{callback(JSONObject())})
requestQueue.add(stringRequest)
}
private val requestQueue: RequestQueue by lazy { private val requestQueue: RequestQueue by lazy {
// applicationContext is key, it keeps you from leaking the // applicationContext is key, it keeps you from leaking the