Android 13 (Tiramisu)’s new APIs and features are ready to be tried out by developers. With the release of Android 13, there have been several changes to push notification permissions. This is a critical update that all Android developers should be aware of and understand. In this article, we'll discuss the new push notification permissions in Android 33 and provide a sample code in Xamarin for implementation.
Why the Change in Push Notification Permission in Android 13?
The change in push notification permission in Android 13 is aimed at providing users with more control over their privacy and security. In the past, push notifications were automatically granted when an app was installed. This resulted in many users receiving unwanted notifications from apps that they never intended to use. With the new push notification permission, users can now choose to allow or deny push notifications from individual apps.
How the Push Notification Permission Works in Android 13
In Android 33, push notification permission is prompted when the user opens an app for the first time. The user will be asked if they want to allow or deny push notifications from the app. If the user chooses to allow notifications, they will receive push notifications from the app. If the user denies notifications, they will not receive any push notifications from the app.
What Does This Mean for Users?
For users, the new push notification permissions system means that they have more control over the data that apps can access. This is a positive step forward in terms of privacy and security, as it gives users more control over the information that is shared with app developers. It also means that users can choose to turn off push notifications for apps that they no longer use or no longer find relevant.
Pre-Requisites
- Visual Studio 2022 - We cannot use Visual Studio 2019, since Android 13 was only available in the release VS2022 and will not be available with VS2019 even though we use the same SDK for both IDEs.
- Target Framework should be Android 13
- The Target SDK version should be Android 13
Sample Code
Once the above requirements are fulfilled, then we need to include POST_NOTIFICATIONS permission in the android manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<!-- Add this permission: -->
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
</manifest>
Add the following code in the Xamarin Main Activity --> OnStart Method
protected override void OnStart() {
base.OnStart();
const int requestLocationId = 2023;
string[] notiPermission = {
Manifest.Permission.PostNotifications
};
if ((int) Build.VERSION.SdkInt < 33) return;
if (this.CheckSelfPermission(Manifest.Permission.PostNotifications) != Permission.Granted) {
this.RequestPermissions(notiPermission, requestLocationId);
}
}
To know more about the "Android 13 - Runtime Notification Permission", visit the official Android developer link by clicking here.