|
@@ -1,6 +1,15 @@
|
|
|
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.content.Intent;
|
|
|
import android.graphics.Color;
|
|
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;
|
|
|
|
|
|
|
@@ -8,8 +17,15 @@ 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.
|
|
@@ -47,11 +63,15 @@ public class MainActivity extends ReactActivity {
|
|
|
// intent.putExtra("triggerTime", System.currentTimeMillis() + 10000); // 60秒后触发
|
|
// intent.putExtra("triggerTime", System.currentTimeMillis() + 10000); // 60秒后触发
|
|
|
// startService(intent);
|
|
// startService(intent);
|
|
|
|
|
|
|
|
- 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;
|
|
|
}
|
|
}
|
|
@@ -62,5 +82,70 @@ 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 及以上版本需要)
|
|
|
|
|
+ createNotificationChannel(context);
|
|
|
|
|
+
|
|
|
|
|
+ // 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.set(Calendar.HOUR_OF_DAY, 9);
|
|
|
|
|
+ calendar.set(Calendar.MINUTE, 0);
|
|
|
|
|
+ calendar.set(Calendar.SECOND, 0);
|
|
|
|
|
+
|
|
|
|
|
+ // 4. 使用 AlarmManager 调度通知
|
|
|
|
|
+ AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
|
|
|
|
|
+// alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
|
|
|
|
|
+// SystemClock.elapsedRealtime() + delaySeconds * 1000,
|
|
|
|
|
+// pendingIntent);
|
|
|
|
|
+ alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
|
|
|
|
|
+ AlarmManager.INTERVAL_DAY, 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);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|