Skip to content

Commit

Permalink
feat(android): getNotificationSettings support (#237)
Browse files Browse the repository at this point in the history
  • Loading branch information
vincent-paing authored Mar 2, 2022
1 parent 864ca83 commit 9eec25d
Show file tree
Hide file tree
Showing 36 changed files with 2,340 additions and 1,981 deletions.
15 changes: 15 additions & 0 deletions android/src/main/java/app/notifee/core/Notifee.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import android.provider.Settings;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.app.NotificationManagerCompat;
import app.notifee.core.event.InitialNotificationEvent;
import app.notifee.core.event.MainComponentEvent;
import app.notifee.core.interfaces.MethodCallResult;
Expand Down Expand Up @@ -380,6 +381,20 @@ public void openPowerManagerSettings(Activity activity, MethodCallResult<Void> r
result.onComplete(null, null);
}

@KeepForSdk
public void getNotificationSettings(MethodCallResult<Bundle> result) {
boolean areNotificationsEnabled =
NotificationManagerCompat.from(ContextHolder.getApplicationContext())
.areNotificationsEnabled();
Bundle notificationSettingsBundle = new Bundle();
if (areNotificationsEnabled) {
notificationSettingsBundle.putInt("authorizationStatus", 1);
} else {
notificationSettingsBundle.putInt("authorizationStatus", 0);
}
result.onComplete(null, notificationSettingsBundle);
}

@KeepForSdk
public void openNotificationSettings(
@Nullable String channelId, Activity activity, MethodCallResult<Void> result) {
Expand Down
2 changes: 1 addition & 1 deletion docs/react-native/docs/android/interaction.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: Interaction
description: Handle the users interaction with your notifications.
next: /react-native/docs/android/progress-indicators
next: /react-native/docs/android/permissions
previous: /react-native/docs/android/grouping-and-sorting
---

Expand Down
1 change: 1 addition & 0 deletions docs/react-native/docs/android/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ The table below summarizes the various topics to read about to customize your no
| [Foreground Service](/react-native/docs/android/foreground-service) | Long running background tasks can take advantage of Android Foreground Services to display an on-going, prominent notification. |
| [Grouping & Sorting](/react-native/docs/android/grouping-and-sorting) | Group and sort related notifications in a single notification pane. |
| [Interaction](/react-native/docs/android/interaction) | Allow users to interact with your application directly from the notification with actions. |
| [Permissions](/react-native/docs/android/permissions) | Check whether notifications are enabled for your app, a channel group, or channel.
| [Progress Indicators](/react-native/docs/android/progress-indicators) | Show users a progress indicator of an on-going background task, and learn how to keep it updated. |
| [Styles](/react-native/docs/android/styles) | Style notifications to show richer content, such as expandable images/text, or message conversations. |
| [Timers](/react-native/docs/android/timers) | Display counting timers on your notification, useful for on-going tasks such as a phone call, or event time remaining. |
59 changes: 59 additions & 0 deletions docs/react-native/docs/android/permissions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
---
title: Permissions
description: Understanding and Checking Android notification permissions
next: /react-native/docs/android/progress-indicators
previous: /react-native/docs/android/interaction
---

# Understanding permissions

On Android, notification permissions are granted by default. However, a user may revoke them later through various means. In addition, a user may manage or revoke permissions at any of three levels; application-wide, channel groups and channels.

# Checking permissions

For each level, you can check whether notifications are permitted through a direct Notifee API call, or listen for changes with event listeners.

## Application wide

To check whether the user has enabled notifications for your app, call [`getNotificationSettings`](/reference/getnotificationsettings). The `authorizationStatus` attributes will returns `DENIED` if user has denied the permission, and `AUTHORIZED` if it's granted.


```js
import notifee from '@notifee/react-native';

async function checkNotificationPermission() {
const settings = await notifee.getNotificationSettings();

if (settings.authorizationStatus == AuthorizationStatus.AUTHORIZED) {
console.log('Notification permissions has been authorized');
} else if (settings.authorizationStatus == AuthorizationStatus.DENIED) {
console.log('Notification permissions has been denied');
}
}
```

If you determine notifications are disabled, you may want to offer the user the ability to open notification settings to enable it. The [`openNotificationSettings`](/reference/opennotificationsettings) API may be used to implement this feature.

## Channels & Channels Groups

To check whether the user has enabled notifications for specific channels or channel groups, call either [`getChannel`](/reference/getchannel) or [`getChannelGroups`](/reference/getchannelgroups). Both of these functions will return a respective object with `blocked` attributes in it.

```js
import notifee from '@notifee/react-native';

async function checkChannelPermission() {
const channel = await notifee.getChannel();

if (channel.blocked) {
console.log('Channel is disabled');
} else {
console.log('Channel is enabled');
}
}
```

> For more understanding on channels, you can refer to the [Channels & Groups](react-native/docs/android/channels) documentation.
## Permission events

For listening to permission change events, refer to the [Listening to channel events](react-native/docs/android/channels#listening-to-channel-events) documentation.
2 changes: 1 addition & 1 deletion docs/react-native/docs/android/progress-indicators.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
title: Progress Indicators
description: Use notifications to display progress on activities within your application.
next: /react-native/docs/android/styles
previous: /react-native/docs/android/interaction
previous: /react-native/docs/android/permissions
---

Notifications can display progress indicators to show users a status of an ongoing operation, for example, the progress
Expand Down
25 changes: 11 additions & 14 deletions docs/react-native/docs/ios/permissions.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ the users of your application full control of how notifications are handled:
The following example shows how to trigger a permission dialog:

```js
import notifee, { IOSAuthorizationStatus } from '@notifee/react-native';
import notifee, { AuthorizationStatus } from '@notifee/react-native';

async function requestUserPermission() {
const settings = await notifee.requestPermission();

if (settings.authorizationStatus >= IOSAuthorizationStatus.AUTHORIZED) {
if (settings.authorizationStatus >= AuthorizationStatus.AUTHORIZED) {
console.log('Permission settings:', settings);
} else {
console.log('User declined permissions');
Expand Down Expand Up @@ -81,30 +81,27 @@ the setting will be silently updated and the `requestPermission` will instantly
### Observing settings

In some cases, you may wish to observe what permission/settings have been granted on the device. The `requestPermission`
API used above resolves with an object value containing the current [`IOSNotificationSettings`](/reference/iosnotificationsettings).
API used above resolves with an object value containing the current [`NotificationSettings`](/reference/notificationsettings).
The settings contain information such as whether the user has specific settings enabled/disabled, and whether notification
permission is enabled/disabled for the entire application.

For example, to view whether the user has overall notification permission enabled/disabled:
permission is enabled/disabled for the entire application, for example:

```js
import notifee from '@notifee/react-native';

async function checkApplicationPermission() {
const settings = await notifee.requestPermission();

// settings only available on iOS
if (settings) {
if (settings.authorizationStatus) {
console.log('User has notification permissions enabled');
} else {
console.log('User has notification permissions disabled');
}
if (settings.authorizationStatus) {
console.log('User has notification permissions enabled');
} else {
console.log('User has notification permissions disabled');
}

console.log('iOS settings: ', settings.ios);
}
```

The value of each setting returns a [`IOSNotificationSettings`](/reference/iosnotificationsettings) value, which can be
The value of each setting in `settings.ios` returns a [`IOSNotificationSettings`](/reference/iosnotificationsettings) value, which can be
one of three values:

- `NOT_SUPPORTED`: The device either does not support the type of permission (the iOS API may be too low), or the permission has not been requested.
Expand Down
9 changes: 9 additions & 0 deletions docs/react-native/docs/release-notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@ next: /react-native/docs/usage
previous: /react-native/docs/installation
---

## 5.0.0
- **[Android/iOS] BREAKING CHANGE**: Added support for checking permissions on Android which introduces a breaking change to `requestPermission` and `getNotificationSettings` APIs.

Both APIs have been updated to return an object of type `NotificationSettings`, in replace of `NotificationSettingsIOS`.

`NotificationSettings` consists of two properties `authorizationStatus` and `ios`, where `ios` is a nested object of type `NotificationSettingsIOS`.

To learn more, see Permissions documentation for [iOS](https://notifee.app/react-native/docs/ios/permissions), and [Android](https://notifee.app/react-native/docs/android/permissions).

## 4.1.0
- **[Android]**: Add support to set `largeIcon` for `AndroidStyle.BIGPICTURE` to null (Fixes [#270](https://github.com/invertase/notifee/issues/270))
- **[Android]**: Fixes an issue with Android 12 when tapping on a notification (Fixes [#250](https://github.com/invertase/notifee/issues/250))
Expand Down
Loading

0 comments on commit 9eec25d

Please sign in to comment.