2013-06-25 31 views
0

我已經在一個長期生活的應用程序中實現了本地通知。該應用程序每天運行24小時亭式模式。其中一個本地通知每天觸發一次,另一個觸發每小時一次。每天觸發一次的通知將刪除前一天的所有本地核心數據信息。每小時觸發一次的通知是應用程序的「心跳」,並且每小時在服務器上創建一次檢查。如何區分本地通知

下面是每小時心跳計劃(這是在我的主視圖 - 控制):

- (void)scheduleHeartBeat 
{ 
[[UIApplication sharedApplication] cancelAllLocalNotifications]; 

UILocalNotification *heartbeat = [[UILocalNotification alloc] init]; 

NSCalendar *calendar = [NSCalendar currentCalendar]; 
NSDateComponents *components = [[NSDateComponents alloc] init]; 

components = [[NSCalendar currentCalendar] components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:[NSDate date]]; 

NSInteger day = [components day]; 
NSInteger month = [components month]; 
NSInteger year = [components year]; 

[components setDay: day]; 
[components setMonth: month]; 
[components setYear: year]; 
[components setHour: 00]; 
[components setMinute: 10]; 
[components setSecond: 0]; 
[calendar setTimeZone: [NSTimeZone systemTimeZone]]; 
NSDate *dateToFire = [calendar dateFromComponents:components]; 

heartbeat.fireDate = dateToFire; 
heartbeat.timeZone = [NSTimeZone systemTimeZone]; 
heartbeat.repeatInterval = NSHourCalendarUnit; 

[[UIApplication sharedApplication] scheduleLocalNotification:heartbeat]; 
} 

我調用viewDidLoad中上述方法。

然後在我的AppDelegate我有以下幾點:

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification 
{ 
application.applicationIconBadgeNumber = 0; 
[[OLEngine myEngine] deleteStuffFromYesterday:@"MyObject"]; 

} 

在didReceiveLocalNotification我需要區分它們之間本地通知已經被解僱了,因爲我不想調用方法每隔一小時deleteStuffFromYesterday做的 - 每一次天。

如何在我的應用程序委託代碼中區分這些計劃的本地通知?

回答

4

您可以使用userInfo屬性爲此存儲NSDictionary

商店的信息:

localNotification.userInfo = @{ @"myCustomType" : @"heartbeat" }; 

檢索信息:

NSString *myCustomType = localNotification.userInfo[@"myCustomType"]; 
+0

你有這種與通知的例子嗎? – motionpotion

+0

只需添加一些代碼。這只是一本應該熟悉的字典。 – Karl

+0

感謝您的幫助。它很棒! – motionpotion