Skip to content

Instantly share code, notes, and snippets.

@hagino3000
Last active December 9, 2015 23:39
Show Gist options
  • Save hagino3000/4345655 to your computer and use it in GitHub Desktop.
Save hagino3000/4345655 to your computer and use it in GitHub Desktop.
Use NotificationCenter for Cybozu Garoon3 schedules.
// AppDelegate.h
#import <Cocoa/Cocoa.h>
@interface AppDelegate : NSObject <NSApplicationDelegate, NSUserNotificationCenterDelegate>
// Windowはいらないのでxibファイルから消しておく
@property (assign) IBOutlet NSWindow *window;
@end
// AppDelegate.m
#define DATA_FILE_PATH @"/Users/hagino3000/tmp/data.json"
#define CHECK_INTERVAL 20 * 60
#import "AppDelegate.h"
@implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
[NSUserNotificationCenter defaultUserNotificationCenter].delegate = self;
// 初回起動
[self checkSchedule];
// 次回以降はタイマーで起動
[NSTimer scheduledTimerWithTimeInterval:CHECK_INTERVAL
target:self
selector:@selector(checkSchedule)
userInfo:nil
repeats:YES];
}
- (void)checkSchedule {
// ファイルでスケジュールデータを受けとる
NSInputStream* stream = [NSInputStream inputStreamWithFileAtPath:DATA_FILE_PATH];
[stream open];
NSError *error;
NSArray *arr = [NSJSONSerialization JSONObjectWithStream:stream
options:NSJSONReadingMutableContainers
error:&error];
[stream close];
for (int i = 0; i < arr.count; i++) {
NSDictionary *dict = [arr objectAtIndex:i];
[self sendNotification:[dict objectForKey:@"title"]
subtitle:[dict objectForKey:@"time"]
body:[dict objectForKey:@"body"]
url:[dict objectForKey:@"url"]];
}
}
- (void)sendNotification:(NSString *)title subtitle:(NSString *)subtitle body:(NSString *)body url:(NSString *)url {
// UserNotificationの生成
NSUserNotification *userNotification = [[NSUserNotification alloc] init];
userNotification.actionButtonTitle = @"開く";
userNotification.title = title;
userNotification.subtitle = subtitle;
userNotification.informativeText = body;
userNotification.userInfo = [[NSDictionary alloc] initWithObjectsAndKeys:url, @"url", nil];
// 通知センターに送る
[[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification:userNotification];
NSLog(@"done!!");
}
- (void)userNotificationCenter:(NSUserNotificationCenter *)center didActivateNotification:(NSUserNotification *)notification {
// アクションボタンがクリックされたら、スケジュールのページを開く
NSURL *url = [NSURL URLWithString:[notification.userInfo valueForKey:@"url"]];
[[NSWorkspace sharedWorkspace] openURL:url];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment