2011-11-22 198 views
1

我的應用程序主要是一個真正沒有連接到互聯網的服務器的客戶端。它連接到Polycom編解碼器並管理2個端點之間的視頻呼叫。所以我的應用程序可以發送像結束呼叫,音量等命令... 但是我的問題是這樣的。當來電發生且應用程序不在前臺時,我需要某種通知。 由於服務器沒有互聯網接入APNS /推送通知不適用於我。我已經研究過像this這樣的事情。這似乎確保我的客戶端正在運行,但是由於我的應用程序在後臺,所以我無法做出提示。本地通知?

因此,除了如何解決我的問題的基礎知識我的問題是:

我能帶我的應用程序使用的鏈接中列出的技術前景(做這樣的事情在下面,我在做什麼)。我可以從日誌中看到,此代碼使我的代碼保持運行。我知道我的while循環是不正確的,最終我會需要KVO,但不管這個答案是否會產生效果。 (有一點我不明白的是這仍然運行我的整個應用程序,而不是僅僅類我在那裏有bcClient?)

- (void)applicationDidEnterBackground:(UIApplication *)application 
{  
    [bcClient connect]; 
    bgTask = [app beginBackgroundTaskWithExpirationHandler:expirationHandler]; 

    // Start the long-running task and return immediately. 
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 

      while(1) { 
       sleep(3); 
       NSLog(@"held join %d",bcClient.heldjoin); 

       if (bcClient.heldjoin == 602 || bcClient.heldjoin == 604 || bcClient.heldjoin == 513) { 
        NSLog(@"incoming call"); 
       } 
      } 
    });   
} 

如果我不能讓我的應用程序到前臺,然後在那裏反正推通知本地(不需要APNS服務器)?

我有一種感覺,這是不可能的,但我想我會問。

+0

如果你可以把你的應用程序放在前臺,比啞應用程序可以做到這一點 - 你永遠無法讓它們消失。這不會縮放。 –

+0

@ B蜥蜴可以請你撤銷你做的事。這是問題的答案。不是問題的一部分。如果有什麼刪除我的後續問題。 –

+0

當然。你應該把後續問題提出一個新的問題。 –

回答

0

這是我的答案。這使我的客戶端應用程序在後臺運行,並顯示一個通知,當有來電的用武之地。

AppDelegate.h

@interface CameleonAppDelegate : NSObject <UIApplicationDelegate> { 

    CrestronClient *cClient; 
    CrestronControllerValues *CCV; 
    RootViewController *rootViewController; 
    CrestronValues *crestronValues; 

    UIBackgroundTaskIdentifier bgTask; 
    dispatch_block_t expirationHandler; 
    UIApplication* app; 
    BOOL showedCall; 
} 
@property (nonatomic, retain) IBOutlet UIWindow *window; 

@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext; 
@property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel; 
@property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator; 

- (void)saveContext; 
- (NSURL *)applicationDocumentsDirectory; 
-(void)save; 
-(void)load; 
- (void)backgroundHandler; 
@end 

AppDelegate.m(只是didFinishLaunchingWithOptionsapplicationDidEnterBackground

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{  
    app = [UIApplication sharedApplication]; 
    expirationHandler = ^{ 

      [app endBackgroundTask:bgTask]; 
      bgTask = UIBackgroundTaskInvalid; 


      bgTask = [app beginBackgroundTaskWithExpirationHandler:expirationHandler]; 
    }; 


    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 

    NSArray *keys = [NSArray arrayWithObjects:@"IPaddress", @"PortNumber",@"IPID", nil]; 

    NSArray *objs = [NSArray arrayWithObjects:@"10.8.40.64", @"41794",@"3", nil]; 

    //10.8.30.143  10.8.40.64 

    NSDictionary *dict = [NSDictionary dictionaryWithObjects:objs forKeys:keys]; 

    [defaults registerDefaults:dict]; 

    CCV = [CrestronControllerValues sharedManager]; 

    [CCV setIpAddress:[defaults stringForKey:@"IPaddress"]]; 
    [CCV setPortNumber:[defaults stringForKey:@"PortNumber"]]; 
    [CCV setIPID:[defaults stringForKey:@"IPID"]]; 


    cClient = [CrestronClient sharedManager]; 


    rootViewController = [[RootViewController alloc]initWithNibName:@"RootViewController" bundle:nil]; 
    self.window.rootViewController = rootViewController; 
    [self.window makeKeyAndVisible]; 

    return YES; 
} 


- (void)applicationDidEnterBackground:(UIApplication *)application 
{ 
    showedCall = FALSE; 
    BOOL backgroundAccepted = [[UIApplication sharedApplication] setKeepAliveTimeout:600 handler:^{ [self backgroundHandler]; }]; 
    if (backgroundAccepted) 
    { 
      NSLog(@"VOIP backgrounding accepted"); 
    } 


    bgTask = [app beginBackgroundTaskWithExpirationHandler:^{ 
      [app endBackgroundTask:bgTask]; 
      bgTask = UIBackgroundTaskInvalid; 
    }]; 


    // Start the long-running task 
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 

      while (1) { 
       sleep(4); 
       //NSLog(@"BGTime left: %f", [UIApplication sharedApplication].backgroundTimeRemaining); 

       if ([rootViewController isIncomingCall] && showedCall != TRUE) { 
        UILocalNotification *localNotif = [[UILocalNotification alloc] init]; 
        if (localNotif) { 
         localNotif.alertBody = [NSString stringWithFormat:@"Incoming Call."]; 
         localNotif.alertAction = NSLocalizedString(@"Accept Call", nil); 
         localNotif.soundName = @"alarmsound.caf"; 
         localNotif.applicationIconBadgeNumber = 1; 
         [application presentLocalNotificationNow:localNotif]; 
         [localNotif release]; 
        } 
        showedCall = TRUE; 
       } 
      } 
    });   
} 
- (void)backgroundHandler { 

    NSLog(@"### -->VOIP backgrounding callback"); 


    bgTask = [app beginBackgroundTaskWithExpirationHandler:^{ 
      [app endBackgroundTask:bgTask]; 
      bgTask = UIBackgroundTaskInvalid; 
    }]; 

    // Start the long-running task 
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 

      while (1) { 
       NSLog(@"BGTime left: %f", [UIApplication sharedApplication].backgroundTimeRemaining); 
       [rootViewController isIncomingCall]; 
       sleep(1); 
      } 
    }); 
} 
+0

歐文,與此代碼將您的應用程序繼續在後臺無限期運行? – Gruntcakes

+0

似乎,havnt完全測試它。我聽說有報道說在一臺設備上可能會在10分鐘後超時。我需要測試 –

+0

歐文,你測試過了嗎? – Cyprian