0

我正在尝试取消我的来自 expo 的预定通知,并从创建的对象中提供 id:

 const newHabit = {
     name: habitName,
     color: updatedColor,
     icon: habitIcon,
     days: isEnabled ? daysCount : null,
     times: isEnabled ? timesCount : null,
     specificDate: isEnabledSpecific ? specificDate : null,
     reminder: isEnabledDate ? reminderTime : null,
     description: description,
     completedDay: null,
     currentDay: 0,
     completed: false,
     completedDates: {},
     timesCompleted: 0,
     progress: 0,
     notificationId: Math.floor(Math.random() * 1000).toString(),
};

我以不同的方式保存通知方法,如下所示:

export default async function schedulePushNotification(content, trigger, repeats) {
    await Notifications.scheduleNotificationAsync({
        content: content,
        trigger: trigger,
        repeats: repeats,
    });
}

export const cancelPushNotification = async (id) => {
    try {
        await Notifications.cancelScheduledNotificationAsync(id);
    } catch (error) {
        console.error(error);
    }
};

在这里,我尝试使用给定的 notificationId 取消它:(数据来自父级的道具并正确包含创建的 notificationId)。

 cancelPushNotification(data.notificationId).then(() => {
            if (habitReminderTime !== null) schedulePushNotification(content, trigger, repeats);
            if (habitSpecificDate !== null)
                schedulePushNotification(contentSpecific, triggerSpecific, false);
        });

它仍然会发送旧通知和新通知。所以它不起作用。有任何想法吗?

4

0 回答 0