本地通知? [英] local notifications?

查看:87
本文介绍了本地通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序主要是一个真正没有连接到互联网的服务器的客户端。它连接到Polycom编解码器并管理2个端点之间的视频通话。所以我的应用程序可以发送命令像结束调用,音量增加等等...
但是我的问题是这个。我需要某种通知,当一个来电发生和应用程序不在前台。
由于服务器没有互联网访问APNS /推送通知不会为我工作。我已经调查过。这似乎让我的客户端运行,但是我不能做警告,因为我的应用程序是在后台。

My application is primarily a client for a server that really doesn't have a connection to the internet. It connects to a Polycom codec and manages the video calls between 2 endpoints. 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. 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:

我可以使用链接中列出的技术将我的应用程序带到前台(做类似下面的操作)。我可以从日志中看到,这段代码让我的代码运行。我知道我的while循环是不对的,最后我需要KVO,但无论如何不应该影响答案。 (我不明白的一件事是这是保持我的整个应用程序运行,而不是只是我在那里有类bcClient?)

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. (one thing I dont understand is this keeps my whole application running as opposed to just the class I have in there 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服务器)?

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)?

我有一个感觉这不是可能的,但我想我会问。

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

@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 applicationDidEnterBackground

AppDelegate.m (just didFinishLaunchingWithOptions and 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);
          }   
     });
}

这篇关于本地通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