Xamarin - Android 13 Notification Permission Prompt - xamarin

As we know, Android 13 is introducing runtime notification permission. Here's the Android developer documentation on this.
We already have a priming page where we show the notification permission prompt in iOS. We just need to do this for all users on Android 13.
After reading the documentation, i've added the following:
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
However I can't seem to find a way to prompt Android users. Has anyone had any success with this/can point me in the right direction?
Also, we've noticed all of our users who have upgraded to Android 13 have notifications turned off by default. Is there a way we can add a link to the notifications permission in the settings?

I am currently using this to achieve that, in your MainActivity put this
protected override void OnStart()
{
base.OnStart();
const int requestLocationId = 0;
string[] notiPermission =
{
Manifest.Permission.PostNotifications
};
if ((int)Build.VERSION.SdkInt < 33) return;
if (this.CheckSelfPermission(Manifest.Permission.PostNotifications) != Permission.Granted)
{
this.RequestPermissions(notiPermission, requestLocationId);
}
}

From document Notification runtime permission, we know that:
To request the new notification permission from your app, update your
app to target Android 13 and complete a similar process compared to
requesting other runtime permissions.
And from document POST_NOTIFICATIONS, we also find that :
POST_NOTIFICATIONS Added in API level 33
public static final String POST_NOTIFICATIONS Allows an app to post
notifications
Protection level: dangerous
Constant Value: "android.permission.POST_NOTIFICATIONS"
That is the Protection level of POST_NOTIFICATIONS is dangerous, so we need to
add Requesting Runtime Permissions.
For more information, you can check : Requesting Runtime Permissions in Android Marshmallow.
You can also check Permissions In Xamarin.Android here.
And there is a sample here:https://github.com/xamarin/monodroid-samples/tree/main/android-m/RuntimePermissions.

Related

How do I deal with a possible exception in a Xamarin Forms application deployed to iOS or Android?

I have a finished application which I would like to make available to run on the iOS and Android platforms.  I have tested the application as much as possible and it works without problem.  But I know there is always the chance that something might go wrong and I could get an exception.
My question is how can I deal with this or what should I do. What happens on the phone, if a Forms application is deployed and there is an exception.
Would appreciate any advice or even links as to how this is handled.
If an exception is thrown and not handled by your code, the app will stop working (i.e. crash).
In order to handle these crashes we are using MS AppCenter (the successor to HockeyApp/Xamarin AppInsights).
You'll have to create a project there (one for each platform), and add the NuGet package to your projects. Afterwards you can initialize it with
AppCenter.Start("ios={Your App Secret};android={Your App Secret}",
typeof(Crashes)); // you'll get the app secrets from appcenter.ms
Crashes will be logged to AppCenter now and you'll be informed whenever there is a new crash.
Please note that it's best practice (if not required by law), that you ask the user for consent before sending the crash report (see here). You are using the delegate Crashes.ShouldAwaitUserConfirmation for that matter. You could for example show an action sheet with Acr.UserDialogs
private bool AwaitUserConfirmation()
{
// you should of course use your own strings
UserDialogs.Instance.ActionSheet(
new ActionSheetConfig
{
Title = "Oopsie",
Message = "The app crashed. Send crash to developers.",
Options = new List<ActionSheetOption>
{
new ActionSheetOption("Sure", () => Crashes.NotifyUserConfirmation(UserConfirmation.Send)),
new ActionSheetOption("Yepp, and don't bug be again.", () => Crashes.NotifyUserConfirmation(UserConfirmation.AlwaysSend)),
new ActionSheetOption("Nope", () => Crashes.NotifyUserConfirmation(UserConfirmation.DontSend))
}
});
return true;
}

Nativescript Android 8.0 READ_CALL_LOG Failed

Using Nativescript 5.0.
Below piece of code works well in Android ver 6.0.1
But fails to get permission in Android 8.0
Permission seeking message box does not appear.
Difference observed in both phone's permission details is, in Android 8.0, additional permission is seen as "Dial through contacts" which can not be set manually.
Permissions.requestPermission(android.Manifest.permission.READ_CALL_LOG, "Needed for connectivity status").then(() => {
console.log("refresh - Permission granted!");
this.readCallLog();
alert("in refresh Permission granted");
}).catch(() => {
console.log("Permission is not granted (sadface)");
alert("in refresh Permission NOT granted");
});
Try including READ_PHONE_STATE permission.
But to be precise nothing was changed in Android 8, but Android 9 introduced breaking changes to read call log permission which now requires phone state permission too to work.
While with earlier Android versions you could just request permission READ_CALL_LOG the newer Android versions are introducing some changes in how you could request the user call logs. The idea is to provide more information to the user explaining why you need these permissions.
An explanatory strings values should be provided in the Andorid resources
<string name="permgroupdesc_calllog">read and write phone call log</string>
<string name="permgrouprequest_calllog">Allow <b>%1$s</b> to access your phone call logs?</string>
Also, you will need to ask for a number of permissions and not just READ_CALL_LOG:
CALL_LOG
READ_CALL_LOG
WRITE_CALL_LOG
ROCESS_OUTGOING_CALLS
Details on the above here

