What's "AutomaticZenRule" ? What is it used for?
Asked Answered
C

2

13

Background

I just noticed some functions of NotificationManager that handle a class that's called AutomaticZenRule :

https://developer.android.com/reference/android/app/NotificationManager.html#addAutomaticZenRule(android.app.AutomaticZenRule)

and others...

The problem

Looking at the docs of AutomaticZenRule, it still doesn't tell much about what it is, and what can it be used for:

Rule instance information for zen mode.

What I tried

Searching the Internet, I can see just in a Commonsware blog post, that they wonder what it is:

It is unclear what AutomaticZenRule is ...

There is practically nothing more that I've found about it. Not "zen mode" and not "AutomaticZenRule".

The questions

  1. What is "zen mode" ?

  2. What is "AutomaticZenRule" , and what can I do with it? How is it related to notifications?

  3. Is there anything special on Android N, that this API was added on this version?

  4. Is there a sample for using it?

Coaler answered 17/10, 2017 at 9:38 Comment(1)
Adding this as a comment as I don't have enough information to actually answer everything. But in the android.provider.Settings class there is a value that contains zen mode, hinting it may have something to do with do not disturb mode. Very vague, but it's at least something. (if the link dies for whatever reason, the important part is: Activity Action: Show Zen Mode (aka Do Not Disturb) priority configuration settings.)Copyholder
B
2

Zen Mode is just another name for Do Not Disturb (DND) mode. Android can activate DND mode based on rules. These rules can be provided either by the system, or by a third-party app.

In the following screenshot you can see two system-provided rules, together with a "Driving" rule provided by the third-party app "Pixel Ambient Services":

DND rules

AutomaticZenRule is there to integrate your own rules into the Android system. To integrate your own rules, you have to follow these rough steps:

  1. Make sure that you have sufficient permissions to access the DND policy (android.permission.ACCESS_NOTIFICATION_POLICY). See NotificationManager.isNotificationPolicyAccessGranted() for details.
  2. Add an activity for your rule:

     <activity android:name="MyRuleConfigurationActivity">
        <meta-data android:name="android.service.zen.automatic.ruleType" android:value="My Rule" />
        <intent-filter>
          <action android:name="android.app.action.AUTOMATIC_ZEN_RULE"/>
        </intent-filter>
     </activity>
    
  3. Android will show your activity whenever the user wants to create or edit a rule of the specified rule type. In the latter case, Android will supply the ID of the existing rule in NotificationManager#EXTRA_AUTOMATIC_RULE_ID. To propagate changes in your activity back to android, you need to construct an AutomaticZenRuleinstance and call NotificationManager.addAutomaticZenRule / updateAutomaticZenRule.

  4. After that, you can tell Android that the conditions for your rule are currently satisfied / not satisfied by calling NotificationManager.setAutomaticZenRuleState.

Boche answered 5/3, 2020 at 12:9 Comment(1)
Yes. Do you have any sample of it, to enable&disable it via code? Does it need a permission?Coaler
L
1

From digging in into the other documents available, i was able to understand ZenMode to some extent(although it can be my own version and not the correct one).

What my understanding is as follows -

Zen Mode is the Do not Disturb mode which now in latest updates can be enabled automatically which depends on factors such as late time of the day, etc. AutomaticZenrule can be used by applications who want their notifications to not be masked or suppressed when in do not disturb mode.

For this your application should make request to policy access by sending the user to the activity that matches the system intent action ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS.

If user has granted access to notification policy for your app, then you will be able to set a priority notification even in do not disturb mode. AutomaticZenrule thus plays a vital role to state the system that the application's notifications not be suppressed.

Although, i dont have a running sample code for it, i guess it should be on similar lines like the enabling device admin code or requesting a permission use case.

Thanks to you i got to read something new :)

Linden answered 17/10, 2017 at 10:13 Comment(3)
"Do not Disturb" changes the way notifications are created/shown? Using "AutomaticZenrule " changes it somehow? In which way?Coaler
Just a guess, it might show the notification silently, without vibration, etc. Something which will add the notification without any disturbance to user.Linden
Can you please create a sample? I have Android O, but I have no idea how to use this new API.Coaler

© 2022 - 2024 — McMap. All rights reserved.