-
-
Save liamzebedee/67e1b2c53c6c5edcf8ec to your computer and use it in GitHub Desktop.
| // | |
| // BackgroundTask.h | |
| // tomtrack | |
| // | |
| // Created by Liam Edwards-Playne on 13/02/2016. | |
| // | |
| #import "RCTBridgeModule.h" | |
| @interface BackgroundTask : NSObject <RCTBridgeModule> | |
| @end |
| // | |
| // BackgroundTask.m | |
| // tomtrack | |
| // | |
| // Created by Liam Edwards-Playne on 13/02/2016. | |
| // | |
| #import <Foundation/Foundation.h> | |
| #import "BackgroundTask.h" | |
| #import "RCTUtils.h" | |
| /* | |
| Sample: | |
| BackgroundTask.beginBackgroundTask("gpsTracking", () => {}) | |
| */ | |
| @implementation BackgroundTask | |
| RCT_EXPORT_MODULE(); | |
| #pragma mark - Public API | |
| RCT_EXPORT_METHOD(beginBackgroundTask:(NSString *)taskName jsCallback:(RCTResponseSenderBlock)jsCallback) | |
| { | |
| UIApplication *application = RCTSharedApplication(); | |
| __block UIBackgroundTaskIdentifier bgTask = [application beginBackgroundTaskWithName:taskName expirationHandler:^{ | |
| // Clean up any unfinished task business by marking where you | |
| // stopped or ending the task outright. | |
| [application endBackgroundTask:bgTask]; | |
| bgTask = UIBackgroundTaskInvalid; | |
| }]; | |
| // Start the long-running task and return immediately. | |
| dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ | |
| printf("Running in the background\n"); | |
| // Call the JS code | |
| jsCallback(@[]); | |
| printf("Back from the JS\n"); | |
| [application endBackgroundTask:bgTask]; | |
| bgTask = UIBackgroundTaskInvalid; | |
| }); | |
| } | |
| @end |
| var BackgroundTask = require('react-native').NativeModules.BackgroundTask; | |
| BackgroundTask.beginTask("taskName", function(){}) |
Do you think this would allow me to maintain a socket connection in the background, while my screen is off? I have been trying to make a running app that communicates with a server with socket, http://stackoverflow.com/questions/35458549/how-to-run-socket-io-in-the-background-on-ios-in-a-react-native-app, but I can't find a way to keep the socket connection alive. I've even tried the react-native-background-geolocation, but ran into different problems there.
Hi, do you have any more information on how to use this? Is it suppose to be used as an request Animation frame? Thanks.
@easilyBaffled Hey, did you succeed in maintaining a socket connection in the background? I'm looking for a way to do that.
@easilyBaffled @arvindnrbt @puredazzle any update? did you manage to keep the socket connection alive?
Inspired by some work from these guys https://github.com/transistorsoft/react-native-background-geolocation/blob/c1cd452751ffa8a4985b2302933c24d2033b35be/RNBackgroundGeolocation/RNBackgroundGeolocation.m