如何在 React Native Expo 通知中安排 "birthday"" 通知?
How to schedule a "birthday"" notification in React Native Expo Notifications?
我想为用户的生日安排一个通知。现在我正在尝试在他注册并提供他的出生日期时这样做。当他点击“注册”时,我将数据添加到数据库中,并调用此函数使用他提供的日期数据安排他的生日:
export const scheduleUserBirthday = async(date) => {
var dob = new Date(date)
const birthdayDay = dob.getDay();
const birthdayMonth = dob.getMonth();
const myBirthdayThisYear = new Date(new Date().getFullYear(), birthdayMonth, birthdayDay).setHours(23, 59, 59);
const addToYear = myBirthdayThisYear > Date.now() ? 0 : 1;
const oneDay = 24*60 * 60 * 1000;
const secondDate = new Date(new Date().getFullYear() + addToYear, birthdayMonth, birthdayDay);
const firstDate = new Date();
const days = Math.round(Math.abs((firstDate.getTime() - secondDate.getTime()) / (oneDay)));
await Notifications.scheduleNotificationAsync({
content: {
title: Happy Birthday,
body: 'Have a wonderfull day'
},
trigger: {
day:days
repeats: true,
},
});
}
但是这给了我这个错误:
[Unhandled promise rejection: Error: Failed to schedule the
notification. Trigger of type: calendar is not supported on Android.]
我注意到它只有在给出小时和分钟参数时才有效,所以我尝试在小时内添加下一个通知,但得到了相同的结果。
触发器对象是否只接受这种格式:小时 < 24 和分钟 < 60?
我有点迷茫...(也想设置未来3个月的定时通知)
从外观上看,当您尝试使用这种格式的触发器时,它正在使用日历 API 安排通知。
正如 docs 所说,此功能似乎仅在 iOS 上可用。
您可以改为尝试强制使用 YearlyTrigger,如下所示:
import * as Notifications from "expo-notifications";
import { Platform } from "react-native";
export const scheduleUserBirthday = async (date) => {
const dob = new Date(date);
const day = dob.getDay();
const month = dob.getMonth();
if (Platform.OS === "android") {
await Notifications.setNotificationChannelAsync("birthday-reminder", {
name: "Birthday reminder",
description: "Remind user about his birthday!",
importance: Notifications.AndroidImportance.HIGH,
sound: "default",
});
}
await Notifications.scheduleNotificationAsync({
content: {
title: "Happy Birthday",
body: "Have a wonderfully day",
sound: "default",
},
trigger: {
channelId: "birthday-reminder",
day: day,
month: month,
hour: 0,
minute: 0,
repeats: true,
},
});
};
这还将帮助您创建每年的定期通知,因此您不必担心这部分。
我添加的另一件事是 setNotificationChannelAsync
。我认为在 android.
上提供通知渠道有点强制性
尽管 Expo 为您创建了一个默认频道,但为生日提醒创建一个特定频道也有其好处。它将允许用户从系统应用程序设置on/off切换特定通知类型
我想为用户的生日安排一个通知。现在我正在尝试在他注册并提供他的出生日期时这样做。当他点击“注册”时,我将数据添加到数据库中,并调用此函数使用他提供的日期数据安排他的生日:
export const scheduleUserBirthday = async(date) => {
var dob = new Date(date)
const birthdayDay = dob.getDay();
const birthdayMonth = dob.getMonth();
const myBirthdayThisYear = new Date(new Date().getFullYear(), birthdayMonth, birthdayDay).setHours(23, 59, 59);
const addToYear = myBirthdayThisYear > Date.now() ? 0 : 1;
const oneDay = 24*60 * 60 * 1000;
const secondDate = new Date(new Date().getFullYear() + addToYear, birthdayMonth, birthdayDay);
const firstDate = new Date();
const days = Math.round(Math.abs((firstDate.getTime() - secondDate.getTime()) / (oneDay)));
await Notifications.scheduleNotificationAsync({
content: {
title: Happy Birthday,
body: 'Have a wonderfull day'
},
trigger: {
day:days
repeats: true,
},
});
}
但是这给了我这个错误:
[Unhandled promise rejection: Error: Failed to schedule the notification. Trigger of type: calendar is not supported on Android.]
我注意到它只有在给出小时和分钟参数时才有效,所以我尝试在小时内添加下一个通知,但得到了相同的结果。
触发器对象是否只接受这种格式:小时 < 24 和分钟 < 60?
我有点迷茫...(也想设置未来3个月的定时通知)
从外观上看,当您尝试使用这种格式的触发器时,它正在使用日历 API 安排通知。
正如 docs 所说,此功能似乎仅在 iOS 上可用。
您可以改为尝试强制使用 YearlyTrigger,如下所示:
import * as Notifications from "expo-notifications";
import { Platform } from "react-native";
export const scheduleUserBirthday = async (date) => {
const dob = new Date(date);
const day = dob.getDay();
const month = dob.getMonth();
if (Platform.OS === "android") {
await Notifications.setNotificationChannelAsync("birthday-reminder", {
name: "Birthday reminder",
description: "Remind user about his birthday!",
importance: Notifications.AndroidImportance.HIGH,
sound: "default",
});
}
await Notifications.scheduleNotificationAsync({
content: {
title: "Happy Birthday",
body: "Have a wonderfully day",
sound: "default",
},
trigger: {
channelId: "birthday-reminder",
day: day,
month: month,
hour: 0,
minute: 0,
repeats: true,
},
});
};
这还将帮助您创建每年的定期通知,因此您不必担心这部分。
我添加的另一件事是 setNotificationChannelAsync
。我认为在 android.
尽管 Expo 为您创建了一个默认频道,但为生日提醒创建一个特定频道也有其好处。它将允许用户从系统应用程序设置on/off切换特定通知类型