Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's "AutomaticZenRule" ? What is it used for?

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?

like image 989
android developer Avatar asked Oct 17 '17 09:10

android developer


People also ask

What is the purpose of the Notificationmanager class?

Class to notify the user of events that happen. This is how you tell the user that something has happened in the background.


2 Answers

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.

like image 174
Eckhart Wörner Avatar answered Sep 19 '22 20:09

Eckhart Wörner


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 :)

like image 40
Neji Avatar answered Sep 20 '22 20:09

Neji