Skip to content

Commit 98b2671

Browse files
author
Olga Danylova
authored
Merge pull request #210 from olgadanylova/Templates
Templates
2 parents bead8bf + d7e9699 commit 98b2671

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+784
-249
lines changed

Pods/Backendless.podspec

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

SDK/Podfile.lock

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
PODS:
2+
- Socket.IO-Client-Swift (13.3.1):
3+
- Starscream (~> 3.0.2)
4+
- Starscream (3.0.6)
5+
6+
DEPENDENCIES:
7+
- Socket.IO-Client-Swift
8+
9+
SPEC REPOS:
10+
https://github.com/cocoapods/specs.git:
11+
- Socket.IO-Client-Swift
12+
- Starscream
13+
14+
SPEC CHECKSUMS:
15+
Socket.IO-Client-Swift: 79f8f85fa44881838175a20f9bd2c32bef2c9d37
16+
Starscream: ef3ece99d765eeccb67de105bfa143f929026cf5
17+
18+
PODFILE CHECKSUM: 675cc991d5f2f8cb6bcd0ce2d67444ed1b8f2a74
19+
20+
COCOAPODS: 1.6.0.beta.1

SDK/backendlessAPI/Classes/Backendless.h

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,15 +80,19 @@
8080
#import "BackendlessCachePolicy.h"
8181
#import "AbstractQuery.h"
8282

83+
84+
/*******************************************************************************************************************
85+
* Backendless singleton accessor: this is how you should ALWAYS get a reference to the Backendless class instance *
86+
*******************************************************************************************************************/
87+
#define backendless [Backendless sharedInstance]
88+
89+
// BackendlessAppConf.plist keys
8390
#define BACKENDLESS_APP_CONF @"BackendlessAppConf"
8491
#define BACKENDLESS_APP_ID @"AppId"
8592
#define BACKENDLESS_API_KEY @"APIKey"
8693
#define BACKENDLESS_DEBLOG_ON @"DebLogOn"
8794

88-
#define backendless [Backendless sharedInstance]
89-
9095
@interface Backendless : NSObject
91-
9296
// context
9397
@property (strong, nonatomic, getter = getHostUrl, setter = setHostUrl:) NSString *hostURL;
9498
@property (strong, nonatomic, getter = getAppId, setter = setAppId:) NSString *appID;
@@ -116,24 +120,34 @@
116120
@property (assign, nonatomic, readonly) MessagingService *messaging;
117121
@property (assign, nonatomic, readonly) FileService *file;
118122

123+
// Singleton accessor: this is how you should ALWAYS get a reference to the class instance. Never init your own.
119124
+(Backendless *)sharedInstance;
120-
125+
/**
126+
* Initializes the Backendless class and all Backendless dependencies.
127+
* This is the first step in using the client API.
128+
*
129+
@param applicationId a Backendless application ID, which could be retrieved at the Backendless console
130+
@param apiKey a Backendless application API key, which could be retrieved at the Backendless console
131+
*/
121132
-(void)initApp:(NSString *)applicationId APIKey:(NSString *)apiKey;
122133
-(void)initApp:(NSString *)plist;
123134
-(void)initApp;
124135
-(void)initAppFault;
136+
#pragma mark - exceptions management
125137
-(void)setThrowException:(BOOL)needThrow;
126138
-(id)throwFault:(Fault *)fault;
139+
#pragma mark - utils
127140
-(NSString *)GUIDString;
128141
-(NSString *)randomString:(int)numCharacters;
129142
-(NSString *)applicationType;
143+
#pragma mark - cache methods
130144
-(void)clearAllCache;
131145
-(void)clearCacheForClassName:(NSString *)className query:(id) query;
132146
-(BOOL)hasResultForClassName:(NSString *)className query:(id) query;
133147
-(void)setCachePolicy:(BackendlessCachePolicy *)policy;
134148
-(void)setCacheStoredType:(BackendlessCacheStoredEnum)storedType;
135149
-(void)saveCache;
150+
#pragma mark - hardware
136151
-(BOOL)is64bitSimulator;
137152
-(BOOL)is64bitHardware;
138-
139153
@end

