How to ask for notifications permission on Android using react-native 0.68? – Android

by
Ali Hasan
android android-permissions react-native

Quick Fix: In AndroidManifest.xml, add this line:

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

Create a function to request permission using this code:

import { Platform,PermissionsAndroid } from 'react-native';

const requestNotificationPermission = async () => {
  if(Platform.OS ==="android"){
    try {
      PermissionsAndroid.check('android.permission.POST_NOTIFICATIONS').then(
        response => {
          if(!response){
            PermissionsAndroid.request('android.permission.POST_NOTIFICATIONS',{
                title: 'Notification',
                message:
                  'App needs access to your notification ' +
                  'so you can get Updates',
                buttonNeutral: 'Ask Me Later',
                buttonNegative: 'Cancel',
                buttonPositive: 'OK',
            })
          }
        }
      ).catch(
        err => {
          console.log("Notification Error=====&gt;",err);
        }
      )
    } catch (err){
      console.log(err);
    }
  }
};

Call requestNotificationPermission() to request Android Notification permission.

The Problem:

A user with React Native 0.68.5 is unable to request notification permissions on Android due to version limitations. They seek a solution to prompt for these permissions on newer Android versions while maintaining compatibility with their current React Native version.

The Solutions:

Solution 1: Request Notification Permission

Step 1: Add Permission in AndroidManifest.xml

Add the以下行于 AndroidManifest.xml 中:

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

Step 2: Create a Permission Request Function

创建以下函数来请求权限:

import { Platform, PermissionsAndroid } from 'react-native';

const requestNotificationPermission = async () => {
  if (Platform.OS === "android") {
    try {
      const response = await PermissionsAndroid.check('android.permission.POST_NOTIFICATIONS');
      if (!response) {
        const result = await PermissionsAndroid.request('android.permission.POST_NOTIFICATIONS', {
          title: 'Notification',
          message:
            'App needs access to your notification ' +
            'so you can get Updates',
          buttonNeutral: 'Ask Me Later',
          buttonNegative: 'Cancel',
          buttonPositive: 'OK',
        });
      }
    } catch (err) {
      console.log("Notification Error===== >", err);
    }
  }
};

Step 3: Call the Permission Request Function

Call the requestNotificationPermission() function when you want to request permission for Android Notification permissions.

Q&A

How to ask for notifications permission on Android using react-native 0.68?

Add permission in AndroidManifest.xml and create a function to request the permission.

Video Explanation:

The following video, titled "Push Notification Using Firebase Cloud Messaging And React ...", provides additional insights and in-depth exploration related to the topics discussed in this post.

Play video

Go to channel · Push Notification In React Native Using Firebase - Android & IOS Tutorial 2023. code with adnan•9.8K views · 10:18. Go to ...