|
|
@@ -9,6 +9,7 @@ import android.app.NotificationManager;
|
|
|
import android.app.PendingIntent;
|
|
|
import android.content.Context;
|
|
|
import android.content.Intent;
|
|
|
+import android.location.LocationManager;
|
|
|
import android.net.Uri;
|
|
|
import android.os.Build;
|
|
|
import android.os.SystemClock;
|
|
|
@@ -19,6 +20,7 @@ import androidx.annotation.NonNull;
|
|
|
import androidx.annotation.Nullable;
|
|
|
|
|
|
import com.facebook.react.bridge.Callback;
|
|
|
+import com.facebook.react.bridge.Promise;
|
|
|
import com.facebook.react.bridge.ReactApplicationContext;
|
|
|
import com.facebook.react.bridge.ReactContextBaseJavaModule;
|
|
|
import com.facebook.react.bridge.ReactMethod;
|
|
|
@@ -35,7 +37,7 @@ import java.util.List;
|
|
|
public class HolaModule extends ReactContextBaseJavaModule {
|
|
|
private static final String CHANNEL_ID = "local_notification_channel";
|
|
|
private static final String CHANNEL_NAME = "Daily Reminder";
|
|
|
- private static final String CHANNEL_DESCRIPTION = "Daily reminder notification at 9:00 AM";
|
|
|
+ private static final String CHANNEL_DESCRIPTION = "Daily reminder notification ";
|
|
|
|
|
|
|
|
|
public HolaModule(@Nullable ReactApplicationContext reactContext) {
|
|
|
@@ -73,8 +75,49 @@ public class HolaModule extends ReactContextBaseJavaModule {
|
|
|
currentActivity.startActivity(intent);
|
|
|
}
|
|
|
|
|
|
+ @ReactMethod
|
|
|
+ public void openSystemLocationSettings() {
|
|
|
+ Activity currentActivity = getCurrentActivity();
|
|
|
+ Context context = getReactApplicationContext();
|
|
|
+ Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
|
|
|
+// Uri uri = Uri.fromParts("package", context.getPackageName(), null);
|
|
|
+// intent.setData(uri);
|
|
|
+ currentActivity.startActivity(intent);
|
|
|
+ }
|
|
|
+
|
|
|
+ @ReactMethod
|
|
|
+ public void checkSystemLocationService(Promise promise){
|
|
|
+ Context context = getReactApplicationContext();
|
|
|
+ LocationManager lm = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
|
|
|
+ boolean gps_enabled = false;
|
|
|
+ boolean network_enabled = false;
|
|
|
+
|
|
|
+ if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.P) {
|
|
|
+ promise.resolve(lm.isLocationEnabled());
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ try {
|
|
|
+ gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
|
|
|
+ } catch(Exception ex) {}
|
|
|
+
|
|
|
+ try {
|
|
|
+ network_enabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
|
|
|
+ } catch(Exception ex) {}
|
|
|
+
|
|
|
+ if (gps_enabled||network_enabled){
|
|
|
+ promise.resolve(true);
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ promise.resolve(false);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
@ReactMethod
|
|
|
public void addLocalPush(String jsonData){
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
if (getCurrentActivity()==null){
|
|
|
return;
|
|
|
}
|
|
|
@@ -88,11 +131,11 @@ public class HolaModule extends ReactContextBaseJavaModule {
|
|
|
String strId = jsonObject.getString("id");
|
|
|
String categoryId = jsonObject.getString("category_id");
|
|
|
|
|
|
- createNotificationChannel(getCurrentActivity(),strId);
|
|
|
+ createNotificationChannel(getCurrentActivity(),categoryId);
|
|
|
Intent intent = new Intent(getCurrentActivity(), AlarmReceiver.class);
|
|
|
intent.putExtra("title", title);
|
|
|
intent.putExtra("message", body);
|
|
|
- intent.putExtra("channel", CHANNEL_ID);
|
|
|
+ intent.putExtra("channel", categoryId);
|
|
|
intent.putExtra("id",strId);
|
|
|
intent.putExtra("categoryId",categoryId);
|
|
|
intent.putExtra("msg_id",i);
|
|
|
@@ -102,14 +145,20 @@ public class HolaModule extends ReactContextBaseJavaModule {
|
|
|
getCurrentActivity(), i, intent, android.app.PendingIntent.FLAG_UPDATE_CURRENT);
|
|
|
|
|
|
AlarmManager alarmManager = (AlarmManager) getCurrentActivity().getSystemService(Context.ALARM_SERVICE);
|
|
|
+
|
|
|
+ AlarmManager.AlarmClockInfo info = alarmManager.getNextAlarmClock();
|
|
|
+ if (info!=null){
|
|
|
+ long t = info.getTriggerTime();
|
|
|
+ PendingIntent pt = info.getShowIntent();
|
|
|
+ String aa = info.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
if (mode.equals("ONCE")){
|
|
|
long leftMillis = toNearest1000(jsonObject.getLong("once")-System.currentTimeMillis());
|
|
|
if (leftMillis>0){
|
|
|
alarmManager.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime()+leftMillis,pendingIntent);
|
|
|
-// alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime()+leftMillis,pendingIntent);
|
|
|
}
|
|
|
-
|
|
|
-// alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime()+10*1000,pendingIntent);
|
|
|
}
|
|
|
else {
|
|
|
JSONObject recurring = jsonObject.getJSONObject("recurring");
|
|
|
@@ -124,32 +173,11 @@ public class HolaModule extends ReactContextBaseJavaModule {
|
|
|
// 如果当前时间已经超过 设定时间,则设置为明天
|
|
|
if (calendar.getTimeInMillis() <= System.currentTimeMillis()) {
|
|
|
calendar.add(Calendar.DAY_OF_YEAR, 1);
|
|
|
-// alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
|
|
|
-// AlarmManager.INTERVAL_DAY, pendingIntent);
|
|
|
- }
|
|
|
- else {
|
|
|
-// Calendar now = Calendar.getInstance();
|
|
|
-// int currentHour = now.get(Calendar.HOUR_OF_DAY);
|
|
|
-// int currentMinute = now.get(Calendar.MINUTE);
|
|
|
-// int currentSecond = now.get(Calendar.SECOND);
|
|
|
-//
|
|
|
-// // 计算目标时间的秒数
|
|
|
-// int targetSeconds = Integer.parseInt(parts[0]) * 3600 + Integer.parseInt(parts[1]) * 60 + Integer.parseInt(parts[2]);
|
|
|
-//
|
|
|
-// // 计算当前时间的秒数
|
|
|
-// int currentSeconds = currentHour * 3600 + currentMinute * 60 + currentSecond;
|
|
|
-//
|
|
|
-// // 计算距离目标时间的秒数
|
|
|
-// int secondsUntilTime = targetSeconds - currentSeconds;
|
|
|
-//
|
|
|
-// alarmManager.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime()+secondsUntilTime*1000,pendingIntent);
|
|
|
-//
|
|
|
-// calendar.add(Calendar.DAY_OF_YEAR, 1);
|
|
|
-// alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
|
|
|
-// AlarmManager.INTERVAL_DAY, pendingIntent);
|
|
|
}
|
|
|
- alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
|
|
|
- AlarmManager.INTERVAL_DAY, pendingIntent);
|
|
|
+
|
|
|
+ alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
|
|
|
+ AlarmManager.INTERVAL_DAY, pendingIntent);
|
|
|
+ Log.i("set alarm status:",time);
|
|
|
|
|
|
}
|
|
|
|
|
|
@@ -159,53 +187,86 @@ public class HolaModule extends ReactContextBaseJavaModule {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public static long toNearest1000(long number) {
|
|
|
- return ((number + 500) / 1000) * 1000;
|
|
|
- }
|
|
|
-
|
|
|
@ReactMethod
|
|
|
- public void rnPageLoaded(){
|
|
|
- GlobalVariable.rnLoaded = true;
|
|
|
- }
|
|
|
-
|
|
|
- @ReactMethod
|
|
|
- public void addLocalPush2(String title,String content,String time){
|
|
|
+ public void addLocalPush2(String jsonData){
|
|
|
if (getCurrentActivity()==null){
|
|
|
return;
|
|
|
}
|
|
|
- String[] parts = time.split(":");
|
|
|
+ try {
|
|
|
+ JSONArray jsonArray = new JSONArray(jsonData);
|
|
|
+ for (int i = 0; i < jsonArray.length(); i++) {
|
|
|
+ JSONObject jsonObject = jsonArray.getJSONObject(i);
|
|
|
+ String mode = jsonObject.getString("mode");
|
|
|
+ String title = jsonObject.getString("title");
|
|
|
+ String body = jsonObject.getString("body");
|
|
|
+ String strId = jsonObject.getString("id");
|
|
|
+ String categoryId = jsonObject.getString("category_id");
|
|
|
|
|
|
- createNotificationChannel(getCurrentActivity(),"111");
|
|
|
- Intent intent = new Intent(getCurrentActivity(), AlarmReceiver.class);
|
|
|
- intent.putExtra("title", title);
|
|
|
- intent.putExtra("message", content);
|
|
|
- intent.putExtra("channel", CHANNEL_ID);
|
|
|
+ createNotificationChannel(getCurrentActivity(),categoryId);
|
|
|
+ Intent intent = new Intent(getCurrentActivity(), AlarmReceiver.class);
|
|
|
+ intent.putExtra("title", title);
|
|
|
+ intent.putExtra("message", body);
|
|
|
+ intent.putExtra("channel", categoryId);
|
|
|
+ intent.putExtra("id",strId);
|
|
|
+ intent.putExtra("categoryId",categoryId);
|
|
|
+ intent.putExtra("msg_id",i);
|
|
|
+ intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
|
|
|
+
|
|
|
+ PendingIntent pendingIntent = PendingIntent.getBroadcast(
|
|
|
+ getCurrentActivity(), i, intent, android.app.PendingIntent.FLAG_UPDATE_CURRENT);
|
|
|
+
|
|
|
+ AlarmManager alarmManager = (AlarmManager) getCurrentActivity().getSystemService(Context.ALARM_SERVICE);
|
|
|
+
|
|
|
+ AlarmManager.AlarmClockInfo info = alarmManager.getNextAlarmClock();
|
|
|
+ long t = info.getTriggerTime();
|
|
|
+ PendingIntent pt = info.getShowIntent();
|
|
|
+ String aa = info.toString();
|
|
|
+
|
|
|
+ if (mode.equals("ONCE")){
|
|
|
+ long leftMillis = toNearest1000(jsonObject.getLong("once")-System.currentTimeMillis());
|
|
|
+ if (leftMillis>0){
|
|
|
+ alarmManager.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime()+leftMillis,pendingIntent);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ JSONObject recurring = jsonObject.getJSONObject("recurring");
|
|
|
+ String time = recurring.getString("time");
|
|
|
+ String[] parts = time.split(":");
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.setTimeInMillis(System.currentTimeMillis());
|
|
|
+ calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(parts[0]));
|
|
|
+ calendar.set(Calendar.MINUTE, Integer.parseInt(parts[1]));
|
|
|
+ calendar.set(Calendar.SECOND,Integer.parseInt(parts[2]));
|
|
|
|
|
|
- PendingIntent pendingIntent = PendingIntent.getBroadcast(
|
|
|
- getCurrentActivity(), 0, intent, android.app.PendingIntent.FLAG_IMMUTABLE);
|
|
|
+ // 如果当前时间已经超过 设定时间,则设置为明天
|
|
|
+ if (calendar.getTimeInMillis() <= System.currentTimeMillis()) {
|
|
|
+ calendar.add(Calendar.DAY_OF_YEAR, 1);
|
|
|
+ }
|
|
|
|
|
|
+ alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
|
|
|
+ AlarmManager.INTERVAL_DAY, pendingIntent);
|
|
|
+ Log.i("set alarm status:",time);
|
|
|
|
|
|
- Calendar calendar = Calendar.getInstance();
|
|
|
- calendar.setTimeInMillis(System.currentTimeMillis());
|
|
|
- calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(parts[0]));
|
|
|
- calendar.set(Calendar.MINUTE, Integer.parseInt(parts[1]));
|
|
|
- calendar.set(Calendar.SECOND,Integer.parseInt(parts[2]));
|
|
|
+ }
|
|
|
|
|
|
- // 如果当前时间已经超过 设定时间,则设置为明天
|
|
|
- if (calendar.getTimeInMillis() <= System.currentTimeMillis()) {
|
|
|
- calendar.add(Calendar.DAY_OF_YEAR, 1);
|
|
|
+ }
|
|
|
+ } catch (JSONException e) {
|
|
|
+ e.printStackTrace();
|
|
|
}
|
|
|
+ }
|
|
|
|
|
|
- // 4. 使用 AlarmManager 调度通知
|
|
|
- AlarmManager alarmManager = (AlarmManager) getCurrentActivity().getSystemService(Context.ALARM_SERVICE);
|
|
|
- alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime()+10*1000,pendingIntent);
|
|
|
-// alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
|
|
|
-// AlarmManager.INTERVAL_DAY, pendingIntent);
|
|
|
+ public static long toNearest1000(long number) {
|
|
|
+ return ((number + 500) / 1000) * 1000;
|
|
|
+ }
|
|
|
+
|
|
|
+ @ReactMethod
|
|
|
+ public void rnPageLoaded(){
|
|
|
+ GlobalVariable.rnLoaded = true;
|
|
|
}
|
|
|
|
|
|
private void createNotificationChannel(Context context,String channelId) {
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
|
|
- NotificationChannel channel = new NotificationChannel(channelId, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
|
|
|
+ NotificationChannel channel = new NotificationChannel(channelId, channelId, NotificationManager.IMPORTANCE_HIGH);
|
|
|
channel.setDescription(CHANNEL_DESCRIPTION);
|
|
|
|
|
|
NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
|