Skip to content

Instantly share code, notes, and snippets.

@TooTallNate
Last active January 11, 2019 10:55
Show Gist options
  • Star 24 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save TooTallNate/3551396342c054cbc39d to your computer and use it in GitHub Desktop.
Save TooTallNate/3551396342c054cbc39d to your computer and use it in GitHub Desktop.
NodObjC NSUserNotificationCenter example - inspired by https://github.com/norio-nomura/usernotification/blob/master/usernotification/main.m
var $ = require('NodObjC');
$.import('Cocoa');
var installNSBundleHook = function() {
var cls = $.NSBundle;
if (cls) {
var bundleIdentifier = cls.getInstanceMethod('bundleIdentifier');
bundleIdentifier.setImplementation(function(val) {
return $('com.tinyspeck.slackmacgap');
});
return true;
}
return false;
};
installNSBundleHook();
var pool = $.NSAutoreleasePool('alloc')('init');
var NSUserNotificationCenterDelegate = $.NSObject.extend('NSUserNotificationCenterDelegate');
NSUserNotificationCenterDelegate.addMethod('init', { retval: '@', args: ['@', ':'] }, function(self) {
$.NSUserNotificationCenter('defaultUserNotificationCenter')('setDelegate', self);
return self;
});
NSUserNotificationCenterDelegate.addMethod('sendNotificationWithTitle:withInfo:', {
retval: 'v',
args: ['@', ':', '@', '@']
}, function(self) {
console.log(arguments);
var center = $.NSUserNotificationCenter('defaultUserNotificationCenter');
var notification = $.NSUserNotification('alloc')('init');
notification('setTitle', $('title'));
notification('setInformativeText', $('hello, world!'));
notification('setSoundName', $('NSUserNotificationDefaultSoundName'));
center('deliverNotification', notification);
});
NSUserNotificationCenterDelegate.addMethod('userNotificationCenter:didActivateNotification:', {
retval: 'v',
args: ['@', ':', '@', '@']
}, function(self, e, center, notification) {
console.log('CLICKED');
});
NSUserNotificationCenterDelegate.addMethod('userNotificationCenter:shouldPresentNotification:', {
retval: 'v',
args: ['@', ':', '@', '@']
}, function(self) {
return true;
});
var center = NSUserNotificationCenterDelegate('alloc')('init');
var string = $.NSString('stringWithUTF8String', 'Hello Objective-C World!');
console.log(string);
center('sendNotificationWithTitle', string, 'withInfo', string);
$.NSRunLoop('mainRunLoop')('run');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment