diff --git a/Example/Example.xcodeproj/project.pbxproj b/Example/Example.xcodeproj/project.pbxproj index ab5285770..f2224d80d 100644 --- a/Example/Example.xcodeproj/project.pbxproj +++ b/Example/Example.xcodeproj/project.pbxproj @@ -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; @@ -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; diff --git a/Example/Example/AppDelegate.swift b/Example/Example/AppDelegate.swift index 1cc7e2847..39c7046ff 100644 --- a/Example/Example/AppDelegate.swift +++ b/Example/Example/AppDelegate.swift @@ -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) diff --git a/Example/Example/Info.plist b/Example/Example/Info.plist index f02fdf6bd..8f246fbb2 100644 --- a/Example/Example/Info.plist +++ b/Example/Example/Info.plist @@ -2,6 +2,10 @@ + BGTaskSchedulerPermittedIdentifiers + + io.radar.notificationCheck + CFBundleDevelopmentRegion $(DEVELOPMENT_LANGUAGE) CFBundleExecutable @@ -18,6 +22,8 @@ $(MARKETING_VERSION) CFBundleVersion 1 + LSApplicationCategoryType + LSRequiresIPhoneOS NSAppTransportSecurity @@ -71,6 +77,7 @@ fetch location + processing UILaunchStoryboardName Launch Screen.storyboard diff --git a/RadarSDK/Radar.m b/RadarSDK/Radar.m index 6583142f3..d2077ec6e 100644 --- a/RadarSDK/Radar.m +++ b/RadarSDK/Radar.m @@ -46,6 +46,8 @@ + (void) nativeSetup { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ [RadarNotificationHelper swizzleNotificationCenterDelegate]; + [RadarNotificationHelper registerBackgroundNotificationChecks]; + [RadarNotificationHelper scheduleBackgroundNotificationChecks]; }); } diff --git a/RadarSDK/RadarNotificationHelper.h b/RadarSDK/RadarNotificationHelper.h index ff30aa436..89240fe18 100644 --- a/RadarSDK/RadarNotificationHelper.h +++ b/RadarSDK/RadarNotificationHelper.h @@ -26,6 +26,10 @@ typedef void (^NotificationPermissionCheckCompletion)(BOOL granted); + (void)logConversionWithNotificationResponse:(UNNotificationResponse *)response; ++ (void)registerBackgroundNotificationChecks; + ++ (void)scheduleBackgroundNotificationChecks; + @end NS_ASSUME_NONNULL_END diff --git a/RadarSDK/RadarNotificationHelper.m b/RadarSDK/RadarNotificationHelper.m index 1fc02ec68..d36e22051 100644 --- a/RadarSDK/RadarNotificationHelper.m +++ b/RadarSDK/RadarNotificationHelper.m @@ -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