Android MediaPlayer notification API 33 not working correctly - android

Media Player notification for Android is working fine with All APIs except API 33.
when pressing play/pause button below error in log
application is not in focus nor is it a system service for user 0
noting that notification is included in manifest as below:
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
other info:
compileSdkVersion 33
Noting also that run time permission for post_notification is called but no use.
code as below:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
if (this.checkSelfPermission(Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.POST_NOTIFICATIONS}, PermissionNumber);
}
}
and permission is requested and granted, still mediaplayer buttons are not working.

on API 33, you have to request Notification Permissions at runtime

Related

Android 10: How to detect if the user accepted/rejected the permission CHANGE_WIFI_STATE from the Android system notification?

I'm using addNetworkSuggestions on Android 10 to auto connect to an existing Wifi access point using this doc reference.
I'm using the following code :
val wifiManager = context.getSystemService(Context.WIFI_SERVICE) as WifiManager;
val status = wifiManager.addNetworkSuggestions(suggestionsList);
if (status != WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS) {
    // do error handling here
}
// Optional (Wait for post connection broadcast to one of your suggestions)val intentFilter = IntentFilter(WifiManager.NETWORK_STATE_CHANGED_ACTION);
val broadcastReceiver = object : BroadcastReceiver() {
    override fun onReceive(context: Context, intent: Intent) {
        if (!intent.action.equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
            return;
}
// do post connect processing here
}
};
context.registerReceiver(broadcastReceiver, intentFilter);
I'm using an Android 10 device and I'm getting a notification inviting the user to allow or not the permission:
According to the doc, this is the correct behaviour on Android 10:
On Android 11 (API level 30) and higher, the user sees a dialog if the app is running in the foreground, and a notification if the app is running in the background.
On Android 10 (API level 29), the user sees a notification, regardless of whether the app is running in the foreground or the background.
If the permission is not yet accepted, I want to show some text inviting the user to accept the permission notification. But if the user has already accepted the permission notification, I would hide this text.
The question is: How to detect if the user accepted/rejected the permission in the Android system notification ?
I've tried the following:
I've checked the result of wifiManager.addNetworkSuggestions method but it returns STATUS_NETWORK_SUGGESTIONS_SUCCESS even though the permission was not granted yet.
val status = wifiManager.addNetworkSuggestions(suggestionsList);
if (status != WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS) {
// this is not triggered even though the user didn't accept the permission notification
}
ActivityCompat.checkSelfPermission
ActivityCompat.checkSelfPermission(this, Manifest.permission.CHANGE_WIFI_STATE)
But it always returns true
AppOpsManager.unsafeCheckOp
From this issue:
appOpsManager.unsafeCheckOp("android:change_wifi_state", Process.myUid(), applicationContext.packageName) == AppOpsManager.MODE_ALLOWED
It returns true even if the permission was not yet accepted/rejected. But it returns false when the user has rejected the permission.
This works only for the case when the user has refused the Android permission notification.
From Desmond's suggestions I've tried using requestPermissions on Manifest.permission.CHANGE_WIFI_STATE. I correctly receive the PackageManager.PERMISSION_GRANTED value. But I keep getting the notification permission.
I have the following permissions in the manifest:
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

changed targetSdkVersion to 33 from 30 and now notifications are not coming up

I updated targetSdkVersion from 30 to 33 and notifications popup is not shown when the app is installed on the device
when the targetSdkVersion is 30 and when I install the app, the following popup shows up and when I click allow I do get notifications
when the targetSdkVersion is 33, I donot get the following popup when the app is installed.
I looked at https://developer.android.com/develop/ui/views/notifications/notification-permission
and added <uses-permission android:name="android.permission.POST_NOTIFICATIONS"/> in manifest file.
still I do not get any prompt asking for permission for notifications.
I have code which checks if notification is enabled and it returns null cuz no prompt is shown.
private fun isNotificationsEnabled(): Boolean {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val manager = oApp.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
if (!manager.areNotificationsEnabled()) {
return false
}
val channels = manager.notificationChannels
for (channel in channels) {
if (channel.importance == NotificationManager.IMPORTANCE_NONE) {
return false
}
}
true
} else {
NotificationManagerCompat.from(oApp).areNotificationsEnabled()
}
}
How can i force prompt or what else I need to do so user get this prompt on first install
thanks in advance
R
As shown in the documentation here, the new POST_NOTIFICATIONS permission is classified as a dangerous permission, which means you must manually request the permission from the user at runtime. The process for requesting a dangerous permission from the user is explained in the documentation here. Best practices for dangerous permissions state that you should not request the permission until the point when the user requests the behavior that requires the permission.

Android background location request permissions not allowed help api 28 and lower

