Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save subtleGradient/a41c9c93a313120777e7 to your computer and use it in GitHub Desktop.
Save subtleGradient/a41c9c93a313120777e7 to your computer and use it in GitHub Desktop.
var $ = require('NodObjC');
module.exports = function installNSBundleHook() {
var cls = $.NSBundle;
if (cls) {
var bundleIdentifier = cls.getInstanceMethod('bundleIdentifier');
bundleIdentifier.setImplementation(function(val) {
return $('com.tinyspeck.slackmacgap');
});
return true;
}
return false;
};
var $ = require('NodObjC');
module.exports = function(props){
var NSUserNotificationCenterDelegate = $.NSObject.extend('NSUserNotificationCenterDelegate');
NSUserNotificationCenterDelegate.addMethod('init', { retval: '@', args: ['@', ':'] }, function(self) {
$.NSUserNotificationCenter('defaultUserNotificationCenter')('setDelegate', self);
return self;
});
NSUserNotificationCenterDelegate.addMethod('sendNotificationWithTitle:withInfo:andSoundNamed:', {
retval: 'v',
args: ['@', ':', '@', '@', '@']
}, function(self, methodName, title, informativeText, soundName) {
console.log(arguments);
var center = $.NSUserNotificationCenter('defaultUserNotificationCenter');
var notification = $.NSUserNotification('alloc')('init');
notification('setTitle', title);
notification('setInformativeText', informativeText);
notification('setSoundName', soundName);
center('deliverNotification', notification);
});
NSUserNotificationCenterDelegate.addMethod('userNotificationCenter:didActivateNotification:', {
retval: 'v',
args: ['@', ':', '@', '@']
}, function(self, e, center, notification) {
console.log('CLICKED');
props.onClick && props.onClick(notification);
});
NSUserNotificationCenterDelegate.addMethod('userNotificationCenter:shouldPresentNotification:', {
retval: 'v',
args: ['@', ':', '@', '@']
}, function(self) {
return true;
});
var center = NSUserNotificationCenterDelegate('alloc')('init');
return center;
}
var $ = require('NodObjC');
$.import('Cocoa');
var createMyAwesomeNSUserNotificationCenterDelegate = require('./MyAwesomeNSUserNotificationCenterDelegate');
var installNSBundleHook = require('./installNSBundleHook');
function main(){
installNSBundleHook();
var pool = $.NSAutoreleasePool('alloc')('init');
var center = createMyAwesomeNSUserNotificationCenterDelegate({
onClick: function(notification){
console.log('clicked', notification);
center('sendNotificationWithTitle', $('Clicked!'),
'withInfo', $(''),
'andSoundNamed', $('NSUserNotificationDefaultSoundName'));
}
});
center('sendNotificationWithTitle', $('Hello Objective-C World'),
'withInfo', $('from NodObjC'),
'andSoundNamed', $('NSUserNotificationDefaultSoundName'));
setInterval(function(){
$.NSRunLoop('mainRunLoop')('runUntilDate', $(new Date(Date.now() + 100)));
}, 100);
}
main();
{
"name": "NodObjC-notification-example",
"version": "0.0.1",
"dependencies": {
"NodObjC": "0.0.15"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment