"Notification" blocking notification tips for specific apps

Source: Internet
Author: User

Notification prompts that need to be blocked by default for specific apps
Set whether the app receives notification interface
Click on each entry to enter the interface

Appnotificationsettings extends SettingspreferencefragmentPrivateswitchpreference mblock;//Entry via preference settingsMblock.setchecked (mapprow.banned); Mblock.setonpreferencechangelistener (NewOnpreferencechangelistener () {@Override     Public Boolean Onpreferencechange(Preference Preference, Object newvalue) {Final BooleanBlock = (Boolean) newvalue;returnmbackend.setnotificationsbanned (Pkg, UID, block);} });//Users cannot block notifications from System/signature packages    //using the tool class to determine if the app is a system package (such as a calculator), remove the settings notification Settings preference    if(Utils.issystempackage (PM, info))        {Getpreferencescreen (). Removepreference (mblock); Mpriority.setdependency (NULL);//don ' t has it depend on a preference that ' s gone}

Set by Mapprow by the current state of the block
Monitoring of the preference State, implemented through Mbackend
Import Com.android.settings.notification.NotificationAppList.AppRow;
Import Com.android.settings.notification.NotificationAppList.Backend;

View Notificationapplist.java

Initialize the properties in Approw public static Approw Loadapprow (Packagemanager pm, applicationinfo app, backend backend) {final Approw row = new Approw ();Row. Pkg= App. PackageName;Row. UID= App. UID;try {row. Label= App. Loadlabel(PM);} catch (Throwable t) {Log. E(TAG,"Error loading application label for"+ row. Pkg, T);Row. Label= row. Pkg;} row. Icon= App. LoadIcon(PM);Row. Banned= Backend. getnotificationsbanned(Row. Pkg, row. UID);//whether to prohibit notificationRow. Priority= Backend. Gethighpriority(Row. Pkg, row. UID);Row. Sensitive= Backend. Getsensitive(Row. Pkg, row. UID);Return row;}


View Inotificationmanager Interface
Find frameworks/-name "inotification*"
Frameworks/support/v4/java/android/support/v4/app/inotificationsidechannel.aidl
Frameworks/base/core/java/com/mediatek/common/mom/inotificationlistener.aidl
Frameworks/base/core/java/android/app/inotificationmanager.aidl
Frameworks/base/core/java/android/service/notification/inotificationlistener.aidl

. aidl file (interface Definition language for interprocess communication)
Frameworks/base/core/java/android/app/inotificationmanager.aidl
The service path implemented is
Frameworks\base\services\java\com\android\server\notificationmanagerservice.java

acquisition of the Accept notification attribute

Type of Mservice Iappopsservice
Frameworks/base/core/java/com/android/internal/app/iappopsservice.aidl
Frameworks/base/services/core/java/com/android/server/appopsservice.java

Boolean arenotificationsenabledforpackage (String pkg, int uid) returns False when the value returned is mode_ignored
Mservice.checkoperation (OP, uid, packagename) = mode_allowed is allowed to receive notifications;

property settings for accept notification
@Override        publicvoidsetNotificationsEnabledForPackageintboolean enabled) {            checkCallerIsSystem();            setNotificationsEnabledForPackageImpl(pkg, uid, enabled);        }


Path to Appopsmanager: Frameworks/base/core/java/android/app/appopsmanager.java

public static final int op_post_notification = 11;
UID = App.uid
pkg = App.packagename;
mode = enabled? AppOpsManager.MODE_ALLOWED:AppOpsManager.MODE_IGNORED

See which locations in your code call the Setnotificationsenabledforpackageimpl method

There are two other places to rewrite and implement the method.

Specific resolution steps

When you need to block all app notifications with no exceptions
In Appopsmanager, there are default values for many of the app's settings, compared to the 12th one is the default to the app's notification on or off, Appopsservice in the Checkoperation method is judged, when the OP is empty, Returns the default mode. So the original mode_allowed changed to Mode_ignored after the compilation frameworks/base after the push into the phone restart will find all apps are blocked notice no exception. When a specific app needs to turn on notifications, we can make changes in checkoperation.

   @Override     Public int checkoperation(intCodeintUID, String packagename) {verifyincominguid (UID); VERIFYINCOMINGOP (code);synchronized( This) {if(isoprestricted (UID, Code, PackageName)) {returnappopsmanager.mode_ignored; } op op = getoplocked (Appopsmanager.optoswitch (code), UID, PackageName,false);if(OP = =NULL) {returnAppopsmanager.optodefaultmode (code); }returnOp.mode; }}

In the checkoperation to judge the need to determine the code value, otherwise prone to click Restart Status, modified code as follows.

 Public int checkoperation(intCodeintUID, String packagename) {verifyincominguid (UID); VERIFYINCOMINGOP (code);synchronized( This) {//chenzilong Add for ZELY-41 block app notifications 20160331 start         if(Code = = appopsmanager.op_post_notification) {if((Packagename.equals ("Com.advan.advanstore")|| Packagename.equals ("Com.stkj.android.freeshare"))){returnappopsmanager.mode_allowed; }Else{returnappopsmanager.mode_ignored; }         }//Chenzilong add for ZELY-41 block app notifications 20160331 end            if(isoprestricted (UID, Code, PackageName)) {returnappopsmanager.mode_ignored; } op op = getoplocked (Appopsmanager.optoswitch (code), UID, PackageName,false);if(OP = =NULL) {returnAppopsmanager.optodefaultmode (code); }returnOp.mode; }


The result of the final implementation

"Notification" blocking notification tips for specific apps

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.