Skip to content

Commit 88e5516

Browse files
committed
feat: refine notification title handling based on message type
- Enhanced CustomPushNotification to dynamically set notification titles for groups, direct messages, and omnichannel based on the Ejson payload. - Updated Ejson class to include a room name field for better context in notifications. - Improved iOS NotificationService to determine notification titles based on payload type, ensuring accurate representation of sender or room names.
1 parent 07a46b3 commit 88e5516

File tree

3 files changed

+55
-3
lines changed

3 files changed

+55
-3
lines changed

android/app/src/main/java/chat/rocket/reactnative/notification/CustomPushNotification.java

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -379,10 +379,27 @@ private Notification.Builder buildNotification(int notificationId) {
379379
Boolean notificationLoaded = mBundle.getBoolean("notificationLoaded", false);
380380
Ejson ejson = safeFromJson(mBundle.getString("ejson", "{}"), Ejson.class);
381381

382+
// Determine the correct title based on notification type
383+
String notificationTitle = title;
384+
if (ejson != null && ejson.type != null) {
385+
if ("p".equals(ejson.type) || "c".equals(ejson.type)) {
386+
// For groups/channels, use room name if available, otherwise fall back to title
387+
notificationTitle = (ejson.name != null && !ejson.name.isEmpty()) ? ejson.name : title;
388+
} else if ("d".equals(ejson.type)) {
389+
// For direct messages, use title (sender name from server)
390+
notificationTitle = title;
391+
} else if ("l".equals(ejson.type)) {
392+
// For omnichannel, use sender name if available, otherwise fall back to title
393+
notificationTitle = (ejson.sender != null && ejson.sender.name != null && !ejson.sender.name.isEmpty())
394+
? ejson.sender.name : title;
395+
}
396+
}
397+
382398
if (ENABLE_VERBOSE_LOGS) {
383399
Log.d(TAG, "[buildNotification] notId=" + notId);
384400
Log.d(TAG, "[buildNotification] notificationLoaded=" + notificationLoaded);
385401
Log.d(TAG, "[buildNotification] title=" + (title != null ? "[present]" : "[null]"));
402+
Log.d(TAG, "[buildNotification] notificationTitle=" + (notificationTitle != null ? "[present]" : "[null]"));
386403
Log.d(TAG, "[buildNotification] message length=" + (message != null ? message.length() : 0));
387404
}
388405

@@ -406,7 +423,7 @@ private Notification.Builder buildNotification(int notificationId) {
406423
}
407424

408425
notification
409-
.setContentTitle(title)
426+
.setContentTitle(notificationTitle)
410427
.setContentText(message)
411428
.setContentIntent(pendingIntent)
412429
.setPriority(Notification.PRIORITY_HIGH)
@@ -562,7 +579,23 @@ private void notificationStyle(Notification.Builder notification, int notId, Bun
562579
}
563580

564581
String title = bundle.getString("title");
565-
messageStyle.setConversationTitle(title);
582+
// Determine the correct conversation title based on notification type
583+
Ejson bundleEjson = safeFromJson(bundle.getString("ejson", "{}"), Ejson.class);
584+
String conversationTitle = title;
585+
if (bundleEjson != null && bundleEjson.type != null) {
586+
if ("p".equals(bundleEjson.type) || "c".equals(bundleEjson.type)) {
587+
// For groups/channels, use room name if available, otherwise fall back to title
588+
conversationTitle = (bundleEjson.name != null && !bundleEjson.name.isEmpty()) ? bundleEjson.name : title;
589+
} else if ("d".equals(bundleEjson.type)) {
590+
// For direct messages, use title (sender name from server)
591+
conversationTitle = title;
592+
} else if ("l".equals(bundleEjson.type)) {
593+
// For omnichannel, use sender name if available, otherwise fall back to title
594+
conversationTitle = (bundleEjson.sender != null && bundleEjson.sender.name != null && !bundleEjson.sender.name.isEmpty())
595+
? bundleEjson.sender.name : title;
596+
}
597+
}
598+
messageStyle.setConversationTitle(conversationTitle);
566599

567600
if (bundles != null) {
568601
for (Bundle data : bundles) {

android/app/src/main/java/chat/rocket/reactnative/notification/Ejson.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public class Ejson {
4242
String notificationType;
4343
String messageType;
4444
String senderName;
45+
String name; // Room name for groups/channels
4546
String msg;
4647
Integer status; // For video conf: 0=incoming, 4=cancelled
4748

ios/NotificationService/NotificationService.swift

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,6 @@ class NotificationService: UNNotificationServiceExtension {
170170
if let messageId = data.messageId {
171171
self.rocketchat?.getPushWithId(messageId) { notification in
172172
if let notification = notification {
173-
self.bestAttemptContent?.title = notification.title
174173
self.bestAttemptContent?.body = notification.text
175174

176175
// Update ejson with full payload from server for correct navigation
@@ -226,6 +225,25 @@ class NotificationService: UNNotificationServiceExtension {
226225
}
227226

228227
func processPayload(payload: Payload) {
228+
// Set notification title based on payload type
229+
let senderName = payload.sender?.name ?? payload.senderName ?? "Unknown"
230+
if let roomType = payload.type {
231+
switch roomType {
232+
case .group, .channel:
233+
// For groups/channels, use room name if available, otherwise fall back to sender name
234+
bestAttemptContent?.title = payload.name ?? senderName
235+
case .direct:
236+
// For direct messages, use sender name
237+
bestAttemptContent?.title = senderName
238+
case .livechat:
239+
// For omnichannel, use sender name
240+
bestAttemptContent?.title = payload.sender?.name ?? senderName
241+
}
242+
} else {
243+
// Fallback to sender name if type is not available
244+
bestAttemptContent?.title = senderName
245+
}
246+
229247
// If is a encrypted message
230248
if payload.messageType == .e2e {
231249
if let rid = payload.rid {

0 commit comments

Comments
 (0)