Skip to content

Commit acfa9fb

Browse files
authored
Align checkNotifications response on Android (#829)
1 parent 7f9e774 commit acfa9fb

File tree

29 files changed

+2441
-1955
lines changed

29 files changed

+2441
-1955
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -772,6 +772,8 @@ request(PERMISSIONS.IOS.LOCATION_ALWAYS).then((result) => {
772772

773773
Check notifications permission status and get notifications settings values.
774774

775+
_⚠️  Android will never return `blocked` after a `checkNotifications`, you have to request the permission to get the info._
776+
775777
```ts
776778
type NotificationSettings = {
777779
// properties only available on iOS

android/src/main/java/com/zoontek/rnpermissions/RNPermissionsModule.java

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,18 +67,28 @@ private boolean isPermissionUnavailable(@NonNull final String permission) {
6767
}
6868
}
6969

70-
@ReactMethod
71-
public void checkNotifications(final Promise promise) {
70+
// Only used on Android < 13 (the POST_NOTIFICATIONS runtime permission isn't available)
71+
private WritableMap getLegacyNotificationsResponse(String disabledStatus) {
7272
final boolean enabled = NotificationManagerCompat
7373
.from(getReactApplicationContext()).areNotificationsEnabled();
7474

7575
final WritableMap output = Arguments.createMap();
7676
final WritableMap settings = Arguments.createMap();
7777

78-
output.putString("status", enabled ? GRANTED : BLOCKED);
78+
output.putString("status", enabled ? GRANTED : disabledStatus);
7979
output.putMap("settings", settings);
8080

81-
promise.resolve(output);
81+
return output;
82+
}
83+
84+
@ReactMethod
85+
public void checkNotifications(final Promise promise) {
86+
promise.resolve(getLegacyNotificationsResponse(DENIED));
87+
}
88+
89+
@Override
90+
public void requestNotifications(final ReadableArray options, final Promise promise) {
91+
promise.resolve(getLegacyNotificationsResponse(BLOCKED));
8292
}
8393

8494
@ReactMethod
@@ -304,11 +314,6 @@ public void requestLocationAccuracy(String purposeKey, Promise promise) {
304314
promise.reject("Permissions:requestLocationAccuracy", "requestLocationAccuracy is not supported on Android");
305315
}
306316

307-
@Override
308-
public void requestNotifications(ReadableArray options, Promise promise) {
309-
promise.reject("Permissions:requestNotifications", "requestNotifications is not supported on Android");
310-
}
311-
312317
@Override
313318
public void openPhotoPicker(Promise promise) {
314319
promise.reject("Permissions:openPhotoPicker", "openPhotoPicker is not supported on Android");

android/src/paper/java/com/zoontek/rnpermissions/NativePermissionsModuleSpec.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ public NativePermissionsModuleSpec(ReactApplicationContext reactContext) {
4242
@DoNotStrip
4343
public abstract void checkNotifications(Promise promise);
4444

45+
@ReactMethod
46+
@DoNotStrip
47+
public abstract void requestNotifications(ReadableArray options, Promise promise);
48+
4549
@ReactMethod
4650
@DoNotStrip
4751
public abstract void check(String permission, Promise promise);
@@ -70,10 +74,6 @@ public NativePermissionsModuleSpec(ReactApplicationContext reactContext) {
7074
@DoNotStrip
7175
public abstract void requestLocationAccuracy(String purposeKey, Promise promise);
7276

73-
@ReactMethod
74-
@DoNotStrip
75-
public abstract void requestNotifications(ReadableArray options, Promise promise);
76-
7777
@ReactMethod
7878
@DoNotStrip
7979
public abstract void openPhotoPicker(Promise promise);

example/android/app/build.gradle

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
apply plugin: "com.android.application"
2+
apply plugin: "org.jetbrains.kotlin.android"
23
apply plugin: "com.facebook.react"
34

45
/**
@@ -70,8 +71,8 @@ def jscFlavor = 'org.webkit:android-jsc:+'
7071

7172
android {
7273
ndkVersion rootProject.ext.ndkVersion
73-
74-
compileSdkVersion rootProject.ext.compileSdkVersion
74+
buildToolsVersion rootProject.ext.buildToolsVersion
75+
compileSdk rootProject.ext.compileSdkVersion
7576

7677
namespace "com.rnpermissionsexample"
7778
defaultConfig {
@@ -106,13 +107,8 @@ android {
106107
dependencies {
107108
// The version of react-native is set by the React Native Gradle Plugin
108109
implementation("com.facebook.react:react-android")
110+
implementation("com.facebook.react:flipper-integration")
109111

110-
debugImplementation("com.facebook.flipper:flipper:${FLIPPER_VERSION}")
111-
debugImplementation("com.facebook.flipper:flipper-network-plugin:${FLIPPER_VERSION}") {
112-
exclude group:'com.squareup.okhttp3', module:'okhttp'
113-
}
114-
115-
debugImplementation("com.facebook.flipper:flipper-fresco-plugin:${FLIPPER_VERSION}")
116112
if (hermesEnabled.toBoolean()) {
117113
implementation("com.facebook.react:hermes-android")
118114
} else {

example/android/app/src/debug/AndroidManifest.xml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,8 @@
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
33
xmlns:tools="http://schemas.android.com/tools">
44

5-
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
6-
75
<application
86
android:usesCleartextTraffic="true"
97
tools:targetApi="28"
10-
tools:ignore="GoogleAppIndexingWarning">
11-
<activity android:name="com.facebook.react.devsupport.DevSettingsActivity" android:exported="false" />
12-
</application>
8+
tools:ignore="GoogleAppIndexingWarning"/>
139
</manifest>

example/android/app/src/debug/java/com/rnpermissionsexample/ReactNativeFlipper.java

Lines changed: 0 additions & 75 deletions
This file was deleted.

example/android/app/src/main/java/com/rnpermissionsexample/MainActivity.java

Lines changed: 0 additions & 32 deletions
This file was deleted.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.rnpermissionsexample
2+
3+
import com.facebook.react.ReactActivity
4+
import com.facebook.react.ReactActivityDelegate
5+
import com.facebook.react.defaults.DefaultNewArchitectureEntryPoint.fabricEnabled
6+
import com.facebook.react.defaults.DefaultReactActivityDelegate
7+
8+
class MainActivity : ReactActivity() {
9+
10+
/**
11+
* Returns the name of the main component registered from JavaScript. This is used to schedule
12+
* rendering of the component.
13+
*/
14+
override fun getMainComponentName(): String = "RNPermissionsExample"
15+
16+
/**
17+
* Returns the instance of the [ReactActivityDelegate]. We use [DefaultReactActivityDelegate]
18+
* which allows you to enable New Architecture with a single boolean flags [fabricEnabled]
19+
*/
20+
override fun createReactActivityDelegate(): ReactActivityDelegate =
21+
DefaultReactActivityDelegate(this, mainComponentName, fabricEnabled)
22+
}

example/android/app/src/main/java/com/rnpermissionsexample/MainApplication.java

Lines changed: 0 additions & 62 deletions
This file was deleted.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.rnpermissionsexample
2+
3+
import android.app.Application
4+
import com.facebook.react.PackageList
5+
import com.facebook.react.ReactApplication
6+
import com.facebook.react.ReactHost
7+
import com.facebook.react.ReactNativeHost
8+
import com.facebook.react.ReactPackage
9+
import com.facebook.react.defaults.DefaultNewArchitectureEntryPoint.load
10+
import com.facebook.react.defaults.DefaultReactHost.getDefaultReactHost
11+
import com.facebook.react.defaults.DefaultReactNativeHost
12+
import com.facebook.react.flipper.ReactNativeFlipper
13+
import com.facebook.soloader.SoLoader
14+
15+
class MainApplication : Application(), ReactApplication {
16+
17+
override val reactNativeHost: ReactNativeHost =
18+
object : DefaultReactNativeHost(this) {
19+
override fun getPackages(): List<ReactPackage> {
20+
// Packages that cannot be autolinked yet can be added manually here, for example:
21+
// packages.add(new MyReactNativePackage());
22+
return PackageList(this).packages
23+
}
24+
25+
override fun getJSMainModuleName(): String = "index"
26+
27+
override fun getUseDeveloperSupport(): Boolean = BuildConfig.DEBUG
28+
29+
override val isNewArchEnabled: Boolean = BuildConfig.IS_NEW_ARCHITECTURE_ENABLED
30+
override val isHermesEnabled: Boolean = BuildConfig.IS_HERMES_ENABLED
31+
}
32+
33+
override val reactHost: ReactHost
34+
get() = getDefaultReactHost(this.applicationContext, reactNativeHost)
35+
36+
override fun onCreate() {
37+
super.onCreate()
38+
SoLoader.init(this, false)
39+
if (BuildConfig.IS_NEW_ARCHITECTURE_ENABLED) {
40+
// If you opted-in for the New Architecture, we load the native entry point for this app.
41+
load()
42+
}
43+
ReactNativeFlipper.initializeFlipper(this, reactNativeHost.reactInstanceManager)
44+
}
45+
}

0 commit comments

Comments
 (0)