From f870dcc168fa880e324f2bb65ec094bc82c1031b Mon Sep 17 00:00:00 2001 From: popaprozac Date: Sat, 22 Mar 2025 17:39:20 -0700 Subject: [PATCH] handle notif content errors --- .../notifications/notifications_darwin.m | 29 ++++++++++++------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/v3/pkg/services/notifications/notifications_darwin.m b/v3/pkg/services/notifications/notifications_darwin.m index 01d85c1bc..cd429c5d6 100644 --- a/v3/pkg/services/notifications/notifications_darwin.m +++ b/v3/pkg/services/notifications/notifications_darwin.m @@ -1,4 +1,5 @@ #import "notifications_darwin.h" +#include #import #import @@ -125,7 +126,7 @@ void checkNotificationAuthorization(int channelID) { // Helper function to create notification content UNMutableNotificationContent* createNotificationContent(const char *title, const char *subtitle, - const char *body, const char *data_json) { + const char *body, const char *data_json, NSError **contentError) { NSString *nsTitle = [NSString stringWithUTF8String:title]; NSString *nsSubtitle = subtitle ? [NSString stringWithUTF8String:subtitle] : @""; NSString *nsBody = [NSString stringWithUTF8String:body]; @@ -146,6 +147,8 @@ UNMutableNotificationContent* createNotificationContent(const char *title, const NSDictionary *parsedData = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error]; if (!error && parsedData) { content.userInfo = parsedData; + } else if (error) { + *contentError = error; } } @@ -162,13 +165,16 @@ void sendNotification(int channelID, const char *identifier, const char *title, UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; NSString *nsIdentifier = [NSString stringWithUTF8String:identifier]; - UNMutableNotificationContent *content = createNotificationContent(title, subtitle, body, data_json); - NSMutableDictionary *customData = [NSMutableDictionary dictionary]; + + NSError *contentError = nil; + UNMutableNotificationContent *content = createNotificationContent(title, subtitle, body, data_json, &contentError); - if (customData.count > 0) { - content.userInfo = customData; + if (contentError) { + NSString *errorMsg = [NSString stringWithFormat:@"Error: %@", [contentError localizedDescription]]; + captureResult(channelID, false, [errorMsg UTF8String]); + return; } - + UNTimeIntervalNotificationTrigger *trigger = nil; UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:nsIdentifier content:content trigger:trigger]; @@ -195,13 +201,14 @@ void sendNotificationWithActions(int channelID, const char *identifier, const ch NSString *nsIdentifier = [NSString stringWithUTF8String:identifier]; NSString *nsCategoryId = [NSString stringWithUTF8String:categoryId]; - UNMutableNotificationContent *content = createNotificationContent(title, subtitle, body, data_json); - NSMutableDictionary *customData = [NSMutableDictionary dictionary]; - content.categoryIdentifier = nsCategoryId; + NSError *contentError = nil; + UNMutableNotificationContent *content = createNotificationContent(title, subtitle, body, data_json, &contentError); - if (customData.count > 0) { - content.userInfo = customData; + if (contentError) { + NSString *errorMsg = [NSString stringWithFormat:@"Error: %@", [contentError localizedDescription]]; + captureResult(channelID, false, [errorMsg UTF8String]); + return; } UNTimeIntervalNotificationTrigger *trigger = nil;