Skip to content

Instantly share code, notes, and snippets.

@ElDeveloper
Created September 11, 2014 06:32
Show Gist options
  • Save ElDeveloper/f7e8b19aff23550af426 to your computer and use it in GitHub Desktop.
Save ElDeveloper/f7e8b19aff23550af426 to your computer and use it in GitHub Desktop.
notifier
//
// main.m
// badonkas
//
// Created by Yoshiki Vázquez Baeza on 21/07/13.
// Copyright (c) 2013 Yoshiki Vázquez Baeza. All rights reserved.
//
// Based on the source code as written by Norio Nomura for the project
// usernotification https://github.com/norio-nomura/usernotification
// gcc -framework Foundation main.m
//
#import <Foundation/Foundation.h>
#import <objc/runtime.h>
NSString *fakeBundleIdentifier = nil;
@implementation NSBundle(swizle)
// Overriding bundleIdentifier works, but overriding NSUserNotificationAlertStyle does not work.
- (NSString *)__bundleIdentifier{
if (self == [NSBundle mainBundle]) {
return fakeBundleIdentifier ? fakeBundleIdentifier : @"com.apple.terminal";
} else {
return [self __bundleIdentifier];
}
}
@end
BOOL installNSBundleHook()
{
Class class = objc_getClass("NSBundle");
if (class) {
method_exchangeImplementations(class_getInstanceMethod(class, @selector(bundleIdentifier)),
class_getInstanceMethod(class, @selector(__bundleIdentifier)));
return YES;
}
return NO;
}
#pragma mark - NotificationCenterDelegate
@interface NotificationCenterDelegate : NSObject<NSUserNotificationCenterDelegate>
@property (nonatomic, assign) BOOL keepRunning;
@end
@implementation NotificationCenterDelegate
- (void)userNotificationCenter:(NSUserNotificationCenter *)center didDeliverNotification:(NSUserNotification *)notification
{
self.keepRunning = NO;
}
@end
int main(int argc, const char * argv[])
{
@autoreleasepool {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *message = [defaults stringForKey:@"-message"] ?: @"";
NSString *title = [defaults stringForKey:@"-title"] ?: @"Notification";
NSString *subtitle = [defaults stringForKey:@"-subtitle"] ?: @"";
if (installNSBundleHook()) {
fakeBundleIdentifier = [defaults stringForKey:@"identifier"];
NSUserNotificationCenter *nc = [NSUserNotificationCenter defaultUserNotificationCenter];
NotificationCenterDelegate *ncDelegate = [[NotificationCenterDelegate alloc]init];
ncDelegate.keepRunning = YES;
nc.delegate = ncDelegate;
NSUserNotification *note = [[NSUserNotification alloc] init];
note.title = title;
note.subtitle = subtitle;
note.informativeText = message;
[nc deliverNotification:note];
while (ncDelegate.keepRunning) {
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.1]];
}
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment