1
1
package com.troplo.privateuploader.api
2
2
3
+ import android.app.Notification
4
+ import android.app.NotificationManager
5
+ import android.app.PendingIntent
3
6
import android.content.Context
7
+ import android.content.Intent
8
+ import android.graphics.drawable.Icon
9
+ import androidx.compose.material.icons.Icons
10
+ import androidx.compose.material.icons.filled.Reply
11
+ import androidx.core.app.NotificationCompat
12
+ import androidx.core.app.NotificationManagerCompat
13
+ import androidx.core.app.RemoteInput
14
+ import androidx.core.content.ContextCompat.getSystemService
15
+ import com.google.gson.Gson
16
+ import com.troplo.privateuploader.R
17
+ import com.troplo.privateuploader.data.model.MessageEvent
4
18
import io.socket.client.IO
5
- import io.socket.client.Manager
6
19
import io.socket.client.Socket
7
- import java.net.URI
20
+ import org.json.JSONObject
8
21
import java.net.URISyntaxException
9
22
import java.util.Collections
10
23
@@ -13,8 +26,9 @@ object SocketHandler {
13
26
private const val SERVER_URL = " http://192.168.0.12:34582" // Replace with your Socket.io server URL
14
27
15
28
private var socket: Socket ? = null
29
+ private val gson = Gson ()
16
30
17
- fun initializeSocket (token : String ) {
31
+ fun initializeSocket (token : String , context : Context ) {
18
32
try {
19
33
val options = IO .Options ()
20
34
options.forceNew = true
@@ -32,10 +46,51 @@ object SocketHandler {
32
46
socket?.on(Socket .EVENT_CONNECT_ERROR ) {
33
47
println (" Socket connect error ${socket?.isActive} " )
34
48
}
35
- // socket on any other events
36
- socket?.on(" message" ) {
37
- println (" Socket $it " )
38
- }
49
+ /* socket?.on("message") {
50
+ val jsonArray = it[0] as JSONObject
51
+ val payload = jsonArray.toString()
52
+ val messageEvent = gson.fromJson(payload, MessageEvent::class.java)
53
+
54
+ val message = messageEvent.message
55
+
56
+ val notificationBuilder = NotificationCompat.Builder(context, "communications")
57
+ .setSmallIcon(R.drawable.ic_launcher_foreground)
58
+ .setContentTitle(message.user?.username)
59
+ .setContentText(message.content)
60
+ .setPriority(NotificationCompat.PRIORITY_DEFAULT)
61
+ .setContentIntent(null)
62
+
63
+ val remoteInput: RemoteInput = RemoteInput.Builder("reply").run {
64
+ setLabel("Reply")
65
+ build()
66
+ }
67
+
68
+ /* val replyPendingIntent: PendingIntent =
69
+ PendingIntent.getBroadcast(
70
+ context,
71
+ messageEvent.association.id,
72
+ getMessageReplyIntent(messageEvent.association.id),
73
+ PendingIntent.FLAG_MUTABLE
74
+ )
75
+
76
+ val action: NotificationCompat.Action =
77
+ NotificationCompat.Action.Builder(
78
+ null,
79
+ "Reply",
80
+ replyPendingIntent
81
+ )
82
+ .addRemoteInput(remoteInput)
83
+ .build()
84
+ */
85
+ val newMessageNotification = Notification.Builder(context, "communications")
86
+ .setSmallIcon(R.drawable.ic_launcher_foreground)
87
+ .setContentTitle(message.user?.username)
88
+ .setContentText(message.content)
89
+ .build()
90
+ val notificationManager: NotificationManager =
91
+ context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
92
+ notificationManager.notify(message.id, newMessageNotification)
93
+ }*/
39
94
println (" Socket connected ${socket?.isActive} " )
40
95
} else {
41
96
println (" Socket is null" )
@@ -45,6 +100,14 @@ object SocketHandler {
45
100
}
46
101
}
47
102
103
+ private fun getMessageReplyIntent (associationId : Int ): Intent {
104
+ return Intent ()
105
+ .addFlags(Intent .FLAG_INCLUDE_STOPPED_PACKAGES )
106
+ .setAction(" reply" )
107
+ .putExtra(" associationId" , associationId)
108
+ }
109
+
110
+
48
111
fun getSocket (): Socket ? {
49
112
return socket
50
113
}
0 commit comments