Skip to content

Commit

Permalink
PR-697 changes (#699)
Browse files Browse the repository at this point in the history
* feat(android): runtime notifications for sdk-33

* Fix checkNotifications implementation

* Add requestNotifications support

* Perform the version check on TS side

* Update README

Co-authored-by: Artur Yorsh <artyorsh.me@gmail.com>

Fix merge conflicts
  • Loading branch information
zoontek committed Jul 14, 2022
1 parent 0a96049 commit 46840ab
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 29 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ Add all wanted permissions to your app `android/app/src/main/AndroidManifest.xml
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_MEDIA_LOCATION" />
<uses-permission android:name="android.permission.ACTIVITY_RECOGNITION" />
<uses-permission android:name="com.android.voicemail.permission.ADD_VOICEMAIL" />
<uses-permission android:name="android.permission.ANSWER_PHONE_CALLS" />
<uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE" />
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
Expand All @@ -176,6 +177,7 @@ Add all wanted permissions to your app `android/app/src/main/AndroidManifest.xml
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
<uses-permission android:name="android.permission.READ_CALENDAR" />
<uses-permission android:name="android.permission.READ_CALL_LOG" />
Expand All @@ -195,7 +197,6 @@ Add all wanted permissions to your app `android/app/src/main/AndroidManifest.xml
<uses-permission android:name="android.permission.WRITE_CALL_LOG" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.android.voicemail.permission.ADD_VOICEMAIL" />

<!---->

Expand Down Expand Up @@ -788,8 +789,8 @@ checkNotifications().then(({status, settings}) => {

Request notifications permission status and get notifications settings values.

You cannot request notifications permissions on Windows. Disabling or enabling notifications can only be done through the App Settings.
`requestNotifications` is iOS-only. To request notifications permissions on Android, use `request(PERMISSIONS.ANDROID.POST_NOTIFICATIONS)`
- You have to target at least SDK 33 to perform request on Android 13+. The permission is always granted for prior versions.
- You cannot request notifications permissions on Windows. Disabling / enabling them can only be done through the App Settings.

```ts
// only used on iOS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@ public String getName() {
return "CAMERA";
if (permission.equals("android.permission.GET_ACCOUNTS"))
return "GET_ACCOUNTS";
if (permission.equals("android.permission.PROCESS_OUTGOING_CALLS"))
return "PROCESS_OUTGOING_CALLS";
if (permission.equals("android.permission.POST_NOTIFICATIONS"))
return "POST_NOTIFICATIONS";
if (permission.equals("android.permission.PROCESS_OUTGOING_CALLS"))
return "PROCESS_OUTGOING_CALLS";
if (permission.equals("android.permission.READ_CALENDAR"))
return "READ_CALENDAR";
if (permission.equals("android.permission.READ_CALL_LOG"))
Expand Down Expand Up @@ -143,13 +143,16 @@ private boolean permissionExists(final String permission) {

@ReactMethod
public void checkNotifications(final Promise promise) {
if(Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) {
this.checkNotificationsCompat(promise);
return;
}
final boolean enabled = NotificationManagerCompat
.from(getReactApplicationContext()).areNotificationsEnabled();

final WritableMap output = Arguments.createMap();
final WritableMap settings = Arguments.createMap();

output.putString("status", enabled ? GRANTED : BLOCKED);
output.putMap("settings", settings);

String fieldName = this.getFieldName("android.permission.POST_NOTIFICATIONS");
this.checkPermission(fieldName, promise);
promise.resolve(output);
}

@ReactMethod
Expand Down Expand Up @@ -367,19 +370,6 @@ public boolean onRequestPermissionsResult(int requestCode, String[] permissions,
return mCallbacks.size() == 0;
}

private void checkNotificationsCompat(final Promise promise) {
final boolean enabled = NotificationManagerCompat
.from(getReactApplicationContext()).areNotificationsEnabled();

final WritableMap output = Arguments.createMap();
final WritableMap settings = Arguments.createMap();

output.putString("status", enabled ? GRANTED : BLOCKED);
output.putMap("settings", settings);

promise.resolve(output);
}

private PermissionAwareActivity getPermissionAwareActivity() {
Activity activity = getCurrentActivity();
if (activity == null) {
Expand Down
2 changes: 1 addition & 1 deletion example/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
<uses-permission android:name="android.permission.READ_CALENDAR" />
<uses-permission android:name="android.permission.READ_CALL_LOG" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
Expand Down
24 changes: 20 additions & 4 deletions src/methods.android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import {
openLimitedPhotoLibraryPicker,
requestLocationAccuracy,
} from './unsupportedPlatformMethods';
import {uniq} from './utils';
import {platformVersion, uniq} from './utils';

const TIRAMISU_VERSION_CODE = 33;

const NativeModule: {
checkPermission: (permission: Permission) => Promise<PermissionStatus>;
Expand Down Expand Up @@ -60,8 +62,22 @@ async function request(permission: Permission, rationale?: Rationale): Promise<P
return NativeModule.requestPermission(permission);
}

function checkNotifications(): Promise<NotificationsResponse> {
return NativeModule.checkNotifications();
async function checkNotifications(): Promise<NotificationsResponse> {
if (platformVersion < TIRAMISU_VERSION_CODE) {
return NativeModule.checkNotifications();
}

const status = await check('android.permission.POST_NOTIFICATIONS');
return {status, settings: {}};
}

async function requestNotifications(): Promise<NotificationsResponse> {
if (platformVersion < TIRAMISU_VERSION_CODE) {
return NativeModule.checkNotifications();
}

const status = await request('android.permission.POST_NOTIFICATIONS');
return {status, settings: {}};
}

function checkMultiple<P extends Permission[]>(
Expand All @@ -88,5 +104,5 @@ export const methods: Contract = {
request,
requestLocationAccuracy,
requestMultiple,
requestNotifications: checkNotifications,
requestNotifications,
};
5 changes: 5 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
import {Platform} from 'react-native';

export const platformVersion =
typeof Platform.Version === 'string' ? parseInt(Platform.Version, 10) : Platform.Version;

export function uniq<T>(array: T[]): T[] {
return array.filter((item, index) => item != null && array.indexOf(item) === index);
}

0 comments on commit 46840ab

Please sign in to comment.