Last active
December 9, 2015 23:39
-
-
Save hagino3000/4345655 to your computer and use it in GitHub Desktop.
Use NotificationCenter for Cybozu Garoon3 schedules.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// AppDelegate.h | |
#import <Cocoa/Cocoa.h> | |
@interface AppDelegate : NSObject <NSApplicationDelegate, NSUserNotificationCenterDelegate> | |
// Windowはいらないのでxibファイルから消しておく | |
@property (assign) IBOutlet NSWindow *window; | |
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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