adds firebase token send
- firebase token is now sent when secret is updated.
This commit is contained in:
parent
9ea64fb460
commit
14e583c164
@ -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.
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -64,16 +64,11 @@ 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)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private fun createChannel(channelId: String, channelName: String) {
|
||||
val notificationChannel = NotificationChannel(
|
||||
channelId,
|
||||
|
@ -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())}})
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user