private async void createNotification() { try { var center = UNUserNotificationCenter.Current; UNNotificationSettings settings = await center.GetNotificationSettingsAsync(); if (settings.AuthorizationStatus != UNAuthorizationStatus.Authorized) { return; } var content = new UNMutableNotificationContent() { ThreadIdentifier = GROUP_NAME, Title = "Javier Cantos", Body = "Mensaje de prueba", SummaryArgument = "Javier Cantos" }; var request = UNNotificationRequest.FromIdentifier( Guid.NewGuid().ToString(), content, null ); center.AddNotificationRequest(request, null); } catch (Exception ex) { Debug.WriteLine(ex.Message); } }
async partial void ScheduleUnthreadedNotification(UIButton sender) { var center = UNUserNotificationCenter.Current; UNNotificationSettings settings = await center.GetNotificationSettingsAsync(); if (settings.AuthorizationStatus != UNAuthorizationStatus.Authorized) { return; } string appointment = appointments[unthreadedMessagesSent % appointments.Length]; var content = new UNMutableNotificationContent() { Title = appointment, Body = "See you for your appointment today at 2:00pm!", SummaryArgument = appointment }; var request = UNNotificationRequest.FromIdentifier( Guid.NewGuid().ToString(), content, UNTimeIntervalNotificationTrigger.CreateTrigger(1, false) ); center.AddNotificationRequest(request, null); unthreadedMessagesSent += 1; }
async partial void ScheduleThreadedNotification(UIButton sender) { var center = UNUserNotificationCenter.Current; UNNotificationSettings settings = await center.GetNotificationSettingsAsync(); if (settings.AuthorizationStatus != UNAuthorizationStatus.Authorized) { return; } string author = friend; if (threadMessagesSent % 2 == 0) { author = "Me"; } string message = "Fine!"; if (threadMessagesSent < messages.Length) { message = messages[threadMessagesSent % messages.Length]; if (threadMessagesSent == 0) { message = String.Format(message, friend); } } var content = new UNMutableNotificationContent() { ThreadIdentifier = threadId, Title = author, Body = message, SummaryArgument = author }; var request = UNNotificationRequest.FromIdentifier( Guid.NewGuid().ToString(), content, UNTimeIntervalNotificationTrigger.CreateTrigger(1, false) ); center.AddNotificationRequest(request, null); threadMessagesSent += 1; }
public NotificationPermissionState AreNotificationsEnabled() { UNNotificationSettings settings = UNUserNotificationCenter.Current.GetNotificationSettingsAsync().Result; switch (settings.AuthorizationStatus) { case UNAuthorizationStatus.Authorized: return(NotificationPermissionState.Allowed); case UNAuthorizationStatus.Denied: return(NotificationPermissionState.Denied); case UNAuthorizationStatus.NotDetermined: case UNAuthorizationStatus.Provisional: return(NotificationPermissionState.Unknown); default: throw new IndexOutOfRangeException("Unrecognized Notification Permission state."); } }
async void UpdateUserInterface() { UNNotificationSettings settings = await UNUserNotificationCenter.Current.GetNotificationSettingsAsync(); BeginInvokeOnMainThread(() => { if (settings.AuthorizationStatus == UNAuthorizationStatus.Authorized || settings.AuthorizationStatus == UNAuthorizationStatus.Provisional) { RedNotificationsSwitch.Enabled = true; RedNotificationsSwitch.On = NSUserDefaults.StandardUserDefaults.BoolForKey(RedNotificationsEnabledKey); RedNotificationsSwitch.Hidden = false; RedNotificationsLabel.Hidden = false; GreenNotificationsSwitch.Enabled = true; GreenNotificationsSwitch.On = NSUserDefaults.StandardUserDefaults.BoolForKey(GreenNotificationsEnabledKey); GreenNotificationsSwitch.Hidden = false; GreenNotificationsLabel.Hidden = false; ManageNotificationsStatusLabel.Text = ""; } else { RedNotificationsSwitch.Enabled = false; RedNotificationsSwitch.On = false; RedNotificationsSwitch.Hidden = true; RedNotificationsLabel.Hidden = true; GreenNotificationsSwitch.Enabled = false; GreenNotificationsSwitch.On = false; GreenNotificationsSwitch.Hidden = true; GreenNotificationsLabel.Hidden = true; ManageNotificationsStatusLabel.Text = "Enable notifications in Settings app."; } }); }
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); } }
private void OnNotificationSettings(UNNotificationSettings settings) { alertsAllowed = (settings.AlertSetting == UNNotificationSetting.Enabled); swtAlertsAllowed.On = alertsAllowed; }