|
|
@@ -129,6 +129,7 @@ public class HolaModule extends ReactContextBaseJavaModule {
|
|
|
@ReactMethod
|
|
|
public void clearNotification(){
|
|
|
clearClockSchedule();
|
|
|
+ clearSunClockSchedule();
|
|
|
}
|
|
|
|
|
|
private void clearClockSchedule(){
|
|
|
@@ -159,6 +160,77 @@ public class HolaModule extends ReactContextBaseJavaModule {
|
|
|
|
|
|
}
|
|
|
|
|
|
+ private void clearSunClockSchedule(){
|
|
|
+
|
|
|
+ int flags = PendingIntent.FLAG_UPDATE_CURRENT;
|
|
|
+ if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.M){
|
|
|
+ flags = PendingIntent.FLAG_IMMUTABLE;
|
|
|
+ }
|
|
|
+ AlarmManager alarmManager = (AlarmManager) getCurrentActivity().getSystemService(Context.ALARM_SERVICE);
|
|
|
+
|
|
|
+ //取消历史alarm
|
|
|
+ List<PendingIntent> allPendingIntents = new ArrayList<>();
|
|
|
+ for (int i = 0; i < 100; i++) {
|
|
|
+ int requestCode = i;
|
|
|
+ PendingIntent pendingIntent = PendingIntent.getBroadcast(getCurrentActivity(), i+100, new Intent(getCurrentActivity(),AlarmReceiver.class), flags);
|
|
|
+ if (pendingIntent != null) {
|
|
|
+ allPendingIntents.add(pendingIntent);
|
|
|
+ } else {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ for (PendingIntent pendingIntent : allPendingIntents) {
|
|
|
+ alarmManager.cancel(pendingIntent);
|
|
|
+ pendingIntent.cancel();
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @ReactMethod
|
|
|
+ public void addSunPush(String jsonData){
|
|
|
+ if (getCurrentActivity()==null){
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ clearSunClockSchedule();
|
|
|
+ AlarmManager alarmManager = (AlarmManager) getCurrentActivity().getSystemService(Context.ALARM_SERVICE);
|
|
|
+
|
|
|
+ int flags = PendingIntent.FLAG_UPDATE_CURRENT;
|
|
|
+ if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.M){
|
|
|
+ flags = PendingIntent.FLAG_IMMUTABLE;
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ JSONArray jsonArray = new JSONArray(jsonData);
|
|
|
+ for (int i = 0; i < jsonArray.length(); i++) {
|
|
|
+ JSONObject jsonObject = jsonArray.getJSONObject(i);
|
|
|
+ String title = jsonObject.getString("title");
|
|
|
+ String body = jsonObject.getString("body");
|
|
|
+ String channelId = jsonObject.getString("channel_id");
|
|
|
+
|
|
|
+ createNotificationChannel(getCurrentActivity(),channelId);
|
|
|
+ Intent intent = new Intent(getCurrentActivity(), AlarmSunReceiver.class);
|
|
|
+ intent.putExtra("title", title);
|
|
|
+ intent.putExtra("message", body);
|
|
|
+ intent.putExtra("msg_id",i);
|
|
|
+ intent.putExtra("channelID",channelId);
|
|
|
+ intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
|
|
|
+
|
|
|
+ PendingIntent pendingIntent = PendingIntent.getBroadcast(
|
|
|
+ getCurrentActivity(), i, intent, flags);
|
|
|
+
|
|
|
+
|
|
|
+ long leftMillis = toNearest1000(jsonObject.getLong("timestamp")-System.currentTimeMillis());
|
|
|
+ if (leftMillis>0){
|
|
|
+ alarmManager.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime()+leftMillis,pendingIntent);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (JSONException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
@ReactMethod
|
|
|
public void addLocalPush(String jsonData){
|
|
|
if (getCurrentActivity()==null){
|