|
1 | 1 | package com.reactnativenotificationsutils; |
2 | 2 |
|
| 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 | + |
3 | 11 | import androidx.annotation.NonNull; |
| 12 | +import androidx.annotation.Nullable; |
4 | 13 |
|
5 | | -import com.facebook.react.bridge.Promise; |
6 | 14 | import com.facebook.react.bridge.ReactApplicationContext; |
7 | 15 | import com.facebook.react.bridge.ReactContextBaseJavaModule; |
8 | 16 | import com.facebook.react.bridge.ReactMethod; |
9 | 17 | import com.facebook.react.module.annotations.ReactModule; |
10 | 18 |
|
11 | 19 | @ReactModule(name = NotificationsUtilsModule.NAME) |
12 | 20 | 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 | + } |
14 | 26 |
|
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; |
17 | 40 | } |
18 | 41 |
|
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); |
23 | 53 | } |
24 | 54 |
|
| 55 | + intent.setFlags(FLAG_ACTIVITY_NEW_TASK); |
25 | 56 |
|
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 | + } |
32 | 59 |
|
33 | 60 | } |
0 commit comments