反应本机本地通知 [英] React native local notifications

查看:35
本文介绍了反应本机本地通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 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.

我.请求许可

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