|
@@ -1,19 +1,38 @@
|
|
|
package com.hola;
|
|
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.graphics.Color;
|
|
|
|
|
+import android.os.Build;
|
|
|
|
|
+import android.os.SystemClock;
|
|
|
|
|
+
|
|
|
|
|
+import androidx.core.app.NotificationCompat;
|
|
|
|
|
+
|
|
|
import expo.modules.ReactActivityDelegateWrapper;
|
|
import expo.modules.ReactActivityDelegateWrapper;
|
|
|
|
|
|
|
|
import com.facebook.react.ReactActivity;
|
|
import com.facebook.react.ReactActivity;
|
|
|
import com.facebook.react.ReactActivityDelegate;
|
|
import com.facebook.react.ReactActivityDelegate;
|
|
|
import com.facebook.react.ReactRootView;
|
|
import com.facebook.react.ReactRootView;
|
|
|
|
|
|
|
|
|
|
+import java.util.Calendar;
|
|
|
|
|
+
|
|
|
public class MainActivity extends ReactActivity {
|
|
public class MainActivity extends ReactActivity {
|
|
|
|
|
|
|
|
|
|
+ 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";
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* Returns the name of the main component registered from JavaScript. This is used to schedule
|
|
* Returns the name of the main component registered from JavaScript. This is used to schedule
|
|
|
* rendering of the component.
|
|
* rendering of the component.
|
|
|
*/
|
|
*/
|
|
|
@Override
|
|
@Override
|
|
|
protected String getMainComponentName() {
|
|
protected String getMainComponentName() {
|
|
|
- return "hola";
|
|
|
|
|
|
|
+ return "taroDemo";
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -26,7 +45,7 @@ public class MainActivity extends ReactActivity {
|
|
|
return new ReactActivityDelegateWrapper(this, new MainActivityDelegate(this, getMainComponentName()));
|
|
return new ReactActivityDelegateWrapper(this, new MainActivityDelegate(this, getMainComponentName()));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public static class MainActivityDelegate extends ReactActivityDelegate {
|
|
|
|
|
|
|
+ public class MainActivityDelegate extends ReactActivityDelegate {
|
|
|
public MainActivityDelegate(ReactActivity activity, String mainComponentName) {
|
|
public MainActivityDelegate(ReactActivity activity, String mainComponentName) {
|
|
|
super(activity, mainComponentName);
|
|
super(activity, mainComponentName);
|
|
|
}
|
|
}
|
|
@@ -34,8 +53,26 @@ public class MainActivity extends ReactActivity {
|
|
|
@Override
|
|
@Override
|
|
|
protected ReactRootView createRootView() {
|
|
protected ReactRootView createRootView() {
|
|
|
ReactRootView reactRootView = new ReactRootView(getContext());
|
|
ReactRootView reactRootView = new ReactRootView(getContext());
|
|
|
|
|
+ reactRootView.setBackgroundColor(Color.rgb(0,0,0));
|
|
|
// If you opted-in for the New Architecture, we enable the Fabric Renderer.
|
|
// If you opted-in for the New Architecture, we enable the Fabric Renderer.
|
|
|
reactRootView.setIsFabric(BuildConfig.IS_NEW_ARCHITECTURE_ENABLED);
|
|
reactRootView.setIsFabric(BuildConfig.IS_NEW_ARCHITECTURE_ENABLED);
|
|
|
|
|
+
|
|
|
|
|
+// Intent intent = new Intent(getContext(), AlarmService.class);
|
|
|
|
|
+// intent.putExtra("title", "My Notification");
|
|
|
|
|
+// intent.putExtra("message", "This is a scheduled local notification.");
|
|
|
|
|
+// intent.putExtra("triggerTime", System.currentTimeMillis() + 10000); // 60秒后触发
|
|
|
|
|
+// startService(intent);
|
|
|
|
|
+
|
|
|
|
|
+// createNotificationChannel(getContext());
|
|
|
|
|
+//
|
|
|
|
|
+// Intent intent = new Intent(getContext(), AlarmService.class);
|
|
|
|
|
+// intent.putExtra("title", "My Notification");
|
|
|
|
|
+// intent.putExtra("message", "This is a scheduled local notification.");
|
|
|
|
|
+// intent.putExtra("triggerTime", System.currentTimeMillis() + 10000); // 60秒后触发
|
|
|
|
|
+// intent.putExtra("channel","CHANNEL_ID");
|
|
|
|
|
+// startService(intent);
|
|
|
|
|
+// scheduleNotificationAfter(getContext(),10,"hellow","world");
|
|
|
|
|
+
|
|
|
return reactRootView;
|
|
return reactRootView;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -45,5 +82,81 @@ public class MainActivity extends ReactActivity {
|
|
|
// More on this on https://reactjs.org/blog/2022/03/29/react-v18.html
|
|
// More on this on https://reactjs.org/blog/2022/03/29/react-v18.html
|
|
|
return BuildConfig.IS_NEW_ARCHITECTURE_ENABLED;
|
|
return BuildConfig.IS_NEW_ARCHITECTURE_ENABLED;
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ public void scheduleNotificationAfter(Context context, long delaySeconds, String title, String body) {
|
|
|
|
|
+ // 1. 创建通知渠道 (Android 8.0 及以上版本需要)
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ // 2. 创建通知意图
|
|
|
|
|
+ Intent intent = new Intent(context, AlarmReceiver.class);
|
|
|
|
|
+ intent.putExtra("title", title);
|
|
|
|
|
+ intent.putExtra("body", body);
|
|
|
|
|
+
|
|
|
|
|
+ // 3. 创建 PendingIntent
|
|
|
|
|
+ android.app.PendingIntent pendingIntent = android.app.PendingIntent.getBroadcast(
|
|
|
|
|
+ context, 0, intent, android.app.PendingIntent.FLAG_IMMUTABLE);
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
|
|
+ calendar.setTimeInMillis(System.currentTimeMillis());
|
|
|
|
|
+ calendar.set(Calendar.HOUR_OF_DAY, 11);
|
|
|
|
|
+ calendar.set(Calendar.MINUTE, 42);
|
|
|
|
|
+
|
|
|
|
|
+ // 如果当前时间已经超过 设定时间,则设置为明天
|
|
|
|
|
+ if (calendar.getTimeInMillis() <= System.currentTimeMillis()) {
|
|
|
|
|
+ calendar.add(Calendar.DAY_OF_YEAR, 1);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 4. 使用 AlarmManager 调度通知
|
|
|
|
|
+ AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
|
|
|
|
|
+ alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
|
|
|
|
|
+ AlarmManager.INTERVAL_DAY, pendingIntent);
|
|
|
|
|
+
|
|
|
|
|
+// alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
|
|
|
|
|
+// SystemClock.elapsedRealtime() + delaySeconds * 1000,
|
|
|
|
|
+// pendingIntent);
|
|
|
|
|
+// alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
|
|
|
|
|
+// 10*1000, pendingIntent);
|
|
|
|
|
+// alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),pendingIntent);
|
|
|
|
|
+// alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime()+10*1000,
|
|
|
|
|
+// pendingIntent);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public void scheduleDailyReminder(Context context) {
|
|
|
|
|
+ // 1. 创建 Notification 的 Channel (Android 8.0 及以上版本需要)
|
|
|
|
|
+ createNotificationChannel(context);
|
|
|
|
|
+
|
|
|
|
|
+ // 2. 设置 Notification 的内容
|
|
|
|
|
+ NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID)
|
|
|
|
|
+ .setSmallIcon(R.drawable.ic_launcher)
|
|
|
|
|
+ .setContentTitle("Daily Reminder")
|
|
|
|
|
+ .setContentText("It's 9:00 AM, time to start your day!")
|
|
|
|
|
+ .setPriority(NotificationCompat.PRIORITY_DEFAULT);
|
|
|
|
|
+
|
|
|
|
|
+ // 3. 设置定时任务
|
|
|
|
|
+ Intent intent = new Intent(context, AlarmReceiver.class);
|
|
|
|
|
+ intent.putExtra("notification", builder.build());
|
|
|
|
|
+
|
|
|
|
|
+ PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_IMMUTABLE);
|
|
|
|
|
+
|
|
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
|
|
+ calendar.set(Calendar.HOUR_OF_DAY, 9);
|
|
|
|
|
+ calendar.set(Calendar.MINUTE, 0);
|
|
|
|
|
+ calendar.set(Calendar.SECOND, 0);
|
|
|
|
|
+
|
|
|
|
|
+ AlarmManager alarmManager = (AlarmManager) context.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);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|