Skip to content

Commit

Permalink
(iOS) Check if file contents for pn-actions.json exists before atte…
Browse files Browse the repository at this point in the history
…mpting to use it. Fixes #512.
  • Loading branch information
Dave Alden committed Sep 22, 2020
1 parent a4e09c9 commit 02a7f94
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 34 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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).
Expand Down
72 changes: 38 additions & 34 deletions src/ios/FirebasePlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 02a7f94

Please sign in to comment.