2020 */
2121
2222#import " BackendlessPushHelper.h"
23+ #import " JSONHelper.h"
24+ #import " UserDefaultsHelper.h"
25+
26+ #define PUSH_TEMPLATES_USER_DEFAULTS @" iOSPushTemplates"
2327
2428@implementation BackendlessPushHelper
2529
30+ +(BackendlessPushHelper *_Nonnull)sharedInstance {
31+ static BackendlessPushHelper *sharedBackendlessPushHelper;
32+ if (!sharedBackendlessPushHelper) {
33+ @synchronized (self) {
34+ sharedBackendlessPushHelper = [BackendlessPushHelper new ];
35+ }
36+ }
37+ return sharedBackendlessPushHelper;
38+ }
39+
2640#if (TARGET_OS_IOS || TARGET_OS_SIMULATOR) && !TARGET_OS_TV && ! TARGET_OS_WATCH
27- +(void )processMutableContent : (UNNotificationRequest *_Nonnull)request withContentHandler : (void (^_Nonnull)(UNNotificationContent *_Nonnull))contentHandler NS_AVAILABLE_IOS(10_0) {
41+ -(void )processMutableContent : (UNNotificationRequest *_Nonnull)request withContentHandler : (void (^_Nonnull)(UNNotificationContent *_Nonnull))contentHandler NS_AVAILABLE_IOS(10_0) {
42+
43+ if ([request.content.userInfo valueForKey: @" ios_immediate_push" ]) {
44+ request = [self prepareRequestWithIosImmediatePush: request];
45+ }
46+
47+ if ([request.content.userInfo valueForKey: @" template_name" ]) {
48+ request = [self prepareRequestWithTemplate: request];
49+ }
50+
2851 UNMutableNotificationContent *bestAttemptContent = [request.content mutableCopy ];
2952 if ([request.content.userInfo valueForKey: @" attachment-url" ]) {
30- NSString *urlString = [request.content.userInfo valueForKey: @" attachment-url" ];
31- NSURL *fileUrl = [NSURL URLWithString: urlString];
53+ NSString *urlString = [request.content.userInfo valueForKey: @" attachment-url" ];
54+ NSURL *fileUrl = [NSURL URLWithString: urlString];
3255 [[[NSURLSession sharedSession ] downloadTaskWithURL: fileUrl
3356 completionHandler: ^(NSURL *location, NSURLResponse *response, NSError *error) {
3457 if (location) {
@@ -48,6 +71,99 @@ +(void)processMutableContent:(UNNotificationRequest *_Nonnull)request withConten
4871 contentHandler (bestAttemptContent);
4972 }
5073}
74+
75+ -(UNNotificationRequest *)prepareRequestWithIosImmediatePush : (UNNotificationRequest *)request {
76+ NSString *JSONString = [request.content.userInfo valueForKey: @" ios_immediate_push" ];
77+ NSDictionary *iosPushTemplate = [jsonHelper dictionaryFromJson: JSONString];
78+ return [self createRequestFromTemplate: [self dictionaryWithoutNulls: iosPushTemplate] request: request];
79+ }
80+
81+ -(UNNotificationRequest *)prepareRequestWithTemplate : (UNNotificationRequest *)request {
82+ NSString *templateName = [request.content.userInfo valueForKey: @" template_name" ];
83+ NSDictionary *iosPushTemplates = [userDefaultsHelper readFromUserDefaultsWithKey: PUSH_TEMPLATES_USER_DEFAULTS withSuiteName: @" group.com.backendless.PushTemplates" ];
84+ NSDictionary *iosPushTemplate = [iosPushTemplates valueForKey: templateName];
85+ return [self createRequestFromTemplate: [self dictionaryWithoutNulls: iosPushTemplate] request: request];
86+ }
87+
88+ -(NSDictionary *)dictionaryWithoutNulls : (NSDictionary *)dictionary {
89+ NSMutableDictionary *resultDictionary = [dictionary mutableCopy ];
90+ NSArray *keysForNullValues = [resultDictionary allKeysForObject: [NSNull null ]];
91+ [resultDictionary removeObjectsForKeys: keysForNullValues];
92+ return resultDictionary;
93+ }
94+
95+ -(UNNotificationRequest *)createRequestFromTemplate : (NSDictionary *)iosPushTemplate request : (UNNotificationRequest *)request {
96+ UNMutableNotificationContent *content = [UNMutableNotificationContent new ];
97+ NSMutableDictionary *userInfo = [NSMutableDictionary new ];
98+
99+ // check if silent
100+ NSNumber *contentAvailable = [iosPushTemplate valueForKey: @" contentAvailable" ];
101+ NSInteger contentAvailableInt = [contentAvailable integerValue ];
102+ if (contentAvailableInt == 1 ) {
103+
104+ }
105+ else {
106+ content.body = [[[request.content.userInfo valueForKey: @" aps" ] valueForKey: @" alert" ] valueForKey: @" body" ];
107+ if (request.content .title ) {
108+ content.title = request.content .title ;
109+ }
110+ else {
111+ content.title = [iosPushTemplate valueForKey: @" alertTitle" ];
112+ }
113+ if (request.content .subtitle ) {
114+ content.subtitle = request.content .subtitle ;
115+ }
116+ else {
117+ content.subtitle = [iosPushTemplate valueForKey: @" alertSubtitle" ];
118+ }
119+ if ([iosPushTemplate valueForKey: @" sound" ]) {
120+ content.sound = [UNNotificationSound soundNamed: [iosPushTemplate valueForKey: @" sound" ]];
121+ }
122+ else {
123+ content.sound = [UNNotificationSound defaultSound ];
124+ }
125+ if ([iosPushTemplate valueForKey: @" badge" ]) {
126+ NSNumber *badge = [iosPushTemplate valueForKey: @" badge" ];
127+ content.badge = badge;
128+ }
129+ else {
130+ content.badge = request.content .badge ;
131+ }
132+ if ([iosPushTemplate valueForKey: @" attachmentUrl" ]) {
133+ NSString *urlString = [iosPushTemplate valueForKey: @" attachmentUrl" ];
134+ [userInfo setObject: urlString forKey: @" attachment-url" ];
135+ content.userInfo = userInfo;
136+ }
137+ NSArray *actionsArray = [iosPushTemplate valueForKey: @" actions" ];
138+ content.categoryIdentifier = [self setActions: actionsArray];
139+ }
140+ if ([iosPushTemplate valueForKey: @" customHeaders" ]) {
141+ NSDictionary *customHeaders = [iosPushTemplate valueForKey: @" customHeaders" ];
142+ for (NSString *headerKey in [customHeaders allKeys ]) {
143+ [userInfo setObject: [customHeaders valueForKey: headerKey] forKey: headerKey];
144+ }
145+ content.userInfo = userInfo;
146+ }
147+ UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval: 0.1 repeats: NO ];
148+ return [UNNotificationRequest requestWithIdentifier: @" request" content: content trigger: trigger];
149+ }
150+
151+ -(NSString *)setActions : (NSArray *)actions {
152+ NSMutableArray *categoryActions = [NSMutableArray new ];
153+
154+ for (NSDictionary *action in actions) {
155+ NSString *actionId = [action valueForKey: @" id" ];
156+ NSString *actionTitle = [action valueForKey: @" title" ];
157+ NSNumber *actionOptions = [action valueForKey: @" options" ];
158+ UNNotificationActionOptions options = [actionOptions integerValue ];
159+ [categoryActions addObject: [UNNotificationAction actionWithIdentifier: actionId title: actionTitle options: options]];
160+ }
161+ NSString *categoryId = @" buttonActionsTemplate" ;
162+ UNNotificationCategory *category = [UNNotificationCategory categoryWithIdentifier: categoryId actions: categoryActions intentIdentifiers: @[] options: UNNotificationCategoryOptionNone ];
163+ [UNUserNotificationCenter .currentNotificationCenter setNotificationCategories: [NSSet setWithObject: category]];
164+ return categoryId;
165+ }
166+
51167#endif
52168
53169@end
0 commit comments