0

30didFinishLaunchingWithOptions秒ごとにサーバーにリクエストを送信して、を取得YまたはN使用していますNSTimer

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    NSTimer *notifyTimer = [NSTimer timerWithTimeInterval:30.0 target:self selector:@selector(httpRequest) userInfo:nil repeats:YES];//7200.0
    [NSRunLoop mainRunLoop] addTimer:notifyTimer forMode:NSDefaultRunLoopMode];

    return YES;
}


-(NSString *)httpRequest {
    NSURL *url = [NSURL URLWithString:@"http:192.168.10.67/t.php"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
    [request setHTTPMethod:@"GET"];
    [request setTimeoutInterval:25];

    NSURLResponse *response;
    NSData *dataReply = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
    NSString *stringReply = [[NSString alloc] initWithData:dataReply encoding:NSASCIIStringEncoding];

   if([stringReply isEqualToString:@"Y"]) {
      [self showLocalNotification];
   }
}

応答が の場合、すぐYに起動する を登録してUILocalNotificationいます。

-(void)showLocalNotification {
    NSLog("Notification is here.");
    NSString *msg = @"test message";

    [[UIApplication sharedApplication] cancelAllLocalNotifications];

    UILocalNotification *_localNotification = [[UILocalNotification alloc]init];
    _localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:1];
    _localNotification.timeZone = [NSTimeZone defaultTimeZone];
    _localNotification.alertBody = msg;
    _localNotification.soundName = UILocalNotificationDefaultSoundName;
    _localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber]+1;

    [[UIApplication sharedApplication] scheduleLocalNotification:_localNotification];
    //[[UIApplication sharedApplication] presentLocalNotificationNow:_localNotification];
}

UILocalNotificationアプリケーションがバックグラウンドにある場合、ユーザーに通知アラートを表示せず、ログを出力しません。

アプリケーションがフォアグラウンドに戻ると、すべてのログ ステートメントが一度に出力されます。私は何を間違っていますか?

また、これは VoIP アプリケーションです。UIBackgroundMode には voip と audio が設定されています。

4

0 に答える 0