反应本地本地通知 [英] React native local notifications
问题描述
我是React Native的新手,需要实现一项功能,使应用程序需要每天在特定时间向用户发送通知.每天要显示的数据存储在客户端的json文件中,并且不会更改.通知按计划进行.鉴于我希望有一种方法可以仅从应用程序本身触发通知.
I am new to React Native and need to implement a functionality where the app needs to send the user a notification every day at a certain time. The data to be shown for each day is stored in a json file on the client side and will not change. The notifications are on a schedule. Given that I was hoping there could be a way to just trigger a notification from the app itself.
有人知道无需将应用程序与博览会分离就可以实现这一目标的方法吗?如果不运行react-native链接,则无法使用"react-native-push-notification",这需要我拆离该应用程序.是对的吗?
Does anyone know of a way to achieve this without having to detach the app from expo? I can't use 'react-native-push-notification'without running react-native link and that requires me to detach the app. Is that right?
这可能吗?
谢谢:)
推荐答案
您可以使用scheduleLocalNotificationAsync
函数在expo中进行此操作(请查看
You can do this with expo using the scheduleLocalNotificationAsync
function (have a look at these docs for more details). Make sure you have permission to send notifications first. Note that if the notification triggers when the app is in the foreground you won't see a notification but you can still listen to this event.
i.寻求许可
import { Permissions } from 'expo';
// ... somewhere before scheduling notifications ...
const { status } = await Permissions.getAsync(Permissions.NOTIFICATIONS);
if (status !== 'granted') {
await Permissions.askAsync(Permissions.NOTIFICATIONS);
}
ii.安排通知
import { Notifications } from 'expo';
const notification = {
title: 'Hi there!',
body: 'Tap me to open the app.',
android: { sound: true }, // Make a sound on Android
ios: { sound: true }, // Make a sound on iOS
};
const options = {
time: Date.now() + 10000, // Schedule it in 10 seconds
repeat: 'day', // Repeat it daily
};
// ... somewhere after requesting permission ...
const id = Notifications.scheduleLocalNotificationAsync(notification, options)
// If you want to react even when your app is still in the
// foreground, you can listen to the event like this:
Notifications.addListener(() => {
console.log('triggered!');
});
iii.取消预定的通知
您可以使用scheduleLocalNotificationAsync
函数的返回ID取消通知.
You can use the returned id of the scheduleLocalNotificationAsync
function to cancel the notification.
import { Notifications } from 'expo';
// ... get the id of the scheduled notification ...
Notifications.cancelScheduledNotificationAsync(id)
这篇关于反应本地本地通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!