-Ionic Project Requesting For Notification Permission

1.2k Views Asked by At

After updating the minimumTargetSDK from 31 to 33 in the config.xml file, notifications are no longer being sent to the mobile device.

I have noticed that this is because of an update in how Android handles notification-related permissions. https://developer.android.com/develop/ui/views/notifications/notification-permission. With the new update, we are required to ask for permission from the users during run time of the application for android 13 and onwards.

I have attempted to get permission from the user by including the code below in the app.component.ts which runs upon starting up the application however the settings dialog is not appearing. If I were to change the requestPermission code from this.androidPermissions.requestPermission(this.androidPermissions.PERMISSION.POST_NOTIFICATIONS) to this.androidPermissions.requestPermission(this.androidPermissions.PERMISSION.CAMERA) however, the settings dialog for camera permission will appear. console.log(this.androidPermissions.PERMISSION); also doesn't show the notification permission (POST_NOTIFICATIONS)

Can someone tell me what am I missing?

app.component.ts

import { AndroidPermissions } from '@ionic-native/android-permissions'; // Import the AndroidPermissions module

constructor (private androidPermissions: AndroidPermissions){
if(this.device.platform == "iOS"){
    }
    else{
     this.androidPermissions.request permission(this.androidPermissions.PERMISSION.POST_NOTIFICATIONS)
              .then(result => {
                  if (result.hasPermission) {
                    console.log("PERMISSION GRANTED");
                      // Permission granted
                  } else {
                    console.log("PERMISSION DENIED");
                      // Permission denied
                  }
              })
              .catch(error => {
                console.log("ERRRRRRRROR");
                  console.error(error);
              });
    }
}

AndroidManifest.xml

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

There are 3 best solutions below

1
KoLivanas On

The issue you are facing might be because the permission you are trying to request (POST_NOTIFICATIONS) may not be correct or applicable. The Android platform itself does not have a specific permission request for notifications in the same way it has for, say, camera or location. Typical approach for managing notification permissions in Android doesn't involve asking for them directly through the Android permissions API, like you do for other permissions. Notifications are usually enabled by default, but the user has the option to disable them from the system settings. You can, however, guide users to the system settings page for your app where they can manually enable notifications.

You can achieve this by using the following code snippet to open the application settings page:

import { Platform } from '@ionic/angular';
import { InAppBrowser } from '@ionic-native/in-app- 
browser/ngx';

constructor(
private platform: Platform, 
private inAppBrowser: InAppBrowser
) {
if (this.platform.is('android')) {
  const packageName = 'your.package.name'; //app'spackage                                       
  this.inAppBrowser.create(`market://details? 
id=${packageName}`,'_system');
 }
 }

This will open the app settings, where the user can then manually enable notifications.

Also, please make sure you are using the correct Android permission in the AndroidManifest.xml. Typically, for notifications, you might include permissions for Internet access or foreground services, etc., but there isn't a specific "post notifications" permission in the standard Android SDK.

<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
1
TomMax On

Enter this before requesting permission:

this.androidPermissions.PERMISSION["POST_NOTIFICATIONS"]="android.permission.POST_NOTIFICATIONS";
0
Anand81 On

Use like this.

this.androidPermissions.requestPermission('android.permission.POST_NOTIFICATIONS').then (....)