local notifications?

前端 未结 1 920
旧时难觅i
旧时难觅i 2021-01-25 16:41

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 e

1条回答
  •  太阳男子
    2021-01-25 17:29

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

    - (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 讨论(0)
提交回复
热议问题