20260706170339
This commit is contained in:
1 parent
ae99f50f6d
commit
4d1e6c8b8f
50 files changed
+3045
-2249
No files matched your search
@@ -0,0 +1,52 @@
|
||||
import * as Notifications from "expo-notifications";
|
||||
|
||||
// -- 初始化配置 --
|
||||
Notifications.setNotificationHandler({
|
||||
handleNotification: async () => ({
|
||||
shouldShowAlert: true,
|
||||
shouldPlaySound: true,
|
||||
shouldSetBadge: false,
|
||||
}),
|
||||
});
|
||||
|
||||
// -- 请求权限 --
|
||||
export async function requestNotificationPermission(): Promise<boolean> {
|
||||
const { status } = await Notifications.requestPermissionsAsync();
|
||||
return status === "granted";
|
||||
}
|
||||
|
||||
// -- 预定提醒通知 --
|
||||
export async function scheduleTodoReminder(
|
||||
todoId: number,
|
||||
todoName: string,
|
||||
remindAt: string,
|
||||
) {
|
||||
if (!remindAt) return null;
|
||||
|
||||
const triggerDate = new Date(remindAt);
|
||||
if (triggerDate <= new Date()) return null; // 时间已过,不预定
|
||||
|
||||
return Notifications.scheduleNotificationAsync({
|
||||
content: {
|
||||
title: "⏰ 待办提醒",
|
||||
body: `「${todoName}」该处理了`,
|
||||
data: { todoId, type: "todo_reminder" },
|
||||
},
|
||||
trigger: { type: Notifications.SchedulableTriggerInputTypes.DATE, date: triggerDate },
|
||||
});
|
||||
}
|
||||
|
||||
// -- 取消提醒通知 --
|
||||
export async function cancelTodoReminder(identifier: string) {
|
||||
await Notifications.cancelScheduledNotificationAsync(identifier);
|
||||
}
|
||||
|
||||
// -- 取消某个todo的所有提醒 --
|
||||
export async function cancelTodoReminders(todoId: number) {
|
||||
const scheduled = await Notifications.getAllScheduledNotificationsAsync();
|
||||
for (const n of scheduled) {
|
||||
if ((n.content.data as any)?.todoId === todoId) {
|
||||
await Notifications.cancelScheduledNotificationAsync(n.identifier);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user