【发布时间】:2020-10-29 20:39:17
【问题描述】:
我目前正在开发一款 Android 移动应用程序。
这是一款厨房食谱应用。应用会在白天向用户发送通知。
在应用程序的设置中,用户可以选择接收通知的数量和时间(例如上午 11 点到晚上 7 点)
这就是问题的开始;
我使用react-native-push-notification 库,代码如下:
static LocalNotif(string)
{
PushNotification.localNotification({
vibrate: true, // (optional) default: true
vibration: 300, // vibration length in milliseconds, ignored if vibrate=false, default: 1000
title: "Vérifier vos produit", // (optional)
message: string, // (required)
largeIcon: "ic_launcher",
smallIcon: "ic_notification",
});
}
接下来,我使用react-native-background-fetch 发送通知,即使应用程序没有运行
static async backFetch(delay_to_next_notif)
{
BackgroundFetch.configure({
minimumFetchInterval: 3600
}, async (taskId) => {
// This is the fetch-event callback.
console.log("[BackgroundFetch] taskId: ", taskId);
// Use a switch statement to route task-handling.
switch (taskId) {
case 'com.foo.customtask':
this.LocalNotif("test")
break;
default:
console.log("Default fetch task");
}
// Finish, providing received taskId.
BackgroundFetch.finish(taskId);
});
// Step 2: Schedule a custom "oneshot" task "com.foo.customtask" to execute 5000ms from now.
BackgroundFetch.scheduleTask({
taskId: "com.foo.customtask",
forceAlarmManager: true,
delay: delay_to_next_notif// <-- milliseconds
});
}
react-native-background-fetch 的使用很奇怪。有时我从未收到通知。
是否可以使用推送通知库并创建例程,以便用户在一天中的特定时间接收通知,即使应用程序没有运行?
【问题讨论】:
标签: javascript android react-native