Skip to content

Commit

Permalink
(iOS) Add support for foreground and destructive UNNotificationAction…
Browse files Browse the repository at this point in the history
…Options. Cherrypicked from and resolves PR #487.
  • Loading branch information
roman-rr authored and dpa99c committed Sep 15, 2020
1 parent 03d0561 commit 2d7fd09
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1165,20 +1165,22 @@ To use them in your app you must do the following:
"category": "news",
"actions": [
{
"id": "read", "title": "Read"
"id": "read", "title": "Read", "foreground": true
},
{
"id": "skip", "title": "Skip"
},
{
"id": "add", "title": "Add to list"
"id": "delete", "title": "Delete", "destructive": true
}
]
}
]
}
```

Note the `foreground` and `destructive` options correspond to the equivalent [UNNotificationActionOptions](https://developer.apple.com/documentation/usernotifications/unnotificationactionoptions?language=objc).

2. Reference it as a resource file in your `config.xml`:

```xml
Expand Down
20 changes: 15 additions & 5 deletions src/ios/FirebasePlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ - (void)pluginInitialize {
if([self getGooglePlistFlagWithDefaultValue:FIREBASE_PERFORMANCE_COLLECTION_ENABLED defaultValue:YES]){
[self setPreferenceFlag:FIREBASE_PERFORMANCE_COLLECTION_ENABLED flag:YES];
}

// Set actionable categories if pn-actions.json exist in bundle
[self setActionableNotifications];

Expand All @@ -92,7 +92,7 @@ - (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];
Expand All @@ -109,9 +109,19 @@ - (void)setActionableNotifications {
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:UNNotificationActionOptionNone]];
title:NSLocalizedString(actionTitle, nil) options:options]];
}

[categories addObject:[UNNotificationCategory categoryWithIdentifier:category
Expand Down Expand Up @@ -287,7 +297,7 @@ - (void)grantPermission:(CDVInvokedUrlCommand *)command {
[self registerForRemoteNotifications];
}
[self handleBoolResultWithPotentialError:error command:command result:granted];

}@catch (NSException *exception) {
[self handlePluginExceptionWithContext:exception :command];
}
Expand Down

0 comments on commit 2d7fd09

Please sign in to comment.