React native 如何在react native中发送随机本地通知消息?
我有一个应用程序,它需要每天向用户发送通知,并根据用户需要的通知数量(每天最多5条通知)和时间间隔(例如,通知仅在每天早上6:00到9:00之间发出)向用户发送随机消息 为了详细说明,我正在构建一个功能,其中包含一个想法,即从硬编码的数组变量或json文件中随机发送鼓舞人心的消息 目前我正在使用这个包:创建本地通知React native 如何在react native中发送随机本地通知消息?,react-native,notifications,react-native-push-notification,React Native,Notifications,React Native Push Notification,我有一个应用程序,它需要每天向用户发送通知,并根据用户需要的通知数量(每天最多5条通知)和时间间隔(例如,通知仅在每天早上6:00到9:00之间发出)向用户发送随机消息 为了详细说明,我正在构建一个功能,其中包含一个想法,即从硬编码的数组变量或json文件中随机发送鼓舞人心的消息 目前我正在使用这个包:创建本地通知 PushNotification.localNotificationSchedule({ id : '1', userInfo : { id: use
PushNotification.localNotificationSchedule({
id : '1',
userInfo : { id: userId },
message : () => {
return Math.random().toString(36).replace(/[^a-z]+/g, '').substr(0, 5); //trying to return random string every time notification fires.
},
date : moment(Date.now()).add(2, 'seconds').toDate(),
repeatType : 'day',
});
我尝试设置一个函数,为localNotificationSchedule的消息参数返回一个字符串,但是当我这样做时,它没有显示通知,而不是使用常规字符串
PushNotification.localNotificationSchedule({
id : '1',
userInfo : { id: userId },
message : () => {
return Math.random().toString(36).replace(/[^a-z]+/g, '').substr(0, 5); //trying to return random string every time notification fires.
},
date : moment(Date.now()).add(2, 'seconds').toDate(),
repeatType : 'day',
});
我考虑过使用其他方法,比如react native headless JS,但它只适用于android
也考虑使用。但是我有一个复杂的通知间隔。例如,用户可以将通知设置为每天早上6:00到6:30运行,并设置为触发5个通知。在此时间间隔内,通知将每6分钟运行一次
但是react native background fetch的最小间隔只有15分钟
我知道这可以通过使用推送通知来实现,但有了推送通知,用户需要连接才能接收通知,这在这种情况下并不理想
我在Ios应用程序中看到了这一点,所以我知道这是可能实现的 根据开发人员的要求,您可以尝试多次调用
PushNotification.localNotificationSchedule
我所做的是:
const messages = [{text:'',time:0}...];
messages.map(message=>{
PushNotification.localNotificationSchedule({
//... You can use all the options from localNotifications
channelId: "my-channel",
message: message.text, // (required)
date: new Date(Date.now() + (60 + message.time) * 1000), // in 60 secs
});
})
以5秒间隔显示来自消息数组的消息。通知是由系统设置的还是由用户选择的?由用户选择的