Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(android): getNotificationSettings support #237

Merged
merged 18 commits into from
Mar 2, 2022
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 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,18 @@ 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 notification has been enabled your app or channel.
helenaford marked this conversation as resolved.
Show resolved Hide resolved
| [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: Android device grants permission for notification by default
helenaford marked this conversation as resolved.
Show resolved Hide resolved
next: /react-native/docs/android/progress-indicators
previous: /react-native/docs/android/interaction
---

# Understanding permission
vincent-paing marked this conversation as resolved.
Show resolved Hide resolved

On Android, notification permission is granted by default. However, user can revoke this permission through your app's settings or the notification itself. In addition,user can block notifiation at three levels; application-wide, channel groups and channels.
helenaford marked this conversation as resolved.
Show resolved Hide resolved

# Checking permission
vincent-paing marked this conversation as resolved.
Show resolved Hide resolved

For each level, you can check whether notifcation permission is enabled on as-need basis or with event listeners.
helenaford marked this conversation as resolved.
Show resolved Hide resolved

## Application wide

To check whether the user has enabled application-wide notifications, call [`getNotificationSettings`](/reference/getnotificationsettings). The `authorizationStatus` attributes will returns `DENIED` if user has denied the permission, and `AUTHORIZED` if it's granted.
vincent-paing marked this conversation as resolved.
Show resolved Hide resolved


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

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

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

In some cases where notification is disabled, you might want to give an option for user to open notification settings to enable it. After you have setup your UI, you can use Notifee's [`openNotificationSettings`](/reference/opennotificationsettings).
helenaford marked this conversation as resolved.
Show resolved Hide resolved

## Channels & Channels Groups

To check whether the user has enabled notification 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.
helenaford marked this conversation as resolved.
Show resolved Hide resolved

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

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

if (channel.blocked) {
console.log('Channel notification has been authorized');
vincent-paing marked this conversation as resolved.
Show resolved Hide resolved
} else {
console.log('Channel notification has been denied');
vincent-paing marked this conversation as resolved.
Show resolved Hide resolved
}
}
```

For more understanding on channels, you can refer to [Channels & Groups](react-native/docs/android/channels).
helenaford marked this conversation as resolved.
Show resolved Hide resolved

## Permission events

For listening to permission change events, refer to [Listening to channel events](react-native/docs/android/channels#listening-to-channel-events).
helenaford marked this conversation as resolved.
Show resolved Hide resolved
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
4 changes: 2 additions & 2 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
Loading