Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/42.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 在特定时间间隔重复报警_Iphone_Ios5_Ios6_Uilocalnotification - Fatal编程技术网

Iphone 在特定时间间隔重复报警

Iphone 在特定时间间隔重复报警,iphone,ios5,ios6,uilocalnotification,Iphone,Ios5,Ios6,Uilocalnotification,问题是,我以编程方式放置了一个日期选择器。在日期选择器中,我只是用来显示从1分钟到23小时的时间。用户应该在选择器中设置时间,并设置通知。 现在,我在后台收到通知,但只有一次。我必须重复时间,直到计时器停止 我发现太多的链接和来源,但无法解决这个问题 我的代码: 在Appdelegate.m中 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchO

问题是,我以编程方式放置了一个日期选择器。在日期选择器中,我只是用来显示从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];
    }
}
迪登特背景:

- (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;
    });
}
-

在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];
}
请帮忙

- (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;
     }
}
我找到了解决办法

- (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;
     }
}