11package me .youchai .rnpush ;
22
33import android .app .NotificationManager ;
4+ import android .content .Context ;
45import android .content .Intent ;
6+ import android .net .ConnectivityManager ;
7+ import android .net .NetworkInfo ;
58import android .net .Uri ;
69import android .os .Build ;
710import android .provider .Settings ;
@@ -28,24 +31,23 @@ public class RNPushModule extends ReactContextBaseJavaModule {
2831 private PushService pushService = null ;
2932 private static ReactApplicationContext __rac ;
3033
31- enum EventType {
34+ enum EventType {
3235 REGISTER ("register" ),
3336 REGISTER_ERROR ("registrationError" ),
3437 NOTIFICATION ("notification" ),
3538 OPEN_NOTIFICATION ("openNotification" ),
3639 NOTIFICATION_AUTHORIZATION ("notificationAuthorization" );
3740
3841 public String value ;
39- private EventType (String value ){
42+
43+ private EventType (String value ) {
4044 this .value = value ;
4145 }
4246 }
4347
4448 public RNPushModule (ReactApplicationContext reactContext ) {
4549 super (reactContext );
4650 __rac = reactContext ;
47-
48-
4951 }
5052
5153 @ Override
@@ -74,22 +76,22 @@ public static void onRegisterError(String message) {
7476 }
7577
7678 public static void onNotification (Notification note ) {
77- Log .d (TAG ,"onNotification" );
79+ Log .d (TAG , "onNotification" );
7880 RNPushModule .sendEvent (EventType .NOTIFICATION .value , note .toWritableMap ());
7981 }
8082
8183 public static void onNotificationClick (Notification note ) {
82- Log .d (TAG ,"onNotificationClick" );
84+ Log .d (TAG , "onNotificationClick" );
8385// PushService.setInitialNotification(note);
8486 RNPushModule .sendEvent (EventType .OPEN_NOTIFICATION .value , note .toWritableMap ());
8587 }
8688
87- public static void onNotificationAuthorization (ReactApplicationContext reactContext ){
88- Log .d (TAG ,"onNotificationAuthorization" );
89+ public static void onNotificationAuthorization (ReactApplicationContext reactContext ) {
90+ Log .d (TAG , "onNotificationAuthorization" );
8991 boolean isOn = NotificationManagerCompat .from (reactContext ).areNotificationsEnabled ();
9092 WritableMap map = Arguments .createMap ();
91- map .putBoolean ("state" ,isOn );
92- RNPushModule .sendEvent (EventType .NOTIFICATION_AUTHORIZATION .value ,map );
93+ map .putBoolean ("state" , isOn );
94+ RNPushModule .sendEvent (EventType .NOTIFICATION_AUTHORIZATION .value , map );
9395 }
9496
9597 public static void sendEvent (String key , WritableMap event ) {
@@ -141,7 +143,7 @@ public void resume() {
141143 @ ReactMethod
142144 public void getRegistrationId (Promise promise ) {
143145 if (pushService == null ) {
144- promise .reject (TAG ,"pushService not initialized" );
146+ promise .reject (TAG , "pushService not initialized" );
145147 }
146148 try {
147149 WritableMap r = Arguments .createMap ();
@@ -168,7 +170,7 @@ public void getInitialNotification(Promise promise) {
168170 @ ReactMethod
169171 public void scheduleLocalNotification (ReadableMap args , Promise promise ) {
170172 if (pushService == null ) {
171- promise .reject (TAG ,"pushService not initialized" );
173+ promise .reject (TAG , "pushService not initialized" );
172174 }
173175 Notification note = new Notification ();
174176 if (args .hasKey ("title" )) {
@@ -196,7 +198,7 @@ public void scheduleLocalNotification(ReadableMap args, Promise promise) {
196198 @ ReactMethod
197199 public void cancelLocalNotifications (String id , Promise promise ) {
198200 if (pushService == null ) {
199- promise .reject (TAG ,"pushService not initialized" );
201+ promise .reject (TAG , "pushService not initialized" );
200202 }
201203 try {
202204 pushService .cancelLocalNotifications (Collections .singletonList (id ));
@@ -210,7 +212,7 @@ public void cancelLocalNotifications(String id, Promise promise) {
210212 @ ReactMethod
211213 public void cancelAllLocalNotifications (Promise promise ) {
212214 if (pushService == null ) {
213- promise .reject (TAG ,"pushService not initialized" );
215+ promise .reject (TAG , "pushService not initialized" );
214216 }
215217 try {
216218 pushService .cancelAllLocalNotifications ();
@@ -252,34 +254,51 @@ public void removeNotifications(String id, Promise promise) {
252254 }
253255
254256 @ ReactMethod
255- public void checkPermission (Promise promise ){
256- boolean state = NotificationManagerCompat .from (__rac ).areNotificationsEnabled ();
257+ public void checkPermission (Promise promise ) {
258+ boolean state = NotificationManagerCompat .from (__rac ).areNotificationsEnabled ();
257259 promise .resolve (state );
258260 }
259261
260262 @ ReactMethod
261- public void openSettingsForNotification (Promise promise ){
263+ public void checkNetwork (Promise promise ) {
264+ try {
265+ ConnectivityManager cm = (ConnectivityManager ) __rac .getSystemService (Context .CONNECTIVITY_SERVICE );
266+ NetworkInfo info = cm .getActiveNetworkInfo ();
267+ Log .i (TAG , "checkNetwork" + info .isConnected () + " " + info .isAvailable () + " " + info .getType () + " " + info .getState ());
268+ if (info != null ) {
269+ promise .resolve (info .isConnected ());
270+ } else {
271+ promise .resolve (false );
272+ }
273+ } catch (Exception e ) {
274+ Log .e (TAG , "checkNetwork" , e );
275+ promise .reject (e );
276+ }
277+ }
278+
279+ @ ReactMethod
280+ public void openSettingsForNotification (Promise promise ) {
262281 Intent intent = new Intent ();
263282 intent .addFlags (Intent .FLAG_ACTIVITY_NEW_TASK );
264283 String packageName = __rac .getPackageName ();
265284 int uid = __rac .getApplicationInfo ().uid ;
266- try {
267- if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .O ){
285+ try {
286+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .O ) {
268287 intent .setAction (Settings .ACTION_APP_NOTIFICATION_SETTINGS );
269- intent .putExtra (Settings .EXTRA_APP_PACKAGE ,packageName );
270- intent .putExtra (Settings .EXTRA_CHANNEL_ID ,uid );
271- } else if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .LOLLIPOP ){
288+ intent .putExtra (Settings .EXTRA_APP_PACKAGE , packageName );
289+ intent .putExtra (Settings .EXTRA_CHANNEL_ID , uid );
290+ } else if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .LOLLIPOP ) {
272291 intent .setAction (Settings .ACTION_APP_NOTIFICATION_SETTINGS );
273- intent .putExtra ("app_package" ,packageName );
274- intent .putExtra ("app_uid" ,uid );
292+ intent .putExtra ("app_package" , packageName );
293+ intent .putExtra ("app_uid" , uid );
275294 }
276295
277296 __rac .startActivity (intent );
278- }catch (Exception e ){
279- Log .e (TAG ,"gotoNotificationSetting" ,e );
297+ } catch (Exception e ) {
298+ Log .e (TAG , "gotoNotificationSetting" , e );
280299
281300 intent .setAction (Settings .ACTION_APPLICATION_DETAILS_SETTINGS );
282- intent .setData (Uri .fromParts ("package" ,packageName ,null ));
301+ intent .setData (Uri .fromParts ("package" , packageName , null ));
283302
284303 __rac .startActivity (intent );
285304 }
0 commit comments