我想為用戶的生日安排通知。現在,當他注冊并給出他的出生日期時,我正在嘗試這樣做。當他單擊“注冊”時,我將資料添加到資料庫中,并使用他提供的日期資料呼叫此函式來安排他的生日:
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,
},
});
}
然而,這給了我這個錯誤:
[未處理的承諾拒絕:錯誤:未能安排通知。觸發器型別:Android 不支持日歷。]
我注意到它只有在給出小時和分鐘引數時才有效,所以我嘗試在小時內添加下一個通知,但得到了相同的結果。
觸發器物件是否只接受這種格式:小時 < 24 和分鐘 < 60?
我有點失落......(也想設定未來3個月的預定通知)
uj5u.com熱心網友回復:
從外觀上看,當您嘗試使用這種觸發器格式時,它正在使用日歷 API 來安排通知。
正如檔案所說,此功能似乎僅在 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 為您創建了一個默認頻道,但為生日提醒創建一個特定頻道也有其好處。它將允許用戶從系統應用程式設定中打開/關閉該特定通知型別
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/422631.html
標籤:
上一篇:ReactNative:本地影像不適用于ReactNative中的FlatList?
下一篇:不使用<Text>標簽的輸出變數
