|
|
@@ -22,8 +22,15 @@ import com.facebook.react.bridge.Callback;
|
|
|
import com.facebook.react.bridge.ReactApplicationContext;
|
|
|
import com.facebook.react.bridge.ReactContextBaseJavaModule;
|
|
|
import com.facebook.react.bridge.ReactMethod;
|
|
|
+import com.facebook.react.bridge.ReadableArray;
|
|
|
|
|
|
+import org.json.JSONArray;
|
|
|
+import org.json.JSONException;
|
|
|
+import org.json.JSONObject;
|
|
|
+
|
|
|
+import java.util.Arrays;
|
|
|
import java.util.Calendar;
|
|
|
+import java.util.List;
|
|
|
|
|
|
public class HolaModule extends ReactContextBaseJavaModule {
|
|
|
private static final String CHANNEL_ID = "local_notification_channel";
|
|
|
@@ -67,14 +74,68 @@ public class HolaModule extends ReactContextBaseJavaModule {
|
|
|
}
|
|
|
|
|
|
@ReactMethod
|
|
|
- public void addLocalPush(String title,String content,String time){
|
|
|
+ public void addLocalPush(String jsonData){
|
|
|
+ if (getCurrentActivity()==null){
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ 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(),strId);
|
|
|
+ Intent intent = new Intent(getCurrentActivity(), AlarmReceiver.class);
|
|
|
+ intent.putExtra("title", title);
|
|
|
+ intent.putExtra("message", body);
|
|
|
+ intent.putExtra("channel", CHANNEL_ID);
|
|
|
+ intent.putExtra("id",strId);
|
|
|
+ intent.putExtra("categoryId",categoryId);
|
|
|
+
|
|
|
+ PendingIntent pendingIntent = PendingIntent.getBroadcast(
|
|
|
+ getCurrentActivity(), 0, intent, android.app.PendingIntent.FLAG_IMMUTABLE);
|
|
|
+
|
|
|
+ AlarmManager alarmManager = (AlarmManager) getCurrentActivity().getSystemService(Context.ALARM_SERVICE);
|
|
|
+ if (mode.equals("ONCE")){
|
|
|
+ alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime()+10*1000,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]));
|
|
|
+
|
|
|
+ // 如果当前时间已经超过 设定时间,则设置为明天
|
|
|
+ if (calendar.getTimeInMillis() <= System.currentTimeMillis()) {
|
|
|
+ calendar.add(Calendar.DAY_OF_YEAR, 1);
|
|
|
+ }
|
|
|
+ alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
|
|
|
+ AlarmManager.INTERVAL_DAY, pendingIntent);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ } catch (JSONException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @ReactMethod
|
|
|
+ public void addLocalPush2(String title,String content,String time){
|
|
|
if (getCurrentActivity()==null){
|
|
|
return;
|
|
|
}
|
|
|
String[] parts = time.split(":");
|
|
|
- Log.e("Hello","world");
|
|
|
- System.out.println("Hello, " + title + "!");
|
|
|
- createNotificationChannel(getCurrentActivity());
|
|
|
+
|
|
|
+ createNotificationChannel(getCurrentActivity(),"111");
|
|
|
Intent intent = new Intent(getCurrentActivity(), AlarmReceiver.class);
|
|
|
intent.putExtra("title", title);
|
|
|
intent.putExtra("message", content);
|
|
|
@@ -97,14 +158,14 @@ public class HolaModule extends ReactContextBaseJavaModule {
|
|
|
|
|
|
// 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);
|
|
|
+ alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime()+10*1000,pendingIntent);
|
|
|
+// alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
|
|
|
+// AlarmManager.INTERVAL_DAY, pendingIntent);
|
|
|
}
|
|
|
|
|
|
- private void createNotificationChannel(Context context) {
|
|
|
+ private void createNotificationChannel(Context context,String channelId) {
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
|
|
- NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT);
|
|
|
+ NotificationChannel channel = new NotificationChannel(channelId, CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT);
|
|
|
channel.setDescription(CHANNEL_DESCRIPTION);
|
|
|
|
|
|
NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
|