Skip to content

Commit

Permalink
修复andorid网络判断错误问题
Browse files Browse the repository at this point in the history
  • Loading branch information
kaisavx committed Sep 10, 2021
1 parent c3ba322 commit 7296f93
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 28 deletions.
3 changes: 3 additions & 0 deletions android.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ const RNPushAndroid = {
checkPermission: async () => {
return RNPush.checkPermission()
},
checkNetwork:async()=>{
return RNPush.checkNetwork()
},
openSettingsForNotification: async () => { return RNPush.openSettingsForNotification() },
}

Expand Down
73 changes: 46 additions & 27 deletions android/src/main/java/me/youchai/rnpush/RNPushModule.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package me.youchai.rnpush;

import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;
Expand All @@ -28,24 +31,23 @@ public class RNPushModule extends ReactContextBaseJavaModule {
private PushService pushService = null;
private static ReactApplicationContext __rac;

enum EventType{
enum EventType {
REGISTER("register"),
REGISTER_ERROR("registrationError"),
NOTIFICATION("notification"),
OPEN_NOTIFICATION("openNotification"),
NOTIFICATION_AUTHORIZATION("notificationAuthorization");

public String value;
private EventType(String value){

private EventType(String value) {
this.value = value;
}
}

public RNPushModule(ReactApplicationContext reactContext) {
super(reactContext);
__rac = reactContext;


}

@Override
Expand Down Expand Up @@ -74,22 +76,22 @@ public static void onRegisterError(String message) {
}

public static void onNotification(Notification note) {
Log.d(TAG,"onNotification");
Log.d(TAG, "onNotification");
RNPushModule.sendEvent(EventType.NOTIFICATION.value, note.toWritableMap());
}

public static void onNotificationClick(Notification note) {
Log.d(TAG,"onNotificationClick");
Log.d(TAG, "onNotificationClick");
// PushService.setInitialNotification(note);
RNPushModule.sendEvent(EventType.OPEN_NOTIFICATION.value, note.toWritableMap());
}

public static void onNotificationAuthorization(ReactApplicationContext reactContext){
Log.d(TAG,"onNotificationAuthorization");
public static void onNotificationAuthorization(ReactApplicationContext reactContext) {
Log.d(TAG, "onNotificationAuthorization");
boolean isOn = NotificationManagerCompat.from(reactContext).areNotificationsEnabled();
WritableMap map = Arguments.createMap();
map.putBoolean("state",isOn);
RNPushModule.sendEvent(EventType.NOTIFICATION_AUTHORIZATION.value,map);
map.putBoolean("state", isOn);
RNPushModule.sendEvent(EventType.NOTIFICATION_AUTHORIZATION.value, map);
}

public static void sendEvent(String key, WritableMap event) {
Expand Down Expand Up @@ -141,7 +143,7 @@ public void resume() {
@ReactMethod
public void getRegistrationId(Promise promise) {
if (pushService == null) {
promise.reject(TAG,"pushService not initialized");
promise.reject(TAG, "pushService not initialized");
}
try {
WritableMap r = Arguments.createMap();
Expand All @@ -168,7 +170,7 @@ public void getInitialNotification(Promise promise) {
@ReactMethod
public void scheduleLocalNotification(ReadableMap args, Promise promise) {
if (pushService == null) {
promise.reject(TAG,"pushService not initialized");
promise.reject(TAG, "pushService not initialized");
}
Notification note = new Notification();
if (args.hasKey("title")) {
Expand Down Expand Up @@ -196,7 +198,7 @@ public void scheduleLocalNotification(ReadableMap args, Promise promise) {
@ReactMethod
public void cancelLocalNotifications(String id, Promise promise) {
if (pushService == null) {
promise.reject(TAG,"pushService not initialized");
promise.reject(TAG, "pushService not initialized");
}
try {
pushService.cancelLocalNotifications(Collections.singletonList(id));
Expand All @@ -210,7 +212,7 @@ public void cancelLocalNotifications(String id, Promise promise) {
@ReactMethod
public void cancelAllLocalNotifications(Promise promise) {
if (pushService == null) {
promise.reject(TAG,"pushService not initialized");
promise.reject(TAG, "pushService not initialized");
}
try {
pushService.cancelAllLocalNotifications();
Expand Down Expand Up @@ -252,34 +254,51 @@ public void removeNotifications(String id, Promise promise) {
}

@ReactMethod
public void checkPermission(Promise promise){
boolean state = NotificationManagerCompat.from(__rac).areNotificationsEnabled();
public void checkPermission(Promise promise) {
boolean state = NotificationManagerCompat.from(__rac).areNotificationsEnabled();
promise.resolve(state);
}

@ReactMethod
public void openSettingsForNotification(Promise promise){
public void checkNetwork(Promise promise) {
try {
ConnectivityManager cm = (ConnectivityManager) __rac.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = cm.getActiveNetworkInfo();
Log.i(TAG, "checkNetwork" + info.isConnected() + " " + info.isAvailable() + " " + info.getType() + " " + info.getState());
if (info != null) {
promise.resolve(info.isConnected());
} else {
promise.resolve(false);
}
} catch (Exception e) {
Log.e(TAG, "checkNetwork", e);
promise.reject(e);
}
}

@ReactMethod
public void openSettingsForNotification(Promise promise) {
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
String packageName = __rac.getPackageName();
int uid = __rac.getApplicationInfo().uid;
try{
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
intent.setAction(Settings.ACTION_APP_NOTIFICATION_SETTINGS);
intent.putExtra(Settings.EXTRA_APP_PACKAGE,packageName);
intent.putExtra(Settings.EXTRA_CHANNEL_ID,uid);
} else if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
intent.putExtra(Settings.EXTRA_APP_PACKAGE, packageName);
intent.putExtra(Settings.EXTRA_CHANNEL_ID, uid);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
intent.setAction(Settings.ACTION_APP_NOTIFICATION_SETTINGS);
intent.putExtra("app_package",packageName);
intent.putExtra("app_uid",uid);
intent.putExtra("app_package", packageName);
intent.putExtra("app_uid", uid);
}

__rac.startActivity(intent);
}catch (Exception e){
Log.e(TAG,"gotoNotificationSetting",e);
} catch (Exception e) {
Log.e(TAG, "gotoNotificationSetting", e);

intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
intent.setData(Uri.fromParts("package",packageName,null));
intent.setData(Uri.fromParts("package", packageName, null));

__rac.startActivity(intent);
}
Expand Down
3 changes: 3 additions & 0 deletions ios.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ const RNPush = {
const state = await RNCNotification.requestNotification()
return state > 1
},
checkNetwork:async()=>{
return false
},
openSettingsForNotification: async () => {
return RNCNotification.openSettingsForNotification()
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@yocdev/react-native-notification",
"version": "1.0.23",
"version": "1.0.24",
"author": "npm@youchai.me",
"license": "MIT",
"description": "react-native notification",
Expand Down

0 comments on commit 7296f93

Please sign in to comment.