feat(c2dm): temp functioning notifications impl
Signed-off-by: Infi <infi@infi.sh>
This commit is contained in:
parent
0696f1a8de
commit
5497933589
|
|
@ -3,6 +3,7 @@ package chat.revolt.c2dm
|
|||
import android.app.PendingIntent
|
||||
import android.content.Intent
|
||||
import android.content.pm.PackageManager
|
||||
import android.util.Log
|
||||
import androidx.core.app.ActivityCompat
|
||||
import androidx.core.app.NotificationCompat
|
||||
import androidx.core.app.NotificationManagerCompat
|
||||
|
|
@ -11,14 +12,22 @@ import androidx.core.app.RemoteInput
|
|||
import androidx.core.graphics.drawable.IconCompat
|
||||
import chat.revolt.R
|
||||
import chat.revolt.activities.MainActivity
|
||||
import chat.revolt.api.internals.SpecialUsers
|
||||
import chat.revolt.api.REVOLT_BASE
|
||||
import chat.revolt.api.RevoltJson
|
||||
import chat.revolt.api.internals.ULID
|
||||
import chat.revolt.api.routes.push.subscribePush
|
||||
import chat.revolt.api.schemas.Message
|
||||
import chat.revolt.api.schemas.User
|
||||
import chat.revolt.c2dm.ChannelRegistrator.Companion.CHANNEL_ID_GROUP_SOCIAL_FRIENDREQUESTS
|
||||
import chat.revolt.persistence.Database
|
||||
import chat.revolt.persistence.SqlStorage
|
||||
import com.bumptech.glide.Glide
|
||||
import com.google.firebase.messaging.FirebaseMessagingService
|
||||
import com.google.firebase.messaging.RemoteMessage
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import kotlinx.serialization.json.contentOrNull
|
||||
import kotlinx.serialization.json.jsonObject
|
||||
import kotlinx.serialization.json.jsonPrimitive
|
||||
|
||||
class HandlerService : FirebaseMessagingService() {
|
||||
override fun onNewToken(token: String) {
|
||||
|
|
@ -28,29 +37,102 @@ class HandlerService : FirebaseMessagingService() {
|
|||
}
|
||||
}
|
||||
|
||||
override fun onMessageReceived(message: RemoteMessage) {
|
||||
val integ = ULID.asInteger(ULID.makeNext())
|
||||
override fun onMessageReceived(fcmMessage: RemoteMessage) {
|
||||
/// TEMPORARY CODE, SCHEMA TO BE REPLACED
|
||||
val payloadString = fcmMessage.data["payload"]
|
||||
if (payloadString == null) {
|
||||
Log.e("HandlerService", "No payload in message, abort")
|
||||
return
|
||||
}
|
||||
|
||||
Log.d("HandlerService", payloadString)
|
||||
|
||||
val payload = RevoltJson.parseToJsonElement(payloadString).jsonObject
|
||||
val keys = payload.keys.toList().toString()
|
||||
Log.d("HandlerService", "following keys: $keys")
|
||||
|
||||
var authorIcon = payload["icon"]?.jsonPrimitive?.contentOrNull
|
||||
val message = payload["message"]?.jsonObject?.let {
|
||||
RevoltJson.decodeFromJsonElement(
|
||||
Message.serializer(),
|
||||
it
|
||||
)
|
||||
} ?: run {
|
||||
Log.e("HandlerService", "No message in payload, abort")
|
||||
return
|
||||
}
|
||||
|
||||
val user = payload["message"]?.jsonObject?.get("user")?.jsonObject?.let {
|
||||
RevoltJson.decodeFromJsonElement(
|
||||
User.serializer(),
|
||||
it
|
||||
)
|
||||
} ?: run {
|
||||
Log.e("HandlerService", "No message->user in payload, abort")
|
||||
return
|
||||
}
|
||||
|
||||
val notificationId = message.channel?.let { ULID.asInteger(it) } ?: run {
|
||||
Log.e("HandlerService", "No channel in message, abort")
|
||||
return
|
||||
}
|
||||
|
||||
if (authorIcon == null) {
|
||||
authorIcon =
|
||||
"$REVOLT_BASE/users/${message.author?.ifBlank { "0".repeat(26) }}/default_avatar"
|
||||
}
|
||||
|
||||
val db = Database(SqlStorage.driver)
|
||||
val channelName = message.channel.let {
|
||||
db.channelQueries.findById(it).executeAsOneOrNull()
|
||||
}?.let {
|
||||
when (it.channelType) {
|
||||
"DirectMessage" -> {
|
||||
user.displayName ?: user.username
|
||||
}
|
||||
|
||||
"TextChannel" -> {
|
||||
"#${it.name}"
|
||||
}
|
||||
|
||||
else -> {
|
||||
it.name ?: getString(R.string.unknown)
|
||||
}
|
||||
}
|
||||
} ?: getString(
|
||||
R.string.unknown
|
||||
)
|
||||
|
||||
val messageTimestamp = message.id?.let { ULID.asTimestamp(it) } ?: run {
|
||||
Log.e("HandlerService", "No message id in message, abort")
|
||||
return
|
||||
}
|
||||
|
||||
val bitmap = Glide.with(this)
|
||||
.asBitmap()
|
||||
.load("https://autumn.revolt.chat/avatars/GJXjHUC1X7tGEgHFhYOvtCByuNRd72qFwlztjKZUHP/bfe0842b74ae716139138574e8a1b749751e2968~3.jpg")
|
||||
.load(authorIcon)
|
||||
.circleCrop()
|
||||
.submit()
|
||||
.get()
|
||||
val jen =
|
||||
|
||||
val author =
|
||||
Person.Builder()
|
||||
.setBot(true)
|
||||
.setKey(SpecialUsers.JENNIFER)
|
||||
.setBot(user.bot != null)
|
||||
.setKey(message.author)
|
||||
.setIcon(IconCompat.createWithBitmap(bitmap))
|
||||
.setName("Jennifer")
|
||||
.setName(user.displayName ?: user.username)
|
||||
.build()
|
||||
var remoteInput = RemoteInput.Builder("Reply").run {
|
||||
setLabel("Reply")
|
||||
|
||||
val remoteInput = RemoteInput.Builder("content").run {
|
||||
setLabel(getString(R.string.message_context_sheet_actions_reply))
|
||||
build()
|
||||
}
|
||||
var action: NotificationCompat.Action =
|
||||
|
||||
val action: NotificationCompat.Action =
|
||||
NotificationCompat.Action.Builder(
|
||||
R.drawable.ic_reply_24dp,
|
||||
"Reply", PendingIntent.getActivity(
|
||||
getString(R.string.message_context_sheet_actions_reply),
|
||||
PendingIntent.getActivity(
|
||||
this,
|
||||
0,
|
||||
Intent(this, MainActivity::class.java),
|
||||
|
|
@ -59,15 +141,20 @@ class HandlerService : FirebaseMessagingService() {
|
|||
)
|
||||
.addRemoteInput(remoteInput)
|
||||
.build()
|
||||
var builder = NotificationCompat.Builder(this, CHANNEL_ID_GROUP_SOCIAL_FRIENDREQUESTS)
|
||||
|
||||
val builder = NotificationCompat.Builder(this, CHANNEL_ID_GROUP_SOCIAL_FRIENDREQUESTS)
|
||||
.setSmallIcon(R.drawable.ic_message_text_24dp)
|
||||
.setContentTitle("Jennifer")
|
||||
.setContentText("Expand for details")
|
||||
.setContentTitle(user.displayName ?: user.username)
|
||||
.setContentText(message.content)
|
||||
.setCategory(NotificationCompat.CATEGORY_MESSAGE)
|
||||
.setStyle(
|
||||
NotificationCompat.MessagingStyle(jen)
|
||||
.setConversationTitle("#Genderal")
|
||||
.addMessage("hiii 👋", System.currentTimeMillis(), jen)
|
||||
NotificationCompat.MessagingStyle(author)
|
||||
.setConversationTitle(channelName)
|
||||
.addMessage(
|
||||
message.content ?: getString(R.string.reply_message_empty_has_attachments),
|
||||
messageTimestamp,
|
||||
author
|
||||
)
|
||||
)
|
||||
.addAction(action)
|
||||
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
|
||||
|
|
@ -80,7 +167,8 @@ class HandlerService : FirebaseMessagingService() {
|
|||
) {
|
||||
return
|
||||
}
|
||||
notify(integ, builder.build())
|
||||
notify(notificationId, builder.build())
|
||||
}
|
||||
/// END TEMPORARY CODE
|
||||
}
|
||||
}
|
||||
|
|
@ -51,4 +51,9 @@ AND dmPartner = ?;
|
|||
findByServer:
|
||||
SELECT *
|
||||
FROM Channel
|
||||
WHERE server = ?;
|
||||
WHERE server = ?;
|
||||
|
||||
findById:
|
||||
SELECT *
|
||||
FROM Channel
|
||||
WHERE id = ?;
|
||||
Loading…
Reference in New Issue