Skip to content

Commit db6ec2e

Browse files
committed
feat(android): adding open app settings function
1 parent 5cbeab1 commit db6ec2e

File tree

1 file changed

+41
-14
lines changed

1 file changed

+41
-14
lines changed
Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,60 @@
11
package com.reactnativenotificationsutils;
22

3+
import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
4+
5+
import android.app.Activity;
6+
import android.content.Context;
7+
import android.content.Intent;
8+
import android.os.Build;
9+
import android.provider.Settings;
10+
311
import androidx.annotation.NonNull;
12+
import androidx.annotation.Nullable;
413

5-
import com.facebook.react.bridge.Promise;
614
import com.facebook.react.bridge.ReactApplicationContext;
715
import com.facebook.react.bridge.ReactContextBaseJavaModule;
816
import com.facebook.react.bridge.ReactMethod;
917
import com.facebook.react.module.annotations.ReactModule;
1018

1119
@ReactModule(name = NotificationsUtilsModule.NAME)
1220
public class NotificationsUtilsModule extends ReactContextBaseJavaModule {
13-
public static final String NAME = "NotificationsUtils";
21+
public static final String NAME = "NotificationsUtils";
22+
23+
public NotificationsUtilsModule(ReactApplicationContext reactContext) {
24+
super(reactContext);
25+
}
1426

15-
public NotificationsUtilsModule(ReactApplicationContext reactContext) {
16-
super(reactContext);
27+
@Override
28+
@NonNull
29+
public String getName() {
30+
return NAME;
31+
}
32+
33+
@ReactMethod
34+
public void openAppNotificationsSettings(String channelId) {
35+
final Activity activity = getCurrentActivity();
36+
final Context context = getReactApplicationContext().getApplicationContext();
37+
38+
if (activity == null) {
39+
return;
1740
}
1841

19-
@Override
20-
@NonNull
21-
public String getName() {
22-
return NAME;
42+
Intent intent;
43+
if (Build.VERSION.SDK_INT >= 26) {
44+
if (channelId != null) {
45+
intent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS);
46+
intent.putExtra(Settings.EXTRA_CHANNEL_ID, channelId);
47+
} else {
48+
intent = new Intent(Settings.ACTION_APP_NOTIFICATION_SETTINGS);
49+
}
50+
intent.putExtra(Settings.EXTRA_APP_PACKAGE, context.getPackageName());
51+
} else {
52+
intent = new Intent(Settings.ACTION_APPLICATION_SETTINGS);
2353
}
2454

55+
intent.setFlags(FLAG_ACTIVITY_NEW_TASK);
2556

26-
// Example method
27-
// See https://reactnative.dev/docs/native-modules-android
28-
@ReactMethod
29-
public void multiply(double a, double b, Promise promise) {
30-
promise.resolve(a * b);
31-
}
57+
activity.runOnUiThread(() -> context.startActivity(intent));
58+
}
3259

3360
}

0 commit comments

Comments
 (0)