public override async void OnActivated(UIApplication uiApplication) { base.OnActivated(uiApplication); try { await SensusContext.Current.MainThreadSynchronizer.ExecuteThreadSafe(async() => { // request authorization to show notifications to the user for data/survey requests. bool notificationsAuthorized = false; // if notifications were previously authorized and configured, there's nothing more to do. UNNotificationSettings settings = await UNUserNotificationCenter.Current.GetNotificationSettingsAsync(); if (settings.BadgeSetting == UNNotificationSetting.Enabled && settings.SoundSetting == UNNotificationSetting.Enabled && settings.AlertSetting == UNNotificationSetting.Enabled) { notificationsAuthorized = true; } else { // request authorization for notifications. if the user previously denied authorization, this will simply return non-granted. Tuple <bool, NSError> grantedError = await UNUserNotificationCenter.Current.RequestAuthorizationAsync(UNAuthorizationOptions.Badge | UNAuthorizationOptions.Sound | UNAuthorizationOptions.Alert); notificationsAuthorized = grantedError.Item1; } // reset the badge number before starting. it appears that badge numbers from previous installations // and instantiations of the app hang around. if (notificationsAuthorized) { UIApplication.SharedApplication.ApplicationIconBadgeNumber = 0; } // ensure service helper is running. it is okay to call the following line multiple times, as repeats have no effect. // per apple guidelines, sensus will run without notifications being authorized above, but the user's ability to // participate will certainly be reduced, as they won't be made aware of probe requests, surveys, etc. await SensusServiceHelper.Get().StartAsync(); // update/run all callbacks await(SensusContext.Current.CallbackScheduler as iOSCallbackScheduler).UpdateCallbacksOnActivationAsync(); // disabling notifications will greatly impair the user's studies. let the user know. if (!notificationsAuthorized) { // warn the user and help them to enable notifications UIAlertView warning = new UIAlertView("Warning", "Notifications are disabled. Please enable notifications to participate fully in your studies. Tap the button below to do this now.", default(IUIAlertViewDelegate), "Close", "Open Notification Settings"); warning.Dismissed += async(sender, e) => { if (e.ButtonIndex == 1) { NSUrl notificationSettingsURL = new NSUrl(UIApplication.OpenSettingsUrlString.ToString()); await uiApplication.OpenUrlAsync(notificationSettingsURL, new UIApplicationOpenUrlOptions()); } }; warning.Show(); } #if UI_TESTING // load and run the UI testing protocol string filePath = NSBundle.MainBundle.PathForResource("UiTestingProtocol", "json"); using (Stream file = new FileStream(filePath, FileMode.Open, FileAccess.Read)) { await Protocol.RunUiTestingProtocolAsync(file); } #endif }); } catch (Exception ex) { SensusException.Report("Exception while activating: " + ex.Message, ex); } }