如何用Expo Notification为指定星期几列表调度重复通知?
解决方案
Expo Notifications 的 scheduleNotificationAsync 方法里,trigger 的 weekday 参数只支持单个数值(1-7,1对应周日),没法直接传入数组实现多星期几重复。要达成需求,只能为每个指定的星期几单独创建一条重复通知,然后收集所有通知ID返回。
先修正你代码里的小问题:vibrate: true | 1000 写法错误,布尔值和数值的按位或会得到1000,如果你需要振动1秒,应该写成 vibrate: [1000];如果只是需要默认振动,直接写 vibrate: true 即可。
修改后的代码如下:
import { Notifications } from 'expo-notifications'; import { AndroidNotificationPriority } from 'expo-notifications'; export async function schedulePushNotification(rotina, horas, minutos, dias) { // 遍历每个星期几,创建对应的通知调度 const notificationPromises = dias.map(async (day) => { return await Notifications.scheduleNotificationAsync({ content: { title: "Rotina agendada", subtitle: "Mensagem de Rotinagem", body: `Não se esqueça, você definiu ${rotina} as ${horas}:${minutos}`, vibrate: [1000], // 修正振动参数写法 priority: AndroidNotificationPriority.HIGH, // sound: 'default', }, trigger: { hour: horas, minute: minutos, weekday: day, // 为当前星期几设置重复 repeats: true, }, }); }); // 并行执行所有通知调度,获取ID列表 const notificationIds = await Promise.all(notificationPromises); return notificationIds; }
说明
- 每个星期几对应一条独立的重复通知调度,因此会返回一个ID数组,每个ID对应一个星期几的通知
- 无法用单个通知ID实现多星期几重复,因为Expo的通知调度不支持同时绑定多个星期几的规则
- 如果后续需要取消这些通知,只需遍历ID数组,调用
Notifications.cancelScheduledNotificationAsync(id)即可
内容的提问来源于stack exchange,提问作者Guilherme Luzin
相关产品推荐
相关产品推荐

