Skip to content

Commit 07a46b3

Browse files
committed
feat: improve notification handling and error management
- Enhanced message parsing in CustomPushNotification to handle cases where the message format may not include a colon. - Updated Ejson to ensure proper URL encoding for avatar paths, improving reliability in generating user and room avatars. - Added error handling in onNotification to catch parsing errors for video conference notifications, logging warnings for failed attempts. - Improved room name assignment logic to handle cases where sender information may be missing. - Updated iOS ReplyNotification to provide user feedback for invalid notification data and failed replies.
1 parent 796e175 commit 07a46b3

File tree

4 files changed

+42
-15
lines changed

4 files changed

+42
-15
lines changed

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -517,8 +517,11 @@ private String extractMessage(String message, Ejson ejson) {
517517
}
518518
if (ejson != null && ejson.type != null && !ejson.type.equals("d")) {
519519
int pos = message.indexOf(":");
520-
int start = pos == -1 ? 0 : pos + 2;
521-
return message.substring(start);
520+
if (pos == -1) {
521+
return message;
522+
}
523+
int start = pos + 2;
524+
return start <= message.length() ? message.substring(start) : "";
522525
}
523526
return message;
524527
}

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

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import com.tencent.mmkv.MMKV;
77

88
import java.math.BigInteger;
9+
import java.net.URLEncoder;
10+
import java.nio.charset.StandardCharsets;
911

1012
import chat.rocket.reactnative.BuildConfig;
1113
import chat.rocket.reactnative.storage.MMKVKeyManager;
@@ -81,19 +83,29 @@ public String getAvatarUri() {
8183
Log.w(TAG, "Cannot generate avatar URI: sender or username is null");
8284
return null;
8385
}
84-
avatarPath = "/avatar/" + sender.username;
85-
if (BuildConfig.DEBUG) {
86-
Log.d(TAG, "Generated avatar URI for user: " + sender.username);
86+
try {
87+
avatarPath = "/avatar/" + URLEncoder.encode(sender.username, StandardCharsets.UTF_8);
88+
if (BuildConfig.DEBUG) {
89+
Log.d(TAG, "Generated avatar URI for user: " + sender.username);
90+
}
91+
} catch (Exception e) {
92+
Log.e(TAG, "Failed to encode username", e);
93+
return null;
8794
}
8895
} else {
8996
// Group/Channel/Livechat: use room avatar
9097
if (rid == null || rid.isEmpty()) {
9198
Log.w(TAG, "Cannot generate avatar URI: rid is null for non-DM");
9299
return null;
93100
}
94-
avatarPath = "/avatar/room/" + rid;
95-
if (BuildConfig.DEBUG) {
96-
Log.d(TAG, "Generated avatar URI for room: " + rid);
101+
try {
102+
avatarPath = "/avatar/room/" + URLEncoder.encode(rid, StandardCharsets.UTF_8);
103+
if (BuildConfig.DEBUG) {
104+
Log.d(TAG, "Generated avatar URI for room: " + rid);
105+
}
106+
} catch (Exception e) {
107+
Log.e(TAG, "Failed to encode rid", e);
108+
return null;
97109
}
98110
}
99111

app/lib/notifications/index.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,13 @@ export const onNotification = (push: INotification): void => {
2121
// Handle video conf notification actions (Accept/Decline buttons)
2222
if (identifier === 'ACCEPT_ACTION' || identifier === 'DECLINE_ACTION') {
2323
if (push?.payload?.ejson) {
24-
const notification = EJSON.parse(push.payload.ejson);
25-
store.dispatch(deepLinkingClickCallPush({ ...notification, event: identifier === 'ACCEPT_ACTION' ? 'accept' : 'decline' }));
26-
return;
24+
try {
25+
const notification = EJSON.parse(push.payload.ejson);
26+
store.dispatch(deepLinkingClickCallPush({ ...notification, event: identifier === 'ACCEPT_ACTION' ? 'accept' : 'decline' }));
27+
return;
28+
} catch (e) {
29+
console.warn('Failed to parse video conf notification:', e);
30+
}
2731
}
2832
}
2933

@@ -49,9 +53,11 @@ export const onNotification = (push: INotification): void => {
4953
p: 'group',
5054
l: 'channels'
5155
};
52-
let roomName = type === SubscriptionType.DIRECT ? sender.username : name;
53-
if (type === SubscriptionType.OMNICHANNEL) {
54-
roomName = sender.name;
56+
let roomName = name;
57+
if (type === SubscriptionType.DIRECT) {
58+
roomName = sender?.username ?? name;
59+
} else if (type === SubscriptionType.OMNICHANNEL) {
60+
roomName = sender?.name ?? name;
5561
}
5662

5763
const params = {

ios/ReplyNotification.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@ class ReplyNotification: NSObject, UNUserNotificationCenterDelegate {
8888
let ejsonData = ejsonString.data(using: .utf8),
8989
let payload = try? JSONDecoder().decode(Payload.self, from: ejsonData),
9090
let rid = payload.rid else {
91+
// Show failure notification to user
92+
let content = UNMutableNotificationContent()
93+
content.body = "Failed to send reply. Invalid notification data."
94+
let request = UNNotificationRequest(identifier: "replyPayloadFailure", content: content, trigger: nil)
95+
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
9196
completionHandler()
9297
return
9398
}
@@ -102,6 +107,7 @@ class ReplyNotification: NSObject, UNUserNotificationCenterDelegate {
102107
UIApplication.shared.endBackgroundTask(backgroundTask)
103108
backgroundTask = .invalid
104109
}
110+
completionHandler()
105111
}
106112

107113
rocketchat.sendMessage(rid: rid, message: message, threadIdentifier: payload.tmid) { response in
@@ -118,7 +124,7 @@ class ReplyNotification: NSObject, UNUserNotificationCenterDelegate {
118124
guard let response = response, response.success else {
119125
// Show failure notification
120126
let content = UNMutableNotificationContent()
121-
content.body = "Failed to reply message."
127+
content.body = "Failed to send reply."
122128
let request = UNNotificationRequest(identifier: "replyFailure", content: content, trigger: nil)
123129
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
124130
return

0 commit comments

Comments
 (0)