I want location permissions for android api 28 and below.. It takes foreground location permissions but not background permissions.. Also on android developers page it says "background location" permissions will be granted automatically when Foreground permissions are taken for below 28, but it does not "grant" "background location" permissions I couldn't solve this problem, can anyone help?
source:https://developer.android.com/training/location/permissions#background
details:
Blockquote
On Android 10 (API level 29) and higher, you must declare the ACCESS_BACKGROUND_LOCATION permission in your app's manifest in order to request background location access at runtime. On earlier versions of Android, when your app receives foreground location access, it automatically receives background location access as well.
Blockquote
//Create location request permissions
var requestLocationPermission=registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()){
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
if (it.getOrDefault(Manifest.permission.ACCESS_COARSE_LOCATION,false) &&
it.getOrDefault(Manifest.permission.ACCESS_FINE_LOCATION,false)){
println("Permissions : Granted")
if (ContextCompat.checkSelfPermission(requireContext(),Manifest.permission.ACCESS_BACKGROUND_LOCATION)==PackageManager.PERMISSION_GRANTED){
println("Background location permissions is granted..!")
}else{
println("Background location permissions is denied..!")
}
}else{
println("Permissions : Denied")
}
}
}
// Code 2
fun requestPermission(){
if (ContextCompat.checkSelfPermission(requireContext(),Manifest.permission.ACCESS_FINE_LOCATION)==PackageManager.PERMISSION_GRANTED &&
ContextCompat.checkSelfPermission(requireContext(),Manifest.permission.ACCESS_COARSE_LOCATION)==PackageManager.PERMISSION_GRANTED){
println("Permissions granted")
}else{
println("Permissions request")
requestLocationPermission.launch(arrayOf(Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION))
}
}

How do I request push notification permissions for android 13?

I've looked through this guide for android 13 push notifications
https://developer.android.com/about/versions/13/changes/notification-permission#user-choice
And I've looked at the guide for requesting permissions
https://developer.android.com/training/permissions/requesting#java
I've updated my compile and target to api 32.
Here is my code so far (in progress). Right now I'm just trying to get the notification prompt to show up.
if (Build.VERSION.SDK_INT >= 32) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_NOTIFICATION_POLICY) == PackageManager.PERMISSION_GRANTED)
return;
ActivityResultLauncher<String> launcher = registerForActivityResult(
new ActivityResultContracts.RequestPermission(), isGranted -> {
}
);
launcher.launch(Manifest.permission.POST_NOTIFICATIONS);
}
The problem I have is I get an error cannot find symbol variable POST_NOTIFICATIONS.
What is the proper manifest permission for push notifications?
Maybe I'm a bit late to the party, I know... But I hope this can help others at least. You need to use compileSdkVersion 33 in your gradle file at the Module level. Then you'll be allowed to use the POST_NOTIFICATIONS permission without any issue.
Android 13 (API level 33) and higher supports a runtime permission for sending non-exempt (including Foreground Services (FGS)) notifications from an app: POST_NOTIFICATIONS. This change helps users focus on the notifications that are most important to them.
reference here
So you need to add this permission to AndroidManifest.xml
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
You need to follow few steps, add post notifications permission in manifest
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
then in your controller as for run time permission like generally we ask:
if (Build.VERSION.SDK_INT >= 33) {
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.POST_NOTIFICATIONS},101);
}
else {
createChannel();
}
}
Then Need to handle permission result as usual
How to add run time permission for notification permission in android studio,
Apps targeting Android 13 will now need to request notification permission from the user before posting notifications,”
Behavior changes: Apps targeting Android 13 or higher
https://developer.android.com/about/versions/13/behavior-changes-13
Add on manifest: uses-permission android: name="android.permission.POST_NOTIFICATIONS
and
Add in MainActivity after onCreate:
if (ContextCompat.checkSelfPermission(this, POST_NOTIFICATIONS) == PackageManager.PERMISSION_DENIED) {
ActivityCompat.requestPermissions(this, new String[]{POST_NOTIFICATIONS}, 1);
}

Application that can get Location and runs on android 4.0.3+

For android versions less than 6 just adding these permission requests to the manifest works:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
But for android version 6 I need explicitly request permission too to get the location:
requestPermissions(new String[]{ Manifest.permission.ACCESS_FINE_LOCATION}, 1340);
The problem is that If I add the above code the App works only on android 6 and fails to run on lower versions as requestPermissions does not exist on lower versions. and If I remove it I won't be able to get the location on android 6.
What can I do that my app be able to get the location on android 4.0.3+
You can check the API level of the phone that is running your app like this:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//later than 4.x - so here you can run the code
//ideally you want to make this call in a helper method which you then annotate with the #TargetApi
requestPermissions(new String[]{ Manifest.permission.ACCESS_FINE_LOCATION}, 1340);
}
else{
//nothing to do in your case ?
}
You can also use #TargetApi(Build.VERSION_CODES.LOLLIPOP) in the methods where you want to make "Lollipop+" calls.

Categories

Resources