Xamarin Forms - How to open specific page after clicking on notification when the app is closed?

I'm actually working on a Xamarin Forms application that need push notifications. I use the Plugin.PushNotification plugin.
When the app is running in the foreground or is sleeping (OnSleep), I have no problem to open a specific page when I click on a notification that I receive. But I was wondering how can I do that when the app is closed. Thanks!
I finally found the answer by myself and I want to share it in case someone needs it.
Nota bene: according to the official documentation of the plugin, it's Xam.Plugin.PushNotification that is deprecated. I use the new version of this plugin, Plugin.PushNotification which uses FCM for Android and APS for iOS.
There is no significant differences to open a notif when the app is running, is sleeping or is closed. Just add the next callback method in the OnCreate method (MyProject.Droid > MainApplication > OnCreate) and FinishedLaunching method (MyProject.iOS > AppDelegate > FinishedLaunching):
CrossPushNotification.Current.OnNotificationOpened += (s, p) =>
{
// manage your notification here with p.Data
App.NotifManager.ManageNotif(p.Data);
};
Common part
App.xaml.cs
// Static fields
// *************************************
public static NotifManager NotifManager;
// Constructor
// *************************************
public App()
{
...
NotifManager = new NotifManager();
...
}
NotifManager.cs
public class NotifManager
{
// Methods
// *************************************
public void ManageNotif(IDictionary<string, object> data)
{
// 1) switch between the different data[key] you have in your project and parse the data you need
// 2) pass data to the view with a MessagingCenter or an event
}
}
Unfortunately there is no succinct answer for either platform. Generally speaking, you need to tell the OS what to do when it starts the app as a result of the push notification. On both platforms, you should also consider what API level you are targeting, otherwise it won't work or even crash the app.
On iOS, you will need to implement this method in AppDelegate appropriately: FinishedLaunching(UIApplication application, NSDictionary launchOptions). The launchOptions will have the payload from the push notification for you to determine what to do with it (e.g. what page to open). For more information on iOS, Xamarin's documentation is a good place to start.
Android has a more complicated topology in terms of more drastic differences between API levels, whether you are using GCM/FCM, as well as requiring more code components. However, to answer the question directly, you will need to handle this in OnCreate(Bundle savedInstanceState) of your main Activity. If you are using Firebase, the push notification payload is available in Intent.Extras. Again, Xamarin's documentation has a good walkthrough.
Finally, note that the Plugin.PushNotification library you are using has been deprecated. I suggest you either change your library and/or your implementation soon. Part of the reason that library has been deprecated is because Google has deprecated the underlying Google Cloud Messaging (GCM) service, which will be decommissioned on April 11, 2019.

Xamarin-System.UnauthorizedAccessException: Access to the path is denied

