2012-02-03 95 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; 
} 
相关问题