2012-05-10 16 views
0

我想在應用程序終止之前使用XMPPFramework在「10分鐘窗口」中顯示背景通知。看起來,所有的網絡請求都在排隊,而它在後臺,並且xmppStream:didReceiveMessage:在應用被帶回到前臺之前不會被調用。XMPPFramework背景UILocalNotification支持沒有VoIP plist鍵

我注意到像動詞這樣的應用程序支持這種行爲。他們顯然不是VoIP應用程序,所以我很好奇他們是如何完成這一行爲的。

+0

爲什麼只有「10分鐘窗口」。 我們可以增加到更多? – iChirag

回答

3

如果有其他人有這個問題,你可以通過將此代碼添加到你的應用程序委託來解決它。我的完整源代碼在這裏,如果你想看看它:OTRAppDelegate.m。其他相關代碼在receiveMessage中:OTRBuddy.m

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

    self.backgroundTask = [application beginBackgroundTaskWithExpirationHandler: ^{ 
     dispatch_async(dispatch_get_main_queue(), ^{ 
      NSLog(@"Background task expired"); 
      if (self.backgroundTimer) 
      { 
       [self.backgroundTimer invalidate]; 
       self.backgroundTimer = nil; 
      } 
      [application endBackgroundTask:self.backgroundTask]; 
      self.backgroundTask = UIBackgroundTaskInvalid; 
     }); 
    }]; 

    dispatch_async(dispatch_get_main_queue(), ^{ 
     self.backgroundTimer = [NSTimer scheduledTimerWithTimeInterval:10 target:self selector:@selector(timerUpdate:) userInfo:nil repeats:YES]; 
    }); 
} 

- (void) timerUpdate:(NSTimer*)timer { 
    UIApplication *application = [UIApplication sharedApplication]; 

    NSLog(@"Timer update, background time left: %f", application.backgroundTimeRemaining); 

    if ([application backgroundTimeRemaining] < 60 && !self.didShowDisconnectionWarning) 
    { 
     UILocalNotification *localNotif = [[UILocalNotification alloc] init]; 
     if (localNotif) { 
      localNotif.alertBody = EXPIRATION_STRING; 
      localNotif.alertAction = OK_STRING; 
      [application presentLocalNotificationNow:localNotif]; 
     } 
     self.didShowDisconnectionWarning = YES; 
    } 
    if ([application backgroundTimeRemaining] < 10) 
    { 
     // Clean up here 
     [self.backgroundTimer invalidate]; 
     self.backgroundTimer = nil; 
     [application endBackgroundTask:self.backgroundTask]; 
     self.backgroundTask = UIBackgroundTaskInvalid; 
    } 
} 

- (void)applicationWillEnterForeground:(UIApplication *)application 
{ 

} 

- (void)applicationDidBecomeActive:(UIApplication *)application 
{ 
    /* 
    Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 
    */ 
    NSLog(@"Application became active"); 
    if (self.backgroundTimer) 
    { 
     [self.backgroundTimer invalidate]; 
     self.backgroundTimer = nil; 
    } 
    if (self.backgroundTask != UIBackgroundTaskInvalid) 
    { 
     [application endBackgroundTask:self.backgroundTask]; 
     self.backgroundTask = UIBackgroundTaskInvalid; 
    } 

    application.applicationIconBadgeNumber = 0; 
}