Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat] Actionable notifications #482

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/ios/AppDelegate+FirebasePlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,11 @@ - (void) userNotificationCenter:(UNUserNotificationCenter *)center
[mutableUserInfo setValue:@"notification" forKey:@"messageType"];
}


// Dynamic Actions
if (response.actionIdentifier && ![response.actionIdentifier isEqual:UNNotificationDefaultActionIdentifier]) {
[mutableUserInfo setValue:response.actionIdentifier forKey:@"action"];
}

// Print full message.
[FirebasePlugin.firebasePlugin _logInfo:[NSString stringWithFormat:@"didReceiveNotificationResponse: %@", mutableUserInfo]];

Expand Down
36 changes: 36 additions & 0 deletions src/ios/FirebasePlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ - (void)pluginInitialize {
[self setPreferenceFlag:FIREBASE_PERFORMANCE_COLLECTION_ENABLED flag:YES];
}

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

// Check for permission and register for remote notifications if granted
[self _hasPermission:^(BOOL result) {}];

Expand All @@ -84,6 +87,39 @@ - (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"];

[buttons addObject:[UNNotificationAction actionWithIdentifier:actionId
title:NSLocalizedString(actionTitle, nil) options:UNNotificationActionOptionNone]];
}

[categories addObject:[UNNotificationCategory categoryWithIdentifier:category
actions:buttons intentIdentifiers:@[] options:UNNotificationCategoryOptionNone]];
}

// Initialize categories
[[UNUserNotificationCenter currentNotificationCenter] setNotificationCategories:categories];
}

// @override abstract
- (void)handleOpenURL:(NSNotification*)notification{
NSURL* url = [notification object];
Expand Down