[英]local notifications?
My application is primarily a client for a server that really doesn't have a connection to the internet. 我的应用程序主要是服务器的客户端,该服务器实际上没有与Internet的连接。 It connects to a Polycom codec and manages the video calls between 2 endpoints.
它连接到Polycom编解码器,并管理2个端点之间的视频通话。 So my application can send commands like end call, volume up, etc... However my problem is this.
所以我的应用程序可以发送诸如结束呼叫,调高音量等命令。但是我的问题是这个。 I need some kind of notification when an incoming call happens and the application is not in the foreground.
当发生来电且应用程序不在前台时,我需要某种通知。 Since the server does not have internet access APNS/push notifications will not work for me.
由于服务器无法访问互联网,因此APNS /推送通知对我不起作用。 I have looked into doing something like this .
我一直看着做这样的事情这样 。 Which does seem to keep my client running however I cannot do an alert since my application is in the background.
这似乎使我的客户端保持运行,但是由于我的应用程序在后台,所以我无法发出警报。
So besides the basics of how to fix my problem my questions are: 因此,除了解决问题的基础知识外,我的问题还有:
Can I bring my application to the foreground using the technique listed in the link (doing something like what I'm doing below). 我可以使用链接中列出的技术将我的应用程序置于前台(做下面类似的事情)。 I can see from the logs that this code keeps my code running.
从日志中可以看到,此代码使代码保持运行状态。 I know my while loop is not right and in the end I would need KVO but regardless that shouldn't effect the answer.
我知道我的while循环不正确,最后我将需要KVO,但是无论如何都不会影响答案。 (one thing I dont understand is this keeps my whole application running as opposed to just the class I have in there bcClient?)
(我不明白的一件事是,这可以使我的整个应用程序运行,而不仅仅是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");
}
}
});
}
If I cannot bring my application to the foreground then is there anyways to push a notification locally (without the need for a APNS server)? 如果我无法将我的应用程序置于前台,那么是否仍然可以在本地推送通知(无需APNS服务器)?
I have a feeling none of this is possible but I figured I would ask. 我觉得这不可能,但是我想问一下。
Here is my answer. 这是我的答案。 This keeps my client application running in the background and shows a notification when a call comes in.
这样可以使我的客户端应用程序在后台运行,并在来电时显示通知。
AppDelegate.h 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 (just didFinishLaunchingWithOptions
and applicationDidEnterBackground
) AppDelegate.m(只是
didFinishLaunchingWithOptions
和applicationDidEnterBackground
)
- (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);
}
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.