🐛 Dozen of hot fixes
This commit is contained in:
@ -98,7 +98,7 @@
|
||||
<receiver
|
||||
android:name=".receiver.ReplyReceiver"
|
||||
android:enabled="true"
|
||||
android:exported="false" />
|
||||
android:exported="true" />
|
||||
|
||||
<service
|
||||
android:name=".service.MessagingService"
|
||||
|
@ -1,14 +1,39 @@
|
||||
package dev.solsynth.solian
|
||||
|
||||
import android.content.Intent
|
||||
import io.flutter.embedding.android.FlutterActivity
|
||||
import io.flutter.embedding.engine.FlutterEngine
|
||||
import io.flutter.plugin.common.MethodChannel
|
||||
import io.flutter.plugins.sharedpreferences.LegacySharedPreferencesPlugin
|
||||
|
||||
class MainActivity : FlutterActivity()
|
||||
{
|
||||
private val CHANNEL = "dev.solsynth.solian/notifications"
|
||||
|
||||
override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
|
||||
super.configureFlutterEngine(flutterEngine)
|
||||
// https://github.com/flutter/flutter/issues/153075#issuecomment-2693189362
|
||||
flutterEngine.plugins.add(LegacySharedPreferencesPlugin())
|
||||
|
||||
MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler { call, result ->
|
||||
if (call.method == "initialLink") {
|
||||
val roomId = intent.getStringExtra("room_id")
|
||||
if (roomId != null) {
|
||||
result.success("/rooms/$roomId")
|
||||
} else {
|
||||
result.success(null)
|
||||
}
|
||||
} else {
|
||||
result.notImplemented()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onNewIntent(intent: Intent) {
|
||||
super.onNewIntent(intent)
|
||||
val roomId = intent.getStringExtra("room_id")
|
||||
if (roomId != null) {
|
||||
MethodChannel(flutterEngine!!.dartExecutor.binaryMessenger, CHANNEL).invokeMethod("newLink", "/rooms/$roomId")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,48 +1,47 @@
|
||||
package dev.solsynth.solian.network
|
||||
|
||||
import android.content.Context
|
||||
import okhttp3.*
|
||||
import android.content.SharedPreferences
|
||||
import okhttp3.Call
|
||||
import okhttp3.Callback
|
||||
import okhttp3.MediaType.Companion.toMediaType
|
||||
import okhttp3.OkHttpClient
|
||||
import okhttp3.Request
|
||||
import okhttp3.RequestBody.Companion.toRequestBody
|
||||
import okhttp3.Response
|
||||
import org.json.JSONObject
|
||||
import java.io.IOException
|
||||
|
||||
class ApiClient(private val context: Context) {
|
||||
private val client = OkHttpClient()
|
||||
private val sharedPreferences: SharedPreferences = context.getSharedPreferences("FlutterSharedPreferences", Context.MODE_PRIVATE)
|
||||
|
||||
fun sendMessage(roomId: String, content: String, repliedMessageId: String, callback: () -> Unit) {
|
||||
val prefs = context.getSharedPreferences("FlutterSharedPreferences", Context.MODE_PRIVATE)
|
||||
val token = prefs.getString("flutter.token", null)
|
||||
val serverUrl = prefs.getString("flutter.serverUrl", null)
|
||||
|
||||
if (token == null || serverUrl == null) {
|
||||
fun sendMessage(roomId: String, message: String, replyTo: String, callback: (Boolean) -> Unit) {
|
||||
val token = sharedPreferences.getString("flutter.token", null)
|
||||
if (token == null) {
|
||||
callback(false)
|
||||
return
|
||||
}
|
||||
|
||||
val url = "$serverUrl/chat/$roomId/messages"
|
||||
|
||||
val json = JSONObject()
|
||||
json.put("content", content)
|
||||
json.put("replied_message_id", repliedMessageId)
|
||||
|
||||
val requestBody = json.toString().toRequestBody("application/json; charset=utf-8".toMediaType())
|
||||
|
||||
val json = JSONObject().apply {
|
||||
put("content", message)
|
||||
put("reply_to", replyTo)
|
||||
}
|
||||
val body = json.toString().toRequestBody("application/json; charset=utf-8".toMediaType())
|
||||
val request = Request.Builder()
|
||||
.url(url)
|
||||
.post(requestBody)
|
||||
.addHeader("Authorization", "AtField $token")
|
||||
.url("https://solian.dev/api/rooms/$roomId/messages")
|
||||
.header("Authorization", "Bearer $token")
|
||||
.post(body)
|
||||
.build()
|
||||
|
||||
client.newCall(request).enqueue(object : Callback {
|
||||
override fun onFailure(call: Call, e: IOException) {
|
||||
// Handle failure
|
||||
callback()
|
||||
callback(false)
|
||||
}
|
||||
|
||||
override fun onResponse(call: Call, response: Response) {
|
||||
// Handle success
|
||||
callback()
|
||||
callback(response.isSuccessful)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
@ -72,6 +72,7 @@ class MessagingService: FirebaseMessagingService() {
|
||||
|
||||
val intent = Intent(this, MainActivity::class.java)
|
||||
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
|
||||
intent.putExtra("room_id", roomId)
|
||||
val pendingIntent = PendingIntent.getActivity(this, 0, intent, pendingIntentFlags)
|
||||
|
||||
val notificationBuilder = NotificationCompat.Builder(this, "messages")
|
||||
|
Reference in New Issue
Block a user