Skip to content

Commit c0a2ffd

Browse files
committed
Enhance NotificationService for improved avatar handling
- Updated type casting for body content to ensure proper string handling. - Simplified variable declarations for senderImage, recipients, and speakableGroupName. - Configured URLSession with specific timeouts for better resource management during avatar fetching.
1 parent 32ea524 commit c0a2ffd

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

ios/NotificationService/NotificationService.swift

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ class NotificationService: UNNotificationServiceExtension {
8686
if roomType == .group || roomType == .channel {
8787
bestAttemptContent.title = payload.name ?? senderName
8888
// Strip sender prefix if present
89-
if let body = bestAttemptContent.body as String? {
89+
if let body = bestAttemptContent.body as? String {
9090
let prefix = "\(senderUsername): "
9191
if body.hasPrefix(prefix) {
9292
bestAttemptContent.body = String(body.dropFirst(prefix.count))
@@ -136,7 +136,7 @@ class NotificationService: UNNotificationServiceExtension {
136136
guard let bestAttemptContent = bestAttemptContent else { return }
137137

138138
// 1. Create Sender
139-
var senderImage: INImage? = nil
139+
var senderImage: INImage?
140140
if let data = avatarData {
141141
senderImage = INImage(imageData: data)
142142
}
@@ -151,8 +151,8 @@ class NotificationService: UNNotificationServiceExtension {
151151
)
152152

153153
// 2. Handle Group Logic
154-
var recipients: [INPerson]? = nil
155-
var speakableGroupName: INSpeakableString? = nil
154+
var recipients: [INPerson]?
155+
var speakableGroupName: INSpeakableString?
156156

157157
if isGroup {
158158
speakableGroupName = (groupName != nil) ? INSpeakableString(spokenPhrase: groupName!) : nil
@@ -259,11 +259,18 @@ class NotificationService: UNNotificationServiceExtension {
259259
return
260260
}
261261

262-
var request = URLRequest(url: avatarURL, timeoutInterval: 3)
262+
// Create URLSessionConfiguration with proper timeouts for notification service extension
263+
// timeoutIntervalForResource ensures total download time is limited (not just inactivity)
264+
let config = URLSessionConfiguration.default
265+
config.timeoutIntervalForRequest = 3 // Inactivity timeout
266+
config.timeoutIntervalForResource = 3 // Total download timeout (critical for notification extensions)
267+
let session = URLSession(configuration: config)
268+
269+
var request = URLRequest(url: avatarURL)
263270
request.httpMethod = "GET"
264271
request.addValue(Bundle.userAgent, forHTTPHeaderField: "User-Agent")
265272

266-
let task = URLSession.shared.dataTask(with: request) { data, response, error in
273+
let task = session.dataTask(with: request) { data, response, error in
267274
guard error == nil,
268275
let httpResponse = response as? HTTPURLResponse,
269276
httpResponse.statusCode == 200,

0 commit comments

Comments
 (0)