Shh! Mastering Do Not Disturb in Flutter: A Quirky Guide to Digital Zen

Natesh Bhat
3 min readOct 3, 2024

--

Picture this: You’re deep in the zone, coding the next big app that’s going to revolutionize… I don’t know, maybe cat videos? Suddenly, your phone erupts with notifications about your aunt’s latest farmville achievements. Poof! There goes your concentration, and your cat video empire crumbles before it even begins.

Fear not, fellow Flutter enthusiasts! Today, we’re diving into the world of Do Not Disturb (DND) mode, or as Android likes to call it, “Zen mode” (because nothing says inner peace like silencing your mother-in-law’s calls, right?).

Enter the do_not_disturb Plugin :

Allow me to introduce you to this new package authored by yours truly (that would be me 🥳) : The do_not_disturb Flutter plugin. This nifty little package allows you to check, and even control, the DND status on Android devices. iOS users, I’m afraid you’ll have to find inner peace the old-fashioned way — maybe try some actual yoga?

Getting Started: Installation and Setup

First things first, let’s add this bad boy to your `pubspec.yaml`:

dependencies:
do_not_disturb: <latest_version>

Reading the DND Status: Are We Zen Yet?

Let’s start with the basics — checking if DND is enabled:

import 'package:do_not_disturb/do_not_disturb.dart';
final dndPlugin = DoNotDisturbPlugin();
Future<void> checkDndStatus() async {
bool isDndEnabled = await dndPlugin.isDndEnabled();
print(isDndEnabled ? "Shh, we're in zen mode!" : "LOUD NOISES!");
}

But wait, there’s more! What if you want to know exactly how zen we are? The plugin’s got you covered with the `getDNDStatus` method:

Future<void> getDetailedDndStatus() async {
InterruptionFilter status = await dndPlugin.getDNDStatus();
switch (status) {
case InterruptionFilter.all:
print("All interruptions welcome. Chaos reigns!");
break;
case InterruptionFilter.priority:
print("VIPs only. Sorry, telemarketers!");
break;
case InterruptionFilter.none:
print("Maximum zen achieved. Not even your thoughts are allowed.");
break;
case InterruptionFilter.alarms:
print("Alarms only. Because oversleeping is the real disturbance.");
break;
default:
print("Unknown state. Did we enter the quantum realm?");
}
}

Taking Control: Becoming the DND Master

Now, for the power move — actually setting the DND mode. But remember, with great power comes great responsibility (and user permissions):

Future<void> embraceTheZen() async {
if (await dndPlugin.hasNotificationPolicyAccess()) {
await dndPlugin.setInterruptionFilter(InterruptionFilter.none);
print("Zen mode activated. You are now one with the void.");
} else {
print("We need the sacred permission to control the zen!");
await dndPlugin.openNotificationPolicyAccessSettings();
// Politely ask the user to grant permission and return to the app
}
}

But before you go off silencing the world, make sure your `AndroidManifest.xml` is properly dressed for the occasion:

<uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY"/>

The User Experience: A Journey to Digital Nirvana

When building your app, remember that controlling DND is a sacred power. Guide your users gently:

  1. Check if your app has the mystical notification policy access.
  2. If not, open the settings screen and ask nicely.
  3. Once granted, use your powers wisely.

Here’s a little function to lead your users on this spiritual journey:

Future<void> guideThroughDNDPermission() async {
bool hasAccess = await dndPlugin.hasNotificationPolicyAccess();
if (!hasAccess) {
// Show a dialog explaining why you need this power
bool userWantsToGrantAccess = await showExplanationDialog();

if (userWantsToGrantAccess) {
await dndPlugin.openNotificationPolicyAccessSettings();
// Wait for the user to return and check again
// You might want to use a stream or state management for this
} else {
print("User chose to remain in the noisy world. Their loss!");
}
} else {
print("We have the power! Use it wisely, young padawan.");
}
}

Conclusion: Embrace the Silence

And there you have it, folks! With the `do_not_disturb` plugin, you now have the power to check, control, and conquer the world of notifications. Use this power wisely — remember, with great silence comes great productivity (and possibly missed calls from your boss, but that’s a problem for future you).

So go forth, build amazing Flutter apps, and may the zen be with you! 🧘‍♂️📱✨

P.S. If you’re feeling particularly zen-like, why not [star the repository](flutter_do_not_disturb) or [buy the developer a coffee](https://www.buymeacoffee.com/nateshmbhat)? After all, good karma is the best notification of all!

--

--

Natesh Bhat
Natesh Bhat

Written by Natesh Bhat

An engineer by passion , developer by profession, singer by heart. You can follow me on https://in.linkedin.com/in/nateshmbhat

No responses yet