Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Experiment] ios background task #402

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Example/Example.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@
"@executable_path/Frameworks",
);
MARKETING_VERSION = 3.5.0;
PRODUCT_BUNDLE_IDENTIFIER = io.radar.example;
PRODUCT_BUNDLE_IDENTIFIER = io.radar.bgfetchexample;
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = "";
SWIFT_VERSION = 5.0;
Expand Down Expand Up @@ -439,7 +439,7 @@
"@executable_path/Frameworks",
);
MARKETING_VERSION = 3.5.0;
PRODUCT_BUNDLE_IDENTIFIER = io.radar.example;
PRODUCT_BUNDLE_IDENTIFIER = io.radar.bgfetchexample;
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = "";
SWIFT_VERSION = 5.0;
Expand Down
4 changes: 2 additions & 2 deletions Example/Example/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UIWindowSceneDelegate, UN
// Replace with a valid test publishable key
let radarInitializeOptions = RadarInitializeOptions()
// Uncomment to enable automatic setup for notification conversions
// radarInitializeOptions.autoSetupNotificationConversion = true
Radar.initialize(publishableKey: "prj_test_pk_0000000000000000000000000000000000000000", options: radarInitializeOptions )
radarInitializeOptions.autoLogNotificationConversions = true
Radar.initialize(publishableKey: "prj_test_pk_", options: radarInitializeOptions )
Radar.setUserId("testUserId")
Radar.setMetadata([ "foo": "bar" ])
Radar.setDelegate(self)
Expand Down
7 changes: 7 additions & 0 deletions Example/Example/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>BGTaskSchedulerPermittedIdentifiers</key>
<array>
<string>io.radar.notificationCheck</string>
</array>
<key>CFBundleDevelopmentRegion</key>
<string>$(DEVELOPMENT_LANGUAGE)</string>
<key>CFBundleExecutable</key>
Expand All @@ -18,6 +22,8 @@
<string>$(MARKETING_VERSION)</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>LSApplicationCategoryType</key>
<string></string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>NSAppTransportSecurity</key>
Expand Down Expand Up @@ -71,6 +77,7 @@
<array>
<string>fetch</string>
<string>location</string>
<string>processing</string>
</array>
<key>UILaunchStoryboardName</key>
<string>Launch Screen.storyboard</string>
Expand Down
2 changes: 2 additions & 0 deletions RadarSDK/Radar.m
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ + (void) nativeSetup {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
[RadarNotificationHelper swizzleNotificationCenterDelegate];
[RadarNotificationHelper registerBackgroundNotificationChecks];
[RadarNotificationHelper scheduleBackgroundNotificationChecks];
});
}

Expand Down
4 changes: 4 additions & 0 deletions RadarSDK/RadarNotificationHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ typedef void (^NotificationPermissionCheckCompletion)(BOOL granted);

+ (void)logConversionWithNotificationResponse:(UNNotificationResponse *)response;

+ (void)registerBackgroundNotificationChecks;

+ (void)scheduleBackgroundNotificationChecks;

@end

NS_ASSUME_NONNULL_END
34 changes: 34 additions & 0 deletions RadarSDK/RadarNotificationHelper.m
Original file line number Diff line number Diff line change
Expand Up @@ -196,4 +196,38 @@ + (void)checkNotificationPermissionsWithCompletionHandler:(NotificationPermissio

}

+ (void)registerBackgroundNotificationChecks {
if (@available(iOS 13.0, *)) {
[[BGTaskScheduler sharedScheduler] registerForTaskWithIdentifier:@"io.radar.notificationCheck" usingQueue:nil launchHandler:^(BGTask *task) {
[self handleAppRefreshTask:task];
}];
}
}

+ (void)scheduleBackgroundNotificationChecks {
if (@available(iOS 13.0, *)) {
BGAppRefreshTaskRequest *request = [[BGAppRefreshTaskRequest alloc] initWithIdentifier:@"io.radar.notificationCheck"];
request.earliestBeginDate = [NSDate dateWithTimeIntervalSinceNow:60*60];
NSError *error = nil;

[[BGTaskScheduler sharedScheduler] submitTaskRequest:request error:&error];
if (error) {
[[RadarLogger sharedInstance] logWithLevel:RadarLogLevelError message:[NSString stringWithFormat:@"Error scheduling app refresh task: %@", error]];
} else {
NSLog(@"scheduled bg task");
}
}
}


+ (void)handleAppRefreshTask:(BGTask *)task API_AVAILABLE(ios(13.0)){
[[RadarLogger sharedInstance] logWithLevel:RadarLogLevelInfo message:[NSString stringWithFormat:@"Performing background task of checking for notification sent"]];
[self scheduleBackgroundNotificationChecks];
[Radar logConversionWithName:@"background_job_firing" metadata:nil completionHandler:^(RadarStatus status, RadarEvent * _Nullable event) {
NSString *message = [NSString stringWithFormat:@"Conversion name = %@: status = %@; event = %@", @"app_refresh", [Radar stringForStatus:status], event];
[[RadarLogger sharedInstance] logWithLevel:RadarLogLevelInfo message:message];
}];
[task setTaskCompletedWithSuccess:YES];
}

@end
Loading