-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathNotificationIntentHandler.kt
More file actions
173 lines (149 loc) · 7.07 KB
/
Copy pathNotificationIntentHandler.kt
File metadata and controls
173 lines (149 loc) · 7.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package chat.rocket.reactnative.notification
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.util.Log
import com.google.gson.GsonBuilder
import chat.rocket.reactnative.voip.VoipNotification
/**
* Handles notification Intent processing from MainActivity.
* Extracts notification data from Intents and stores it for React Native to process.
*/
class NotificationIntentHandler {
companion object {
private const val TAG = "RocketChat.NotificationIntentHandler"
/**
* Handles a notification Intent from MainActivity.
* Processes VoIP, video conf, and regular notification intents.
*/
@JvmStatic
fun handleIntent(context: Context, intent: Intent) {
if (VoipNotification.handleMainActivityVoipIntent(context, intent)) {
return
}
// Handle video conf action
if (handleVideoConfIntent(context, intent)) {
return
}
// Handle regular notification tap
handleNotificationIntent(context, intent)
}
/**
* Handles video conference notification Intent.
* @return true if this was a video conf intent, false otherwise
*/
@JvmStatic
private fun handleVideoConfIntent(context: Context, intent: Intent): Boolean {
if (!intent.getBooleanExtra("videoConfAction", false)) {
return false
}
val notificationId = intent.getIntExtra("notificationId", 0)
val event = intent.getStringExtra("event") ?: return true
val rid = intent.getStringExtra("rid") ?: ""
val callerId = intent.getStringExtra("callerId") ?: ""
val caller = intent.getStringExtra("caller") ?: ""
val host = intent.getStringExtra("host") ?: ""
val callId = intent.getStringExtra("callId") ?: ""
// Cancel the notification
if (notificationId != 0) {
VideoConfNotification.cancelById(context, notificationId)
}
// Store action for JS to pick up - include all required fields
val data = mapOf(
"notificationType" to "videoconf",
"rid" to rid,
"event" to event,
"host" to host,
"callId" to callId,
"caller" to mapOf(
"_id" to callerId,
"name" to caller
)
)
val gson = GsonBuilder().create()
val jsonData = gson.toJson(data)
VideoConfModule.storePendingAction(context, jsonData)
// Clear the video conf flag to prevent re-processing
intent.removeExtra("videoConfAction")
return true
}
/**
* Handles regular notification tap (non-video conf).
* Extracts Intent extras and stores them for React Native to pick up.
*/
@JvmStatic
private fun handleNotificationIntent(context: Context, intent: Intent) {
val extras = intent.extras ?: return
// Check if this Intent has notification data (ejson)
val ejson = extras.getString("ejson")
if (ejson.isNullOrEmpty()) {
return
}
try {
val notId = extras.getString("notId")
// Clear the notification messages from the static map to prevent stacking
if (!notId.isNullOrEmpty()) {
try {
val notIdInt = notId.toIntOrNull()
if (notIdInt != null) {
CustomPushNotification.clearMessages(notIdInt)
}
} catch (e: Exception) {
Log.e(TAG, "Error clearing notification messages for ID $notId: ${e.message}", e)
}
}
// Extract all notification data from Intent extras
// Only include serializable types to avoid JSON serialization errors
val notificationData = mutableMapOf<String, Any?>()
// Copy all extras to the notification data map, filtering out non-serializable types
extras.keySet().forEach { key ->
try {
when (val value = extras.get(key)) {
is String -> notificationData[key] = value
is Int -> notificationData[key] = value
is Boolean -> notificationData[key] = value
is Long -> notificationData[key] = value
is Float -> notificationData[key] = value
is Double -> notificationData[key] = value
is Byte -> notificationData[key] = value
is Char -> notificationData[key] = value
is Short -> notificationData[key] = value
// Skip complex types that can't be serialized (Bundle, Parcelable, etc.)
is Bundle -> {
// Skip Bundle objects - they're not JSON serializable
Log.w(TAG, "Skipping Bundle extra: $key")
}
null -> {
// Skip null values
}
else -> {
// For other types, try to convert to String only if it's a simple type
// Skip complex objects that might not serialize properly
val stringValue = value.toString()
// Only include if it's a reasonable string representation (not object reference)
if (!stringValue.startsWith("android.") && !stringValue.contains("@")) {
notificationData[key] = stringValue
} else {
Log.w(TAG, "Skipping non-serializable extra: $key (type: ${value.javaClass.simpleName})")
}
}
}
} catch (e: Exception) {
Log.w(TAG, "Error processing extra $key: ${e.message}")
}
}
// Convert to JSON and store for React Native
val gson = GsonBuilder().create()
val jsonData = gson.toJson(notificationData)
// Store notification data with error handling
try {
PushNotificationModule.storePendingNotification(context, jsonData)
} catch (e: Exception) {
Log.e(TAG, "Failed to store pending notification: ${e.message}", e)
}
} catch (e: Exception) {
Log.e(TAG, "Error handling notification intent: ${e.message}", e)
}
}
}
}