Permissions needed for NotificationManager

安稳与你 提交于 2019-12-11 05:13:57

问题


I'm trying to set the Ringer to Silent and Do not Disturb to Priority Only using the following

AudioManager myAudioMgr = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
NotificationManager myNOtificationMgr = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

myAudioMgr.setRingerMode(AudioManager.RINGER_MODE_SILENT);
myNOtificationMgr.setInterruptionFilter(NotificationManager.INTERRUPTION_FILTER_PRIORITY);

I keep getting a security error

java.lang.RuntimeException: Unable to start receiver MyBroadcastReceiver: java.lang.SecurityException: Notification policy access denied

I've added the Access Notification Policy permission to my Manifest file

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

Am I missing an additional permission?


回答1:


It seems like the user needs to explicity grant the permission to app via the settings screen in order for the app to manipulate the priority/silent via the notifications api. I am assuming that you are using the notificationManager class for this. Some links that might help you:

https://developer.android.com/reference/android/provider/Settings.html#ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS

https://developer.android.com/reference/android/app/NotificationManager.html#ACTION_NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED

I think essentially what you need to do is direct the user to the "Show Do Not Disturb access settings" and have him enable the option for notification management (Something similar for what would you do for mock location for example)

Sample code:

startActivityForResult(new Intent(android.settings.NOTIFICATION_POLICY_ACCESS_SETTINGS), 0);

Hope this helps.




回答2:


From Android Developers:

Request policy access by sending the user to the activity that matches the system intent action ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS.

Use ACTION_NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED to listen for user grant or denial of this access.

So, to get permission you should request policy access by sending the user to the activity that matches the system intent action ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS and listen for response using BroadcastReceiver with action ACTION_NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED.

Also take a look at method isNotificationPolicyAccessGranted() from NotificationManager to check if permission granted.




回答3:


To correct the java.lang.SecurityException: Access denied to process: 3454 error, android.permission.SET_WALLPAPER permission must be added to the Android Manifest.

Add the following line in your AndroidManifest.xml:

uses-permission android:name="android.permission.SET_WALLPAPER" 



回答4:


You'll need to do something like

public class SomeFragment extends FragmentCompat {
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        int permissionNotifications = ContextCompat.checkSelfPermission(getActivity(), Manifest.permission. ACCESS_NOTIFICATION_POLICY);

        if (permissionNotifications != PackageManager.PERMISSION_GRANTED ) {
            ActivityCompat.requestPermissions(
                    getActivity(),
                    new String[] { Manifest.permission.ACCESS_NOTIFICATION_POLICY },
                    PERMISSION_REQUEST
            );
        }
    }
}

keep in mind I'm not able to run this code so it may require some TLC but it's a start



来源:https://stackoverflow.com/questions/41308512/permissions-needed-for-notificationmanager

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!