From 02a7f9414fcaa7d375cd5ecf028150a37583e3c2 Mon Sep 17 00:00:00 2001 From: Dave Alden Date: Tue, 22 Sep 2020 09:33:41 +0100 Subject: [PATCH] (iOS) Check if file contents for `pn-actions.json` exists before attempting to use it. Fixes #512. --- CHANGELOG.md | 4 +++ src/ios/FirebasePlugin.m | 72 +++++++++++++++++++++------------------- 2 files changed, 42 insertions(+), 34 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bb8810916..3a038950d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +* (iOS) Check if file contents for `pn-actions.json` exists before attempting to use it. + * Resolves [#512](https://github.com/dpa99c/cordova-plugin-firebasex/issues/512). + * Bug introduced by PR [#482](https://github.com/dpa99c/cordova-plugin-firebasex/pull/482). + # Version 11.0.1 * (iOS) Set the Sign In with Apple capability based on the `IOS_ENABLE_APPLE_SIGNIN` plugin variable. * Resolves [#511](https://github.com/dpa99c/cordova-plugin-firebasex/issues/511). diff --git a/src/ios/FirebasePlugin.m b/src/ios/FirebasePlugin.m index dfd9b3636..b957a33f4 100644 --- a/src/ios/FirebasePlugin.m +++ b/src/ios/FirebasePlugin.m @@ -92,44 +92,48 @@ - (void)pluginInitialize { // Dynamic actions from pn-actions.json - (void)setActionableNotifications { - - // Parse JSON - NSString *path = [[NSBundle mainBundle] pathForResource:@"pn-actions" ofType:@"json"]; - NSData *data = [NSData dataWithContentsOfFile:path]; - NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil]; - - // Assign actions for categories - NSMutableSet *categories = [[NSMutableSet alloc] init]; - NSArray *actionsArray = [dict objectForKey:@"PushNotificationActions"]; - for (NSDictionary *item in actionsArray) { - NSMutableArray *buttons = [NSMutableArray new]; - NSString *category = [item objectForKey:@"category"]; - - NSArray *actions = [item objectForKey:@"actions"]; - for (NSDictionary *action in actions) { - NSString *actionId = [action objectForKey:@"id"]; - NSString *actionTitle = [action objectForKey:@"title"]; - UNNotificationActionOptions options = UNNotificationActionOptionNone; - - id mode = [action objectForKey:@"foreground"]; - if (mode != nil && (([mode isKindOfClass:[NSString class]] && [mode isEqualToString:@"true"]) || [mode boolValue])) { - options |= UNNotificationActionOptionForeground; - } - id destructive = [action objectForKey:@"destructive"]; - if (destructive != nil && (([destructive isKindOfClass:[NSString class]] && [destructive isEqualToString:@"true"]) || [destructive boolValue])) { - options |= UNNotificationActionOptionDestructive; + @try { + // Parse JSON + NSString *path = [[NSBundle mainBundle] pathForResource:@"pn-actions" ofType:@"json"]; + NSData *data = [NSData dataWithContentsOfFile:path]; + if(data == nil) return; + NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil]; + + // Assign actions for categories + NSMutableSet *categories = [[NSMutableSet alloc] init]; + NSArray *actionsArray = [dict objectForKey:@"PushNotificationActions"]; + for (NSDictionary *item in actionsArray) { + NSMutableArray *buttons = [NSMutableArray new]; + NSString *category = [item objectForKey:@"category"]; + + NSArray *actions = [item objectForKey:@"actions"]; + for (NSDictionary *action in actions) { + NSString *actionId = [action objectForKey:@"id"]; + NSString *actionTitle = [action objectForKey:@"title"]; + UNNotificationActionOptions options = UNNotificationActionOptionNone; + + id mode = [action objectForKey:@"foreground"]; + if (mode != nil && (([mode isKindOfClass:[NSString class]] && [mode isEqualToString:@"true"]) || [mode boolValue])) { + options |= UNNotificationActionOptionForeground; + } + id destructive = [action objectForKey:@"destructive"]; + if (destructive != nil && (([destructive isKindOfClass:[NSString class]] && [destructive isEqualToString:@"true"]) || [destructive boolValue])) { + options |= UNNotificationActionOptionDestructive; + } + + [buttons addObject:[UNNotificationAction actionWithIdentifier:actionId + title:NSLocalizedString(actionTitle, nil) options:options]]; } - - [buttons addObject:[UNNotificationAction actionWithIdentifier:actionId - title:NSLocalizedString(actionTitle, nil) options:options]]; + + [categories addObject:[UNNotificationCategory categoryWithIdentifier:category + actions:buttons intentIdentifiers:@[] options:UNNotificationCategoryOptionNone]]; } - [categories addObject:[UNNotificationCategory categoryWithIdentifier:category - actions:buttons intentIdentifiers:@[] options:UNNotificationCategoryOptionNone]]; + // Initialize categories + [[UNUserNotificationCenter currentNotificationCenter] setNotificationCategories:categories]; + }@catch (NSException *exception) { + [self handlePluginExceptionWithoutContext:exception]; } - - // Initialize categories - [[UNUserNotificationCenter currentNotificationCenter] setNotificationCategories:categories]; } // @override abstract