2011-11-22 81 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