DEV Community

Cover image for App Settings in .NET MAUI - Notifications
Victor Hugo Garcia
Victor Hugo Garcia

Posted on • Edited on

1

App Settings in .NET MAUI - Notifications

In this article, I'm going to show you how to validate if the user enabled or disabled the app notifications in .NET MAUI for Android and iOS.


Create a static class NotificationsHelper

public static bool AreDeviceNotificationsEnabled()
    {
#if ANDROID
        return AndroidX.Core.App.NotificationManagerCompat.From(Platform.CurrentActivity).AreNotificationsEnabled();
#elif IOS
        var settings = UIKit.UIApplication.SharedApplication.CurrentUserNotificationSettings.Types;
        return settings != UIKit.UIUserNotificationType.None;
#endif
    }
Enter fullscreen mode Exit fullscreen mode

Validate

It is a good practice to help the users by asking them if they want to open the app settings, so they can enable the notifications.


if (!AreDeviceNotificationsEnabled())
{
   if (showAlert)
   {
      var result = await Application.Current.MainPage.DisplayAlert("Enable Notifications", "Your notifications system are currently turned off", "Go to Settings", "Cancel");
      if (result)
      {
         AppInfo.ShowSettingsUI();
      }
   }
}    
Enter fullscreen mode Exit fullscreen mode

Conclusion

In .NET MAUI it is really cool how easy you can implement a static method that allows to execute code based on each platform. Of course if you prefer to avoid the declarative conditionals you can always use the .ios.cs and .android.cs file class naming convention.

Thanks for reading! Follow me on Twitter @ivictorhugo

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read more →

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more