- First Commit

This commit is contained in:
giuseppenuc committed 2015-12-17 16:55:03 +01:00
1 parent b34b29256e
commit 3e3fec3773
275 files changed
+11345 -25

No files matched your search

+25
View File
@@ -0,0 +1,25 @@
//
// PushNotification.h
// NotificationManager
//
// Created by Giuseppe Nucifora on 07/10/15.
// Copyright (c) 2015 Giuseppe Nucifora. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "PushNotificationAlert.h"
#import "PushNotificationBody.h"
@interface PushNotification : NSObject
- (instancetype) initWithNotificationInfo:(NSDictionary *) notificationInfo;
@property (nonatomic, strong, readonly) PushNotificationAlert *alert;
@property (nonatomic, strong, readonly) NSNumber *badge;
@property (nonatomic, strong, readonly) NSString *sound;
@property (nonatomic, strong, readonly) NSString *category;
@property (nonatomic, strong, readonly) PushNotificationBody *body;
@end
+92
View File
@@ -0,0 +1,92 @@
//
// PushNotification.m
// NotificationManager
//
// Created by Giuseppe Nucifora on 07/10/15.
// Copyright (c) 2015 Giuseppe Nucifora. All rights reserved.
//
#import "PushNotification.h"
#import "PushNotificationAlert.h"
#import "PushNotificationBody.h"
#define NOTIFICATION_APS @"aps"
#define NOTIFICATION_APS_ALERT @"alert"
#define NOTIFICATION_APS_BADGE @"badge"
#define NOTIFICATION_APS_SOUND @"sound"
#define NOTIFICATION_APS_CATEGORY @"category"
#define NOTIFICATION_BODY @"body"
/*{
"aps" : {
"alert" : {
"title" : "Game Request",
"body" : "Bob wants to play poker",
"action-loc-key" : "PLAY"
"loc-args" = ("peppino.peaaa");
"loc-key" = NLIKE;
},
"badge" : 9,
"sound" : "bingbong.aiff",
"category" : "NEW_MESSAGE_CATEGORY"
},
"body" : {
"objectId" : "",
"action" : ""
}
}*/
@implementation PushNotification
- (instancetype) initWithNotificationInfo:(NSDictionary *)notificationInfo {
self = [super init];
if (self) {
if ([notificationInfo isKindOfClass:[NSDictionary class]]) {
for (NSString *key in [notificationInfo allKeys]) {
((void (^)())@{
NOTIFICATION_APS : ^{
for (NSString *apsKey in [[notificationInfo objectForKey:key] allKeys]) {
((void (^)())@{
NOTIFICATION_APS_ALERT : ^{
_alert = [PushNotificationAlert AlertWithInfo:[[notificationInfo objectForKey:key] objectForKey:apsKey]];
},
NOTIFICATION_APS_BADGE : ^{
_badge = [NSNumber numberWithInteger:[[[notificationInfo objectForKey:key] objectForKey:apsKey] integerValue]];
},
NOTIFICATION_APS_SOUND : ^{
_sound = [[notificationInfo objectForKey:key] objectForKey:apsKey];
},
NOTIFICATION_APS_CATEGORY : ^{
_category = [[notificationInfo objectForKey:key] objectForKey:apsKey];
}
}[apsKey] ?: ^{
NSLog(@"Notification aps default");
})();
}
},
NOTIFICATION_BODY : ^{
_body = [PushNotificationBody BodyWithInfo:[notificationInfo objectForKey:key]];
},
}[key] ?: ^{
NSLog(@"Notification default");
})();
}
}
}
return self;
}
@end
+24
View File
@@ -0,0 +1,24 @@
//
// PushNotificationAlert.h
// Pods
//
// Created by Giuseppe Nucifora on 17/12/15.
//
//
#import <Foundation/Foundation.h>
@interface PushNotificationAlert : NSObject
+ (instancetype) AlertWithInfo:(NSDictionary *)alertInfo;
- (instancetype) initWithAlertInfo:(NSDictionary *)alertInfo;
@property (nonatomic, strong, readonly) NSString *title;
@property (nonatomic, strong, readonly) NSString *body;
@property (nonatomic, strong, readonly) NSString *actionLocalizedKey;
@property (nonatomic, strong, readonly) NSString *localizedKey;
@property (nonatomic, strong, readonly) NSArray *localizedArgs;
@end
+70
View File
@@ -0,0 +1,70 @@
//
// PushNotificationAlert.m
// Pods
//
// Created by Giuseppe Nucifora on 17/12/15.
//
//
#import "PushNotificationAlert.h"
#define NOTIFICATION_ALERT_TITLE @"title"
#define NOTIFICATION_ALERT_BODY @"body"
#define NOTIFICATION_ALERT_LOC_KEY @"loc-key"
#define NOTIFICATION_ALERT_LOC_ARGS @"loc-args"
#define NOTIFICATION_ALERT_ACTION_LOC_KEY @"action-loc-key"
@implementation PushNotificationAlert
/*
"alert" : {
"title" : "Game Request",
"body" : "Bob wants to play poker",
"action-loc-key" : "PLAY"
"loc-args" = ("peppino.peaaa");
"loc-key" = NLIKE;
},
*/
+ (instancetype) AlertWithInfo:(NSDictionary *)alertInfo {
return [[PushNotificationAlert alloc] initWithAlertInfo:alertInfo];
}
- (instancetype) initWithAlertInfo:(NSDictionary *)alertInfo {
self = [super init];
if (self) {
if (alertInfo && [alertInfo isKindOfClass:[NSDictionary class]]) {
for (NSString *key in [alertInfo allKeys]) {
((void (^)())@{
NOTIFICATION_ALERT_TITLE : ^{
_title = [alertInfo objectForKey:key];
},
NOTIFICATION_ALERT_BODY : ^{
_body = [alertInfo objectForKey:key];
},
NOTIFICATION_ALERT_LOC_KEY : ^{
_localizedKey = [alertInfo objectForKey:key];
},
NOTIFICATION_ALERT_LOC_ARGS : ^{
if ([[alertInfo objectForKey:key] isKindOfClass:[NSArray class]]) {
_localizedArgs = [[NSArray alloc] initWithArray:[alertInfo objectForKey:key]];
}
},
NOTIFICATION_ALERT_ACTION_LOC_KEY : ^{
_actionLocalizedKey = [alertInfo objectForKey:key];
}
}[key] ?: ^{
NSLog(@"Notification alert default");
})();
}
}
}
return self;
}
@end
+30
View File
@@ -0,0 +1,30 @@
//
// PushNotificationBody.h
// Pods
//
// Created by Giuseppe Nucifora on 17/12/15.
//
//
#import <Foundation/Foundation.h>
typedef NS_ENUM(NSInteger, PushNotificationAction) {
PushNotificationActionNewMessage = 0,
PushNotificationActionStar,
PushNotificationActionLike,
PushNotificationActionFollowUser,
PushNotificationActionNewReview,
PushNotificationActionBuy
};
@interface PushNotificationBody : NSObject
+ (instancetype) BodyWithInfo:(NSDictionary *)bodyInfo;
- (instancetype) initWithBodyInfo:(NSDictionary *)bodyInfo;
@property (nonatomic, strong, readonly) NSString *objectId;
@property (nonatomic, strong, readonly) NSString *actionString;
@property (nonatomic, readonly) PushNotificationAction action;
@end
+66
View File
@@ -0,0 +1,66 @@
//
// PushNotificationBody.m
// Pods
//
// Created by Giuseppe Nucifora on 17/12/15.
//
//
#import "PushNotificationBody.h"
#define BODY_OBJECTID @"objectId"
#define BODY_ACTION @"action"
#define ACTION_NEW_MESSAGE @"NEW_MESSAGE"
#define ACTION_STAR_EVENT @"STAR"
#define ACTION_LIKE_EVENT @"LIKE"
#define ACTION_FOLLOW_USER @"FOLLOW_USER"
#define ACTION_NEW_REVIEW @"NEW_REVIEW"
#define ACTION_BUY_EVENT @"BUY"
static const NSArray *actionsArray;
#define PushNotificationActionFromString(string) (PushNotificationAction)[actionsArray indexOfObject:string]
@implementation PushNotificationBody
+ (void)initialize
{
actionsArray = @[ACTION_NEW_MESSAGE,ACTION_STAR_EVENT,ACTION_LIKE_EVENT,ACTION_FOLLOW_USER,ACTION_NEW_REVIEW,ACTION_BUY_EVENT];
}
+ (instancetype) BodyWithInfo:(NSDictionary *)bodyInfo {
return [[PushNotificationBody alloc] initWithBodyInfo:bodyInfo];
}
- (instancetype) initWithBodyInfo:(NSDictionary *)bodyInfo {
self = [super init];
if (self) {
if (bodyInfo && [bodyInfo isKindOfClass:[NSDictionary class]]) {
for (NSString *key in [bodyInfo allKeys]) {
((void (^)())@{
BODY_OBJECTID : ^{
_objectId = [bodyInfo objectForKey:key];
},
BODY_ACTION : ^{
_actionString = [bodyInfo objectForKey:key];
if ([actionsArray containsObject:[bodyInfo objectForKey:key]]) {
_action = PushNotificationActionFromString([bodyInfo objectForKey:key]);
}
}
}[key] ?: ^{
NSLog(@"Notification body default");
})();
}
}
}
return self;
}
@end
+33
View File
@@ -0,0 +1,33 @@
//
// PushNotificationManager.h
// NotificationManager
//
// Created by Giuseppe Nucifora on 07/10/15.
// Copyright (c) 2015 Giuseppe Nucifora. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "PushNotification.h"
#import <MPGNotification/MPGNotification.h>
typedef void (^PushNotificationBlock)(PushNotification *pushNotification);
@interface PushNotificationManager : NSObject
//@property (nonatomic, copy) PushNotificationBlock pushNotificationBlock;
/**
* gets singleton object.
* @return singleton
*/
+ (PushNotificationManager*)sharedInstance;
- (void) addNotificationHandler:(PushNotificationBlock) block;
- (void) sendPushNotificationToHandlers:(NSDictionary*) userInfo;
- (void) getPushNotificationFromUserInfo: (NSDictionary*) userInfo
withBlockSuccess: (PushNotificationBlock) success
failure: (void (^)(BOOL status)) failure;
@end
+130
View File
@@ -0,0 +1,130 @@
//
// PushNotificationManager.m
// NotificationManager
//
// Created by Giuseppe Nucifora on 07/10/15.
// Copyright (c) 2015 Giuseppe Nucifora. All rights reserved.
//
#import "PushNotificationManager.h"
@interface PushNotificationManager()
@property (nonatomic, strong) NSMutableArray *notificationHandlers;
@end
@implementation PushNotificationManager
static PushNotificationManager *SINGLETON = nil;
static bool isFirstAccess = YES;
#pragma mark - Public Method
+ (id)sharedInstance
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
isFirstAccess = NO;
SINGLETON = [[super allocWithZone:NULL] init];
});
return SINGLETON;
}
- (void) addNotificationHandler:(PushNotificationBlock) block {
if (block) {
if (![_notificationHandlers containsObject:block]) {
[_notificationHandlers addObject:block];
}
}
}
- (void) removeNotificationHandler:(PushNotificationBlock) block {
if (block) {
if ([_notificationHandlers containsObject:block]) {
[_notificationHandlers removeObject:block];
}
}
}
- (void) sendPushNotificationToHandlers:(NSDictionary*) userInfo {
[self getPushNotificationFromUserInfo:userInfo withBlockSuccess:^(PushNotification *pushNotification) {
for (int i =0; i<[_notificationHandlers count]; i++) {
PushNotificationBlock block = [_notificationHandlers objectAtIndex:i];
if (block) {
block(pushNotification);
}
}
} failure:^(BOOL status) {
}];
}
- (void) getPushNotificationFromUserInfo: (NSDictionary*) userInfo
withBlockSuccess: (void (^)(PushNotification *pushNotification))success
failure: (void (^)(BOOL status))failure {
PushNotification *notification = [[PushNotification alloc] initWithNotificationInfo:userInfo];
if (notification) {
if (success) {
success(notification);
}
}
else {
if (failure) {
failure(NO);
}
}
}
#pragma mark - Life Cycle
+ (id) allocWithZone:(NSZone *)zone
{
return [self sharedInstance];
}
+ (id)copyWithZone:(struct _NSZone *)zone
{
return [self sharedInstance];
}
+ (id)mutableCopyWithZone:(struct _NSZone *)zone
{
return [self sharedInstance];
}
- (id)copy
{
return [[PushNotificationManager alloc] init];
}
- (id)mutableCopy
{
return [[PushNotificationManager alloc] init];
}
- (id) init
{
if(SINGLETON){
return SINGLETON;
}
if (isFirstAccess) {
[self doesNotRecognizeSelector:_cmd];
}
self = [super init];
if (self) {
_notificationHandlers = [[NSMutableArray alloc] init];
}
return self;
}
@end
View File
Whitespace-only changes.