如何创建带有结束日期的本地通知

时间:2020-08-18 08:54:46

标签: react-native uilocalnotification

我需要以结束日期进行本地通知。我已经看过https://wix.github.io/react-native-notifications/docs/localNotifications/https://github.com/zo0r/react-native-push-notification了,但是找不到如何工作的示例。我希望通知的开始日期和结束日期都带有间隔

1 个答案:

答案 0 :(得分:0)

这是针对您提到的第二个图书馆的答案:https://github.com/zo0r/react-native-push-notification

为设置通知的开始日期,我们有 文档中介绍的localNotificationSchedule方法:https://github.com/zo0r/react-native-push-notification#scheduled-notifications

例如:

import PushNotification from 'react-native-push-notification';

PushNotification.localNotificationSchedule({
  id: "1",
  message: "My Notification Message", // (required)
  date: new Date(Date.now() + 60 * 1000), // in 60 secs
  // other props...
});

因此,您传递给date属性的任何日期对象都是在触发通知的那一刻。

请注意,默认情况下,autoCancel的值为true,因此,在传递通知后,无论如何都会取消该通知。因此最好不要处理结束日期,因为已经为您处理了。


如果仍然要取消它,则可以在上面的示例中添加autoCancel: true并取消通知,如下所示:

PushNotification.cancelLocalNotifications({id: '1'});

因此,如果您想控制结束日期,可以先触发推送通知,然后直接在cancelLocalNotifications内调用setTimeout之后触发推送通知。然后,您可以控制通知何时开始以及何时取消。

如果您想在后台执行此操作,可以查看https://github.com/transistorsoft/react-native-background-fetch之类的后台任务库。