SDK/backendlessAPI/Classes/Files/BackendlessFile.h

100755100644
File mode changed.

SDK/backendlessAPI/Classes/Messaging/BackendlessPushHelper.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,14 @@
2424
#import <UserNotifications/UserNotifications.h>
2525
#endif
2626

27+
#define backendlessPushHelper [BackendlessPushHelper sharedInstance]
28+
2729
@interface BackendlessPushHelper : NSObject
2830

31+
+(BackendlessPushHelper *_Nonnull)sharedInstance;
32+
2933
#if (TARGET_OS_IOS || TARGET_OS_SIMULATOR) && !TARGET_OS_TV && ! TARGET_OS_WATCH
30-
+(void)processMutableContent:(UNNotificationRequest *_Nonnull)request withContentHandler:(void(^_Nonnull)(UNNotificationContent *_Nonnull))contentHandler NS_AVAILABLE_IOS(10_0);
34+
-(void)processMutableContent:(UNNotificationRequest *_Nonnull)request withContentHandler:(void(^_Nonnull)(UNNotificationContent *_Nonnull))contentHandler NS_AVAILABLE_IOS(10_0);
3135
#endif
3236

3337
@end

SDK/backendlessAPI/Classes/Messaging/BackendlessPushHelper.m

Lines changed: 119 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,38 @@
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

SDK/backendlessAPI/Classes/Messaging/MessagingService.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@
5555
-(MessageStatus *)sendHTMLEmail:(NSString *)subject body:(NSString *)messageBody to:(NSArray<NSString*> *)recipients;
5656
-(MessageStatus *)sendEmail:(NSString *)subject body:(BodyParts *)bodyParts to:(NSArray<NSString*> *)recipients;
5757
-(MessageStatus *)sendEmail:(NSString *)subject body:(BodyParts *)bodyParts to:(NSArray<NSString*> *)recipients attachment:(NSArray *)attachments;
58-
-(MessageStatus *)getMessageStatus:(NSString*)messageId;
58+
-(MessageStatus *)getMessageStatus:(NSString *)messageId;
59+
-(MessageStatus *)pushWithTemplate:(NSString *)templateName;
5960

6061
// async methods with block-based callbacks
6162
-(void)registerDevice:(NSData *)deviceToken response:(void(^)(NSString *))responseBlock error:(void(^)(Fault *))errorBlock;
@@ -74,7 +75,8 @@
7475
-(void)sendHTMLEmail:(NSString *)subject body:(NSString *)messageBody to:(NSArray<NSString*> *)recipients response:(void(^)(MessageStatus *))responseBlock error:(void(^)(Fault *))errorBlock;
7576
-(void)sendEmail:(NSString *)subject body:(BodyParts *)bodyParts to:(NSArray<NSString*> *)recipients response:(void(^)(MessageStatus *))responseBlock error:(void(^)(Fault *))errorBlock;
7677
-(void)sendEmail:(NSString *)subject body:(BodyParts *)bodyParts to:(NSArray<NSString*> *)recipients attachment:(NSArray *)attachments response:(void(^)(MessageStatus *))responseBlock error:(void(^)(Fault *))errorBlock;
77-
-(void)getMessageStatus:(NSString*)messageId response:(void(^)(MessageStatus*))responseBlock error:(void(^)(Fault *))errorBlock;
78+
-(void)getMessageStatus:(NSString *)messageId response:(void(^)(MessageStatus *))responseBlock error:(void(^)(Fault *))errorBlock;
79+
-(void)pushWithTemplate:(NSString *)templateName response:(void(^)(MessageStatus *))responseBlock error:(void(^)(Fault *))errorBlock;
7880

7981
// utilites
8082
-(DeviceRegistration *)currentDevice;

0 commit comments

Comments
 (0)