Last active
April 29, 2020 17:46
-
-
Save axldyb/44691b7a4fb905da6591 to your computer and use it in GitHub Desktop.
Darwin notifications listen and send for iOS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
typedef void (^DarwinNotificationBlock)(NSString *identifier); | |
@interface Darwin : NSObject | |
+ (Darwin *)sharedInstance; | |
- (void)startListening:(DarwinNotificationBlock)block; | |
- (void)test; | |
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "Darwin.h" | |
static NSString * const DarwinNotificationName = @"DarwinNotificationName"; | |
@interface Darwin () | |
@property (nonatomic, strong) DarwinNotificationBlock listenBlock; | |
@end | |
@implementation Darwin | |
+ (Darwin *)sharedInstance | |
{ | |
static Darwin *_sharedInstance = nil; | |
static dispatch_once_t once; | |
dispatch_once(&once, ^{ | |
_sharedInstance = [[self alloc] init]; | |
}); | |
return _sharedInstance; | |
} | |
- (instancetype)init | |
{ | |
if ((self = [super init])) { | |
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveMessageNotification:) name:DarwinNotificationName object:nil]; | |
[self registerForNotifications]; | |
} | |
return self; | |
} | |
- (void)startListening:(DarwinNotificationBlock)block | |
{ | |
self.listenBlock = block; | |
} | |
- (void)registerForNotifications { | |
CFNotificationCenterRef const center = CFNotificationCenterGetDarwinNotifyCenter(); | |
CFStringRef str = (__bridge CFStringRef)@"Test_identifier"; | |
CFNotificationCenterAddObserver(center, (__bridge const void *)(self), notificationCallback, str, NULL, CFNotificationSuspensionBehaviorDeliverImmediately); | |
} | |
- (void)test | |
{ | |
CFNotificationCenterRef const center = CFNotificationCenterGetDarwinNotifyCenter(); | |
CFDictionaryRef const userInfo = NULL; | |
BOOL const deliverImmediately = YES; | |
CFStringRef str = (__bridge CFStringRef)@"Test_identifier"; | |
CFNotificationCenterPostNotification(center, str, NULL, userInfo, deliverImmediately); | |
} | |
void notificationCallback(CFNotificationCenterRef center, void * observer, CFStringRef name, void const * object, CFDictionaryRef userInfo) | |
{ | |
NSString *identifier = (__bridge NSString *)name; | |
[[NSNotificationCenter defaultCenter] postNotificationName:DarwinNotificationName object:nil userInfo:@{@"identifier" : identifier}]; | |
} | |
- (void)didReceiveMessageNotification:(NSNotification *)notification | |
{ | |
NSDictionary *userInfo = notification.userInfo; | |
NSString *identifier = [userInfo valueForKey:@"identifier"]; | |
if (self.listenBlock != nil) { | |
self.listenBlock(identifier); | |
} | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment