Skip to content

Commit

Permalink
feat: Add localNotification for event subscription
Browse files Browse the repository at this point in the history
  • Loading branch information
love98ooo committed Nov 22, 2023
1 parent 4d3745a commit 37f6e28
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
9 changes: 9 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"@capacitor/camera": "^5.0.7",
"@capacitor/core": "5.5.0",
"@capacitor/keyboard": "5.0.6",
"@capacitor/local-notifications": "^5.0.6",
"@capacitor/share": "^5.0.6",
"@capacitor/status-bar": "^5.0.6",
"@capacitor/toast": "^5.0.6",
Expand Down
31 changes: 31 additions & 0 deletions src/pages/Event.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { getEventInfo, getUserParticipant, registerEvent, subcribeEvent } from '
import { folderOpenOutline, peopleOutline, pricetagsOutline, shareSocialOutline, alarmOutline, alarmSharp, timeOutline, businessOutline, documentOutline, statsChartOutline, statsChartSharp } from 'ionicons/icons';
import './Event.scss';
import { Share } from '@capacitor/share';
import { LocalNotificationSchema, LocalNotifications, ScheduleOptions, Schedule, LocalNotificationDescriptor, CancelOptions } from '@capacitor/local-notifications';


const EventPage: React.FC = () => {
Expand Down Expand Up @@ -61,6 +62,28 @@ const EventPage: React.FC = () => {
const subscribe = () => {
subcribeEvent(Number(eventId), true).then(() => {
setIsSubscribe(true);
const scheduleDate = new Date();
scheduleDate.setFullYear(Number(event.gmtEventStart.slice(0, 4)));
scheduleDate.setMonth(Number(event.gmtEventStart.slice(5, 7)) - 1);
scheduleDate.setDate(Number(event.gmtEventStart.slice(8, 10)));
scheduleDate.setHours(Number(event.gmtEventStart.slice(11, 13)));
scheduleDate.setMinutes(Number(event.gmtEventStart.slice(14, 16)));
scheduleDate.setSeconds(0);
const schedule: Schedule = {
at: scheduleDate
}
const localNotificationSchema: LocalNotificationSchema = {
title: "即将开始:" + event.title,
body: event.description,
schedule: schedule,
id: Number(eventId)
};
const scheduleOptions: ScheduleOptions = {
notifications: [localNotificationSchema]
}
LocalNotifications.schedule(scheduleOptions);
console.log(scheduleOptions);
console.log(localNotificationSchema);
}, (error) => {
console.log(error);
setIsSubscribe(false);
Expand All @@ -70,6 +93,14 @@ const EventPage: React.FC = () => {
const unsubscribe = () => {
subcribeEvent(Number(eventId), false).then(() => {
setIsSubscribe(false);
const localNotificationDescriptor: LocalNotificationDescriptor = {
id: Number(eventId)
}
const cancelOptions: CancelOptions = {
notifications: [localNotificationDescriptor]
}
LocalNotifications.cancel(cancelOptions);
console.log(cancelOptions);
}, (error) => {
console.log(error);
setIsSubscribe(true);
Expand Down

0 comments on commit 37f6e28

Please sign in to comment.