2013年4月26日 星期五

[iOS Dev] UILocalNotification in particular time - 指定時間本地通知

Add Local Notification at particular time

//
// AppDelegate.m
//
- (void)applicationDidEnterBackground:(UIApplication *)application
{
NSDate *alertTime = [[NSDate date]
dateByAddingTimeInterval:10];
NSDate *alertTime2 = [[NSDate date]
dateByAddingTimeInterval:30];
NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setDay:25];
[comps setMonth:4];
[comps setYear:2013];
[comps setHour:13];
[comps setMinute:46];
NSDate *alertTime3 = [cal dateFromComponents:comps];
NSLog(@"%@", [NSDate date]);
NSLog(@"%@", alertTime);
NSLog(@"%@", alertTime2);
NSLog(@"%@", alertTime3);
[self addNewSchedult:alertTime];
[self addNewSchedult:alertTime2];
[self addNewSchedult:alertTime3];
}
- (void)addNewSchedult:(NSDate *)d {
UIApplication* app = [UIApplication sharedApplication];
UILocalNotification* notifyAlarm = [[UILocalNotification alloc] init];
if (notifyAlarm)
{
notifyAlarm.fireDate = d;
notifyAlarm.timeZone = [NSTimeZone defaultTimeZone];
notifyAlarm.repeatInterval = 0;
// notifyAlarm.soundName = @"abc.mp3";
notifyAlarm.alertBody = [NSString stringWithFormat: @"Time is money! %@", d];
[app scheduleLocalNotification:notifyAlarm];
}
}

Cancel one particular LocalNotification

UIApplication *app = [UIApplication sharedApplication];
NSArray *events = [app scheduledLocalNotifications];
cancelNid = @"1";
for (int i=0; i<[events count]; i++)
{
UILocalNotification* notify = [eventArray objectAtIndex:i];
NSString *uid = [NSString stringWithFormat:@"%@",[notify.userInfo valueForKey:@"nid"]];
if ([nid isEqualToString:cancelNid])
{
[app cancelLocalNotification:notify];
break;
}
}

2013年4月25日 星期四

[NodeJS] Auto deploy by github hook

Github hook service會在有新的commit的時候觸發hook API(bitbucket也有), 這可以用在

  • 遠端Server自動部署, 才不用每次都ssh到遠端Server去git pull然後deploy一遍
  • 自動發訊息到團隊的協作平台或是寄發Email
  • ...

Github本身支援很多好用的服務, 那我是用WebHook在我的主機上, 當有commit時會觸發Server上的hook, 然後執行一個Shell Script, 自動git pull然後重新啓動我的Web Service

$ cd /var/web/project
$ git pull
$ forever restart /var/web/project/app.js

Code
https://github.com/hankwang/node-github-hook-deploy


2013年4月2日 星期二

[Linux] 使用 Crontab 定時執行程式

為什麼用 crontab ?

執行例行性的工作, 例:

  • 定期備份資料
  • 定期寄送Server log到某個Email
  • 定期檢查系統狀態

crontab 指令

$ crontab -l # 列出所有 cron jobs
$ crontab -r # 刪除所有 cron jobs
$ crontab -e # 編輯目前使用者的 cron jobs
$ crontab [-u  user] -l # 列出某個使用者的cron jobs (要有權限) 

時間格式 time format

  • minutes 0-59
  • hour 0-23
  • day of month 1-31
  • month 1-12
  • day of week 0-7 (0 or 7 is Sun)

crontab sample


See Also

crontab(5) - Linux man page

[Sublime Text] 我的Sublime Text 2設定檔

高亮目前選取行"highlight_line": true

停用Vintage模式 (Vintage Mode) "ignored_packages": [ "Vintage" ],

Soda樣式的tab"soda_classic_tabs": false

tab的大小"tab_size": 4

把tab轉為空白鍵"translate_tabs_to_spaces": true

存檔時移除不必要的空白"trim_trailing_white_space_on_save": true

偵測跑很慢的plugin, 會跳出警告"detect_slow_plugins": false

完整的設定檔

Related Posts Plugin for WordPress, Blogger...