From 14e583c164448fb5812e153ecaa9c92a81dc939a Mon Sep 17 00:00:00 2001 From: raphael Date: Mon, 3 Jan 2022 12:28:45 +0100 Subject: [PATCH] adds firebase token send - firebase token is now sent when secret is updated. --- .../maenle/bump/MyFirebaseMessagingService.kt | 3 +- .../java/com/maenle/bump/ui/FirstFragment.kt | 9 +---- .../com/maenle/bump/util/BumpProcessor.kt | 38 ++++++++++--------- .../com/maenle/bump/util/RestSingleton.kt | 15 ++++++++ 4 files changed, 39 insertions(+), 26 deletions(-) diff --git a/app/src/main/java/com/maenle/bump/MyFirebaseMessagingService.kt b/app/src/main/java/com/maenle/bump/MyFirebaseMessagingService.kt index 0a8f8a5..d4fdc84 100644 --- a/app/src/main/java/com/maenle/bump/MyFirebaseMessagingService.kt +++ b/app/src/main/java/com/maenle/bump/MyFirebaseMessagingService.kt @@ -7,6 +7,8 @@ import androidx.core.content.ContextCompat import com.maenle.bump.util.sendNotification import com.google.firebase.messaging.FirebaseMessagingService import com.google.firebase.messaging.RemoteMessage +import com.maenle.bump.util.BumpProcessor +import com.maenle.bump.util.RestSingleton class MyFirebaseMessagingService : FirebaseMessagingService() { @@ -59,7 +61,6 @@ class MyFirebaseMessagingService : FirebaseMessagingService() { * @param token The new token. */ private fun sendRegistrationToServer(token: String?) { - // TODO: Implement this method to send token to your app server. } /** diff --git a/app/src/main/java/com/maenle/bump/ui/FirstFragment.kt b/app/src/main/java/com/maenle/bump/ui/FirstFragment.kt index 230d576..1737f05 100644 --- a/app/src/main/java/com/maenle/bump/ui/FirstFragment.kt +++ b/app/src/main/java/com/maenle/bump/ui/FirstFragment.kt @@ -64,14 +64,9 @@ class FirstFragment : Fragment() { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) - if(bump.hasSecret()){ - binding.buttonFirst.visibility = View.GONE - } else { - binding.buttonFirst.setOnClickListener { - findNavController().navigate(R.id.action_FirstFragment_to_SecondFragment) - } + binding.buttonFirst.setOnClickListener { + findNavController().navigate(R.id.action_FirstFragment_to_SecondFragment) } - } private fun createChannel(channelId: String, channelName: String) { diff --git a/app/src/main/java/com/maenle/bump/util/BumpProcessor.kt b/app/src/main/java/com/maenle/bump/util/BumpProcessor.kt index 70b6bb9..5b50be1 100644 --- a/app/src/main/java/com/maenle/bump/util/BumpProcessor.kt +++ b/app/src/main/java/com/maenle/bump/util/BumpProcessor.kt @@ -3,6 +3,9 @@ package com.maenle.bump.util import android.content.Context import android.os.Handler 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.JSONObject import java.io.File @@ -12,11 +15,12 @@ class BumpProcessor constructor(context: Context) { private var rest: RestSingleton = RestSingleton.getInstance(context) private var secret: String? = null private lateinit var log: JSONArray - private lateinit var messenger: MessageProcessor + private var messenger: MessageProcessor? = null init { secret = getSecret(context) log = getLog(context) + messenger = secret?.let { MessageProcessor(it) } } companion object { @@ -54,14 +58,8 @@ class BumpProcessor constructor(context: Context) { } private fun getSecret(context: Context):String? { - val secretFile = File(context.filesDir, ".bump_secrets") - !secretFile.exists().let { - secretFile.createNewFile() - } - for(line in secretFile.readLines()) { - return line - } - return null + val local = LocalData(context) + return local.code } fun startUpdateHandler(context: Context) { @@ -78,7 +76,7 @@ class BumpProcessor constructor(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) { @@ -86,15 +84,12 @@ class BumpProcessor constructor(context: Context) { logFile.appendText(line.toString()) } - fun addSecret(context: Context, newSecret: String) { - if(!hasSecret()) { - val secretFile = File(context.filesDir, ".bump_secrets") - !secretFile.exists().let { - secretFile.createNewFile() - } - secretFile.appendText(newSecret) - } + fun addSecret(context: Context, newCode: String) { + val local = LocalData(context) + local.code = newCode secret = getSecret(context) + + setFirebaseToken() } private fun getLog(context: Context): JSONArray { @@ -109,4 +104,11 @@ class BumpProcessor constructor(context: Context) { 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())}}) + } + } + } \ No newline at end of file diff --git a/app/src/main/java/com/maenle/bump/util/RestSingleton.kt b/app/src/main/java/com/maenle/bump/util/RestSingleton.kt index 73f335a..38a16f7 100644 --- a/app/src/main/java/com/maenle/bump/util/RestSingleton.kt +++ b/app/src/main/java/com/maenle/bump/util/RestSingleton.kt @@ -84,6 +84,21 @@ class RestSingleton constructor(context: Context){ 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 { // applicationContext is key, it keeps you from leaking the