反应本机本地通知
React native local notifications
我是 React Native 的新手,需要实现一个应用程序需要每天在特定时间向用户发送通知的功能。每天要显示的数据存储在客户端的 json 文件中,不会更改。通知是按计划进行的。考虑到我希望有一种方法可以从应用程序本身触发通知。
有谁知道无需将应用程序与 expo 分离即可实现此目的的方法吗?我不能在没有 运行 react-native link 的情况下使用“react-native-push-notification”,这需要我分离应用程序。那正确吗?
这可能吗?
谢谢:)
您可以使用 scheduleLocalNotificationAsync
函数在 expo 中执行此操作(有关详细信息,请查看 these docs)。请确保您有权先发送通知。请注意,如果通知在应用程序位于前台时触发,您将不会看到通知,但您仍然可以收听此事件。
我。请求许可
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!');
});
三。取消预定的通知
您可以使用scheduleLocalNotificationAsync
函数返回的id取消通知。
import { Notifications } from 'expo';
// ... get the id of the scheduled notification ...
Notifications.cancelScheduledNotificationAsync(id)
我是 React Native 的新手,需要实现一个应用程序需要每天在特定时间向用户发送通知的功能。每天要显示的数据存储在客户端的 json 文件中,不会更改。通知是按计划进行的。考虑到我希望有一种方法可以从应用程序本身触发通知。
有谁知道无需将应用程序与 expo 分离即可实现此目的的方法吗?我不能在没有 运行 react-native link 的情况下使用“react-native-push-notification”,这需要我分离应用程序。那正确吗?
这可能吗?
谢谢:)
您可以使用 scheduleLocalNotificationAsync
函数在 expo 中执行此操作(有关详细信息,请查看 these docs)。请确保您有权先发送通知。请注意,如果通知在应用程序位于前台时触发,您将不会看到通知,但您仍然可以收听此事件。
我。请求许可
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!');
});
三。取消预定的通知
您可以使用scheduleLocalNotificationAsync
函数返回的id取消通知。
import { Notifications } from 'expo';
// ... get the id of the scheduled notification ...
Notifications.cancelScheduledNotificationAsync(id)