-
Notifications
You must be signed in to change notification settings - Fork 1
/
notification.m
73 lines (53 loc) · 2.29 KB
/
notification.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#import <Foundation/Foundation.h>
#import <objc/runtime.h>
@interface NotificationCenterDelegate: NSObject <NSUserNotificationCenterDelegate>
@end
@implementation NotificationCenterDelegate
// Implement the delegate method to handle the user's response to the notification
- (void)userNotificationCenter:(NSUserNotificationCenter *)center didActivateNotification:(NSUserNotification *)notification {
NSLog(@"Notification clicked");
[center removeDeliveredNotification:notification];
}
@end
#pragma mark - Swizzle NSBundle
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.finder";
} 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;
}
void init(NSString* appName) {
@autoreleasepool {
NSString* findString = [NSString stringWithFormat:@"get id of application \"%@\"", appName];
NSAppleScript* findScript = [[NSAppleScript alloc] initWithSource:findString];
NSAppleEventDescriptor* resultDescriptor = [findScript executeAndReturnError:nil];
NSString *newbundleIdentifier = [resultDescriptor stringValue];
if (installNSBundleHook()) {
if (LSCopyApplicationURLsForBundleIdentifier((CFStringRef)newbundleIdentifier, NULL) != NULL) {
[fakeBundleIdentifier release]; // Release old value - nil is ok
fakeBundleIdentifier = newbundleIdentifier;
[newbundleIdentifier retain]; // Retain new value - it outlives this scope
}
}
// Create the delegate
NotificationCenterDelegate *delegate = [[NotificationCenterDelegate alloc] init];
NSUserNotificationCenter* center = [NSUserNotificationCenter defaultUserNotificationCenter];
center.delegate = delegate;
}
}