|
|
@@ -0,0 +1,81 @@
|
|
|
+package com.hola;
|
|
|
+
|
|
|
+import android.app.AlarmManager;
|
|
|
+import android.app.NotificationChannel;
|
|
|
+import android.app.NotificationManager;
|
|
|
+import android.app.PendingIntent;
|
|
|
+import android.content.Context;
|
|
|
+import android.content.Intent;
|
|
|
+import android.os.Build;
|
|
|
+import android.util.Log;
|
|
|
+
|
|
|
+import androidx.annotation.NonNull;
|
|
|
+import androidx.annotation.Nullable;
|
|
|
+
|
|
|
+import com.facebook.react.bridge.ReactApplicationContext;
|
|
|
+import com.facebook.react.bridge.ReactContextBaseJavaModule;
|
|
|
+import com.facebook.react.bridge.ReactMethod;
|
|
|
+
|
|
|
+import java.util.Calendar;
|
|
|
+
|
|
|
+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";
|
|
|
+
|
|
|
+
|
|
|
+ public HolaModule(@Nullable ReactApplicationContext reactContext) {
|
|
|
+ super(reactContext);
|
|
|
+ }
|
|
|
+ private static final String MODULE_NAME = "HolaModule";
|
|
|
+ @NonNull
|
|
|
+ @Override
|
|
|
+ public String getName() {
|
|
|
+ return MODULE_NAME;
|
|
|
+ }
|
|
|
+
|
|
|
+ @ReactMethod
|
|
|
+ public void addLocalPush(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());
|
|
|
+ Intent intent = new Intent(getCurrentActivity(), AlarmReceiver.class);
|
|
|
+ intent.putExtra("title", title);
|
|
|
+ intent.putExtra("message", content);
|
|
|
+ intent.putExtra("channel", CHANNEL_ID);
|
|
|
+
|
|
|
+ PendingIntent pendingIntent = PendingIntent.getBroadcast(
|
|
|
+ getCurrentActivity(), 0, intent, android.app.PendingIntent.FLAG_IMMUTABLE);
|
|
|
+
|
|
|
+
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 4. 使用 AlarmManager 调度通知
|
|
|
+ AlarmManager alarmManager = (AlarmManager) getCurrentActivity().getSystemService(Context.ALARM_SERVICE);
|
|
|
+ alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
|
|
|
+ AlarmManager.INTERVAL_DAY, pendingIntent);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void createNotificationChannel(Context context) {
|
|
|
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
|
|
+ NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT);
|
|
|
+ channel.setDescription(CHANNEL_DESCRIPTION);
|
|
|
+
|
|
|
+ NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
|
|
|
+ notificationManager.createNotificationChannel(channel);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|