I'm trying to download a file and I'm getting System.UnauthorizedAccessException: Access to the path "/storage/emulated/0/Download/test.pdf" is denied. I have set required permission in Android Manifest file.
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Download Path:
Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDownloads)
If i use the below path as my download path i can able to download the file. But i cant able to share the PDF file to google drive, drop box or any other System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal)
I am using Xamarin.Forms v2.4.0.282 and Xamarin.Android.Support packages v25.4.0.2.
Note: The code was woking fine when use Xamarin.Forms version 2.3.5.256-pre2 and Xamarin.Android.Support packages v23.3.0
Please suggest your ideas to resolve the issue.
Thanks in Advance.
Depending on the version of Android you are using even with the permissions added in the manifest in 6.0 or up the user has to explicitly enable the permission when the app runs and on lower versions permission is asked during install. For example, on startup of the app I created a method to check if it is enabled and request permission if it's not.
private void CheckAppPermissions()
{
if ((int)Build.VERSION.SdkInt < 23)
{
return;
}
else
{
if (PackageManager.CheckPermission(Manifest.Permission.ReadExternalStorage, PackageName) != Permission.Granted
&& PackageManager.CheckPermission(Manifest.Permission.WriteExternalStorage, PackageName) != Permission.Granted)
{
var permissions = new string[] { Manifest.Permission.ReadExternalStorage, Manifest.Permission.WriteExternalStorage };
RequestPermissions(permissions, 1);
}
}
}
You can also use the support library to do this, which is simpler and allows you to not have to check the Android version. For more info check out google's documentation.
If targeting API 29+, you will get the error even if you request the permission and user grants it, because they changed how storage works.
The correct solution is to look how it should be done on API 29+ and do it.
But if you are like me, tired of Android making things more complicated every day, just add android:requestLegacyExternalStorage="true" to your manifest <application> tag and you are saved until you start targeting API 30.
Those of you who are facing this issue after your app is Targeting API29 or higher, please go to this link and check LandLu's Answer:
https://forums.xamarin.com/discussion/171039/saving-files-to-external-storage
Earlier I was accessing Folder path using
return Android.OS.Environment.ExternalStorageDirectory.AbsolutePath;
But using this line solved my problem:
return Android.App.Application.Context.GetExternalFilesDir("").AbsolutePath;
You need user's permission on run time even you have mentioned them in your manifest file if you are running Android api level 23 or greater. Check and if user has not yet granted granted READ_EXTERNAL_STORAGE & WRITE_EXTERNAL_STORAGE, use the bellow code;
var permissions = new string[] { Manifest.Permission.ReadExternalStorage, Manifest.Permission.WriteExternalStorage };
RequestPermissions(permissions, 77);
If i use the below path as my download path i can able to download the
file. But i cant able to share the PDF file to google drive, drop box
or any other
System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal)
You are storing files on app's private storage. All files saved to the internal storage are private to your application and other applications ( google drive, drop box or any other ) cannot access them (nor can the user). You can use any public folder for that purpose;
var finalPath = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDownloads);
replace
Android.OS.Environment.ExternalStorageDirectory.AbsolutePath;
with
Android.App.Application.Context.GetExternalFilesDir("").AbsolutePath;

Parse FB SDK Login Application Transport Security errors on device but not on simulator XCode 7.1 Swift IOS 9 FBSDK 4.7

Although I have seen similar questions posted here, I have not been able to resolve my issue via plist settings. Any other ideas? Here is where I am:
Adding FB Login to a simple IOS app using the Parse framework
All versions are up-to-date as of today. XCode 7.1, IOS 9, Parse latest, FB SDK latest (4.7)
I have added the specified IOS 9 settings for the FB SDK in my plist. I even combined the lists to include the extra setting required for FB SDK 4.7.
The mystery:
last night I could not get this to run on my simulator, but after
re-adding the plist settings and re-importing all the libraries, I
finally got the FB login screens to show up on the iphone 6 (has been
upgraded to IOS 9). I couldn't get it to run on the simulator but i
left it at that.
now today i tried it on the iphone again and i keep getting the ATS-looking errors.
On the iphone the error is:
Cannot Verify Server Identity. The identity of "m.facebook.com" cannot be verified by Safari. Review the certificate details to continue.
In the XCode console the error is:
FBSDKLog: WARNING: FBSDK secure network request failed. Please verify you have configured your app for Application Transport Security compatibility described at https://developers.facebook.com/docs/ios/ios9
It seems to me that the plist fix is allowing the FB Login to work in my simulator, but why wouldn't it also work on the iphone? Could this have anything to do with Parse? Below is my login code:
class LoginViewController: UIViewController, PFLogInViewControllerDelegate, PFSignUpViewControllerDelegate, UITextFieldDelegate {
#IBAction func FBLoginButtonPressed(sender: AnyObject) {
// Set permissions required from the facebook user account
var permissionsArray : [String]?
permissionsArray = ["user_about_me"]
var userBlock : PFUserResultBlock?
// Login PFUser using Facebook
PFFacebookUtils.logInInBackgroundWithReadPermissions(permissionsArray, block: userBlock)
PFFacebookUtils.logInInBackgroundWithReadPermissions(permissionsArray) {
(user: PFUser?, error: NSError?) -> Void in
if let user = user {
if user.isNew {
print("User signed up and logged in through Facebook!")
} else {
print("User logged in through Facebook!")
}
self.performSegueWithIdentifier(self.tableViewSegue, sender: nil)
} else {
print("Uh oh. The user cancelled the Facebook login.")
}
if let e = error {
print(error)
}
}
print(userBlock.debugDescription)
}
picture of plist
Okay, the mystery has been solved. Parents of teens may be able to relate.
It turns out that last night when the login was working on my teen's iphone, it was set to use mobile data ON PURPOSE to bypass the content filtering settings on our network! The reason I didn't realize this today was because the FB app seemed to be working on the iphone (upon later scrutiny I see that there was just some cached content), and my mac is set to bypass the content filtering. So this is why it worked on the simulator but not on the iphone (when the iphone was set to use wifi and not mobile data).
So the FB error was a red herring. I just wasn't looking down that path...

Resources