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

[Feature] Actionable notifications: foreground, background, destructive #487

Closed
wants to merge 4 commits 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
2 changes: 2 additions & 0 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@
<source-file src="src/ios/FirebasePluginMessageReceiver.m" />
<header-file src="src/ios/FirebasePluginMessageReceiverManager.h" />
<source-file src="src/ios/FirebasePluginMessageReceiverManager.m" />
<header-file src="src/ios/PharmacyActionableNotifications.h" />
<source-file src="src/ios/PharmacyActionableNotifications.m" />

<framework src="AuthenticationServices.framework" />

Expand Down
4 changes: 4 additions & 0 deletions src/ios/AppDelegate+FirebasePlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#import "FirebasePlugin.h"
#import "Firebase.h"
#import <objc/runtime.h>
#import "PharmacyActionableNotifications.h"


@import UserNotifications;
Expand Down Expand Up @@ -474,6 +475,9 @@ - (void) userNotificationCenter:(UNUserNotificationCenter *)center
// Dynamic Actions
if (response.actionIdentifier && ![response.actionIdentifier isEqual:UNNotificationDefaultActionIdentifier]) {
[mutableUserInfo setValue:response.actionIdentifier forKey:@"action"];

// Send post request to update reminder event
[[[PharmacyActionableNotifications alloc] init] updateHistoryAction:mutableUserInfo AppDelegate:self];
}

// Print full message.
Expand Down
15 changes: 12 additions & 3 deletions src/ios/FirebasePlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ - (void)pluginInitialize {
}
}


// Dynamic actions from pn-actions.json
- (void)setActionableNotifications {

Expand All @@ -107,9 +106,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
10 changes: 10 additions & 0 deletions src/ios/PharmacyActionableNotifications.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//
// PharmacyActionableNotifications.h
// Atlantic Pharmacy
//
// Created by Roman Antonov on 13/9/20.
//
#import "AppDelegate.h"
@interface PharmacyActionableNotifications: NSObject
- (void) updateHistoryAction:(NSDictionary *) userInfo AppDelegate:(AppDelegate*) appDelegate;
@end
80 changes: 80 additions & 0 deletions src/ios/PharmacyActionableNotifications.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
//
// PharmacyActionableNotifications.m
// Atlantic Pharmacy
//
// Created by Roman Antonov on 13/9/20.
//

#import "PharmacyActionableNotifications.h"
#import <WebKit/WKWebView.h>

@implementation PharmacyActionableNotifications

- (void)updateHistoryAction:(NSDictionary *)userInfo
AppDelegate:(AppDelegate *) appDelegate {
if ([[userInfo objectForKey:@"action"] isEqualToString:@"snooze"]
|| [[userInfo objectForKey:@"action"] isEqualToString:@"take"]
|| [[userInfo objectForKey:@"action"] isEqualToString:@"skip"]) {

// Get data from localStorage of wkwebview
[(WKWebView*) appDelegate.viewController.webView
evaluateJavaScript:@"JSON.stringify({device_token_key: localStorage.device_token_key, \
device_token: localStorage.device_token, \
API_URL: localStorage.API_URL, \
API_KEY: localStorage.API_KEY})"
completionHandler:^(NSString* result, NSError *error) {

if (error == nil) {
if (result != nil) {
NSError *jsonError;
NSData *objectData = [result dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:objectData
options:NSJSONReadingMutableContainers
error:&jsonError];

NSString *url = [json objectForKey:@"API_URL"];
url = [url stringByAppendingString:@"api/update-history-action/"];
url = [url stringByAppendingString:[json objectForKey:@"API_KEY"]];

NSString *params = [NSString stringWithFormat:@"device_token_key=%@&device_token=%@&action=%@&recurring_id=%@&history_id=%@&device_type=%@",
[json objectForKey:@"device_token_key"],
[json objectForKey:@"device_token"],
[userInfo objectForKey:@"action"],
[userInfo objectForKey:@"recurring_id"],
[userInfo objectForKey:@"history_id"],
@"apns"];
NSLog(@"POST PARAMS -> %@", params);
NSLog(@"POST RESULT -> %@", [self sendPOST:url withParams:params]);
}
}

}];
}
}

- (NSString *) sendPOST:(NSString *)endpoint withParams:(NSString *)params {
NSData *postData = [params dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%lu",[postData length]];


NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setHTTPMethod:@"POST"];
[request setURL:[NSURL URLWithString:endpoint]];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];

NSError *error = nil;
NSHTTPURLResponse *responseCode = nil;

NSData *oResponseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&responseCode error:&error];

if([responseCode statusCode] != 200){
NSLog(@"Error getting %@, HTTP status code %li", endpoint, (long)[responseCode statusCode]);
return nil;
}

return [[NSString alloc] initWithData:oResponseData encoding:NSUTF8StringEncoding];
}

@end