2012-02-03 104 views

回答

9

你需要到當地的響應通知在兩個地方在你的應用程序代理:

- (BOOL) application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 

- (void) application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification 

第一種是當你的應用程序沒有運行 - 使用launchOptions參數檢查您的應用是否因本地通知而啓動。

第二個是當你的應用程序當前正在運行(活動或不活動)。您可以通過檢查application:didReceiveLocalNotification:方法中的NSApplication的applicationState屬性來檢查應用程序是否處於非活動狀態。

2
- (void)sendNotification 
{ 
    UILocalNotification *localNotification = [[UILocalNotification alloc] init]; 
// notification.repeatInterval = NSDayCalendarUnit; 
    localNotification.fireDate = vwPicker.date; 
    localNotification.alertBody = txtAlarmTitle.text; 
    localNotification.timeZone = [NSTimeZone defaultTimeZone]; 
    localNotification.userInfo = @{@"Title": txtAlarmTitle.text}; 
    localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1; 

    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification]; 
} 


- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification 
{ 
    [self handleNotification:notification application:application]; 
} 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    UILocalNotification *localNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey]; 
    if (localNotification) 
     [self handleNotification:localNotification application:application]; 

    return YES; 
} 

-(void)handleNotification: (UILocalNotification *)notification application:(UIApplication *)application 
{ 
    NSString *title = [notification.userInfo objectForKey:@"Title"]; 

    [[[UIAlertView alloc]initWithTitle:@"Smart Alarm" message:title delegate:self cancelButtonTitle:@"Answer the Teaser" otherButtonTitles: nil] show]; 

    application.applicationIconBadgeNumber = 0; 
} 
相關問題