2013-04-03 123 views
0

問題是,我以編程方式放置了日期選擇器。在日期選擇器,我只是用來顯示時間從1分鐘到23小時。用戶應該在選擇器中設置時間,並設置通知。 現在,我在後臺收到通知,但只有一次。我必須重複時間,直到計時器不會停止。在特定的時間間隔重複報警

我發現太多的聯繫和來源,但沒有能夠解決這個問題

我的代碼:

在Appdelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    application.applicationIconBadgeNumber = 0; 

    //------ Handle launching from a notification------- 
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)]; 

    self.localNotification =[launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey]; 
    if (self.localNotification) 
    { 
     application.applicationIconBadgeNumber = self.localNotification.applicationIconBadgeNumber-1; 
     NSLog(@"badge number: %d", application.applicationIconBadgeNumber); 
     [self playSoundWithNotification:self.localNotification]; 
    } 
    else 
    { 
     [[UIApplication sharedApplication]cancelAllLocalNotifications]; 
    } 
} 

didenterinbackground:

- (void)applicationDidEnterBackground:(UIApplication *)application 
{ 
    NSLog(@"Application entered background state."); 
    NSAssert(self->bgTask == UIBackgroundTaskInvalid, nil); 

    bgTask = [application beginBackgroundTaskWithExpirationHandler: ^{ 
     dispatch_async(dispatch_get_main_queue(), ^{ 
      [application endBackgroundTask:self->bgTask]; 
      self->bgTask = UIBackgroundTaskInvalid; 
     }); 
    }]; 

    dispatch_async(dispatch_get_main_queue(), ^{ 
     while ([application backgroundTimeRemaining] > 1.0) 
     { 
      NSString *str_friend = @"Hello,"; 
      if (str_friend) 
      { 
       UILocalNotification *localNotif = [[UILocalNotification alloc] init]; 
       if (localNotif) 
       { 
        localNotif.alertBody = [NSString stringWithFormat: 
              NSLocalizedString(@"%@ has a message for you.", nil), str_friend]; 
        localNotif.alertAction = NSLocalizedString(@"Read Msg", nil); 
        localNotif.soundName = @"alarmsound.caf"; 
        //localNotif.soundName =[NSString stringWithFormat:@"%@.mp3",str_Alarm]; 
        localNotif.applicationIconBadgeNumber = 1; 
        [application presentLocalNotificationNow:localNotif]; 

        NSLog(@"sound: %@, alertAction: %@, alerBody: %@",localNotif.soundName, localNotif.alertAction, localNotif.alertBody); 
        str_friend = nil; 
        break; 
       } 
      } 
     } 
     [application endBackgroundTask:self->bgTask]; 
     self->bgTask = UIBackgroundTaskInvalid; 
    }); 
} 

-

(void)playSoundWithNotification:(UILocalNotification *)notification 
{ 
    notification.soundName=[NSString stringWithFormat:@"%@.mp3",str_Alarm]; 
    NSLog(@"soundname: %@",notification.soundName); 
} 

- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif 
{ 
    testDate=notif.fireDate; 
    NSLog(@"DATE IS: %@, %@",testDate, notif.fireDate); 
    // Handle the notificaton when the app is running 
    NSLog(@"Recieved Notification %@",notif); 
    [self playSoundWithNotification:notif]; 

    [self _showAlert:[NSString stringWithFormat:@"%@",str_Motivation] withTitle:@"Daily Achiever"]; 
} 

- (void) _showAlert:(NSString*)pushmessage withTitle:(NSString*)title 
{ 
    UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:title message:pushmessage delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
    [alertView show]; 
    if (alertView) 
    { 
    } 
} 
在myviewcontroller

-(void)insert:(NSDate *)fire 
{ 
    appDelegate=(AppDelegate *)[[UIApplication sharedApplication] delegate]; 
    appDelegate.localNotification = [[UILocalNotification alloc] init]; 
    NSLog(@"localNotification %@", appDelegate.localNotification); 

    if (appDelegate.localNotification == nil) 
     return; 

    appDelegate.localNotification.fireDate = fire; 
    appDelegate.localNotification.timeZone = [NSTimeZone defaultTimeZone]; 
    appDelegate.localNotification.alertBody = appDelegate.str_Motivation; 
    appDelegate.localNotification.alertAction = @"View"; 
    appDelegate.localNotification.soundName = [NSString stringWithFormat:@"%@.mp3",appDelegate.str_Alarm]; 

    appDelegate.localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber]+1; 
    NSLog(@"localNotification.alertBody %@", appDelegate.localNotification.alertBody); 
    NSLog(@"localNotification.soundName %@",appDelegate.localNotification.soundName); 
    [[UIApplication sharedApplication] scheduleLocalNotification:appDelegate.localNotification]; 
} 

請幫助。

回答

0
- (void)applicationDidEnterBackground:(UIApplication *)application 
{ 
    ...... 
    ...... 
    if (localNotif) 
    { 
     localNotif.alertBody = [NSString stringWithFormat: 
               NSLocalizedString(@"%@ has a message for you.", nil), str_friend]; 
     localNotif.alertAction = NSLocalizedString(@"Read Msg", nil); 
     localNotif.soundName = @"alarmsound.caf"; 
     localNotif.applicationIconBadgeNumber = 1; 
     [localNotif setRepeatInterval:NSMinuteCalendarUnit]; 
     [application presentLocalNotificationNow:localNotif]; 

     str_friend = nil; 
     break; 
    } 
} 

我得到了解決方案。