From 624a7b4e88f9c4c1d3cce4b688943c9b018fb2cb Mon Sep 17 00:00:00 2001 From: William Casarin Date: Sun, 6 Aug 2023 08:09:23 -0700 Subject: [PATCH] notifications: fix rare crash with local notification This shouldn't happen, but I found a log that crashed here, so we will fix this anyways. Changelog-Fixed: Fix rare crash triggered by local notifications --- damus/Util/LocalNotification.swift | 7 +++++-- damus/damusApp.swift | 4 +++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/damus/Util/LocalNotification.swift b/damus/Util/LocalNotification.swift index c10ea21f..51827734 100644 --- a/damus/Util/LocalNotification.swift +++ b/damus/Util/LocalNotification.swift @@ -18,8 +18,11 @@ struct LossyLocalNotification { ] } - static func from_user_info(user_info: [AnyHashable: Any]) -> LossyLocalNotification { - let target_id = MentionRef.from_bech32(str: user_info["id"] as! String)! + static func from_user_info(user_info: [AnyHashable: Any]) -> LossyLocalNotification? { + guard let id = user_info["id"] as? String, + let target_id = MentionRef.from_bech32(str: id) else { + return nil + } let typestr = user_info["type"] as! String let type = LocalNotificationType(rawValue: typestr)! diff --git a/damus/damusApp.swift b/damus/damusApp.swift index a634f88f..08c53521 100644 --- a/damus/damusApp.swift +++ b/damus/damusApp.swift @@ -58,7 +58,9 @@ class AppDelegate: NSObject, UIApplicationDelegate, UNUserNotificationCenterDele func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) { let userInfo = response.notification.request.content.userInfo - let notification = LossyLocalNotification.from_user_info(user_info: userInfo) + guard let notification = LossyLocalNotification.from_user_info(user_info: userInfo) else { + return + } notify(.local_notification(notification)) completionHandler() }