expo-notifications
@amazon-devices/expo-notifications提供了一个API来显示、关闭和列出本地通知。
这是一个系统部署的库,可供适用于Vega的React Native应用使用,无需单独的安装过程。它作为自动链接库进行部署,您的应用在运行时会链接到该库。只能在库和为其构建的适用于Vega的React Native版本之间保证兼容性。
安装
- 在
package.json文件中添加JavaScript库依赖项。"dependencies": { ... "@amazon-devices/expo-notifications": "~2.0.0", "expo": "~50.0.0", ... } -
使用
npm install命令重新安装依赖项。 - 更新您的
manifest.toml文件。[needs] ... [[needs.privilege]] id = "com.amazon.notification.privilege.post" [[needs.privilege]] id = "com.amazon.notification.privilege.query" - 使用
npm run build:app命令重新构建应用。
示例
安排、列出和关闭本地通知的示例:
import * as Notifications from '@amazon-devices/expo-notifications';
import {
Importance,
InformationKind,
} from '@amazon-devices/expo-notifications/build/kepler';
import React, {useState} from 'react';
import {Button, StyleSheet, Text, View} from 'react-native';
export const App = () => {
const [result, setResult] = useState('');
const presentLocalNotificationAsync = async () => {
const id = await Notifications.scheduleNotificationAsync({
content: {
title: '测试通知!',
subtitle: '字幕',
body: '此处键入正文',
data: {},
iconUri: 'https://example.org',
channel: {
name: '测试频道',
description: '演示应用的测试频道',
importance: {
importance: Importance.IMPORTANCE_LOW,
},
kind: new InformationKind(),
},
sound: false,
},
trigger: null,
});
setResult(`Displayed notification ${id}`);
};
const listLocalNotificationsAsync = async () => {
const res = await Notifications.getPresentedNotificationsAsync();
setResult(JSON.stringify(res, null, 2));
};
const countPresentedNotifications = async () => {
const presentedNotifications =
await Notifications.getPresentedNotificationsAsync();
setResult(`Count: ${presentedNotifications.length}`);
};
const dismissAll = async () => {
await Notifications.dismissAllNotificationsAsync();
setResult('通知已关闭');
};
const dismissSingle = async () => {
const presentedNotifications =
await Notifications.getPresentedNotificationsAsync();
if (!presentedNotifications.length) {
setResult('将不会关闭任何通知');
return;
}
const lastNotificationUuid = presentedNotifications[0].request.identifier;
await Notifications.dismissNotificationAsync(lastNotificationUuid);
setResult(`通知 ${lastNotificationUuid} 已关闭`);
};
return (
<View style={styles.container}>
<Text style={styles.text}>本地通知</Text>
<Button
onPress={presentLocalNotificationAsync}
title="立即显示通知"
/>
<Button
onPress={listLocalNotificationsAsync}
title="列出所有通知"
/>
<Button
onPress={countPresentedNotifications}
title="Count presented notifications"
/>
<Text style={styles.text}>将关闭通知</Text>
<Button onPress={dismissSingle} title="关闭单个通知" />
<Button onPress={dismissAll} title="关闭所有通知" />
<View style={{flex: 1}}>
{result && (
<Text style={styles.text}>
Last Result:{'\n'}
{result}
</Text>
)}
</View>
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: 'white',
},
text: {
color: 'black',
fontSize: 32,
},
});
API参考
请您查看以下专门文档页面,了解有关此库、API参考等的信息: 针对expo-notifications的官方Expo文档(仅提供英文版)。
请注意,本文档适用于所有平台,并且包括Vega不支持的方法。
方法
| 方法 | 描述 | 平台支持 |
|---|---|---|
| addPushTokenListener | 在极少数情况下,推送通知服务可能会在应用运行时更改推送令牌。当令牌更新后,旧令牌将失效,向其发送通知将失败。您可以使用推送令牌侦听器立即在后端注册新令牌,从容地处理这种情况。 | Android、iOS |
| getDevicePushTokenAsync | 返回本机FCM、APNs令牌或可用于其他推送通知服务的PushSubscription数据。 |
Android、iOS |
| getExpoPushTokenAsync | 返回Expo令牌,该令牌可用于使用Expo的推送通知服务向设备发送推送通知。 | Android、iOS |
| removePushTokenSubscription | 移除由addPushTokenListener调用返回的推送令牌订阅。 |
Android、iOS |
| addNotificationReceivedListener | 在应用运行期间,只要收到通知,系统就会调用通过此方法注册的侦听器。 | Android、iOS |
| addNotificationResponseReceivedListener | 每当用户与通知进行交互(例如,点击通知)时,系统将调用通过此方法注册的侦听器。 | Android、iOS |
| addNotificationsDroppedListener | 每当服务器删除某些通知时,系统就会调用通过此方法注册的侦听器。仅适用于我们在Android系统上用作通知服务的Firebase云消息传递。它对应于onDeletedMessages () 回调。您可以在Firebase文档中查看更多信息。 | Android、iOS |
| getLastNotificationResponseAsync | 返回最近收到的通知响应(通知响应表示与通知的互动,例如点击通知)。 | Android、iOS |
| removeNotificationSubscription | 移除由addNotificationListener调用返回的通知订阅。 |
Android、iOS |
| setNotificationHandler | 当在应用运行时收到通知时,您可以使用此函数设置回调,以决定是否应向用户显示通知。 | Android、iOS |
| registerTaskAsync | 当在应用于后台运行时收到通知时,您可以使用此函数设置一个回调,使该回调根据该通知运行。在后台,此函数使用expo-task-manager运行。您必须先使用TaskManager.defineTask定义任务。确保在全局范围内定义该任务。 |
Android、iOS |
| unregisterTaskAsync | 用于取消注册使用registerTaskAsync方法注册的任务。 |
Android、iOS |
| getPermissionsAsync | 调用此函数可检查与通知相关的当前权限设置。它可以让您验证应用当前是否能显示提醒、播放声音等。调用此方法不会对用户方面产生任何效果。 | Android、iOS |
| requestPermissionsAsync | 根据请求提示用户提供通知权限。请求默认要求用户允许显示提醒、设置徽标数量和播放声音。 | Android、iOS |
| getBadgeCountAsync | 获取设备主屏幕上当前设置为应用图标徽标的数值。值为0表示不显示徽标。 |
Android、iOS |
| setBadgeCountAsync | 将应用图标的徽标设置为指定数值。将数值设置为0会清除徽标。在iOS上,此方法要求您已通过requestPermissionsAsync向用户请求了allowBadge的权限,否则它将自动返回false。 |
Android、iOS |
| cancelAllScheduledNotificationsAsync | Cancels all scheduled notifications. | Android、iOS |
| cancelScheduledNotificationAsync | 取消单个计划的通知。给定ID的计划通知不会触发。 | Android、iOS |
| getAllScheduledNotificationsAsync | 获取有关所有计划通知的信息。 | Android、iOS |
| getNextTriggerDateAsync | 允许您检查给定通知触发器输入的下一个触发日期。 | Android、iOS |
| presentNotificationAsync | 将通知规划为立即触发。 | Android、iOS |
| scheduleNotificationAsync | 将通知规划为在将来触发。 | 所有平台 |
| dismissAllNotificationsAsync | 移除通知托盘(通知中心)中显示的所有应用通知。 | 所有平台 |
| dismissNotificationAsync | 移除通知托盘(通知中心)中显示的通知。 | 所有平台 |
| getPresentedNotificationsAsync | 获取有关通知托盘(通知中心)中存在的所有通知的信息。 | 所有平台 |
| deleteNotificationChannelAsync | Removes the notification channel. | Android |
| deleteNotificationChannelGroupAsync | 移除通知通道组和属于该组的所有通知通道。 | Android |
| getNotificationChannelAsync | 获取有关单个通知通道的信息。 | Android |
| getNotificationChannelGroupAsync | 获取有关单个通知通道组的信息。 | Android |
| getNotificationChannelGroupsAsync | 获取有关所有已知通知通道组的信息。 | Android |
| getNotificationChannelsAsync | 获取有关所有已知通知通道的信息。 | Android |
| setNotificationChannelAsync | 将通道配置分配给指定名称的通道(必要时创建)。您可以使用此方法将给定的通知通道分配给通知通道组。 | Android |
| setNotificationChannelGroupAsync | 将通道组配置分配给指定名称的通道组(必要时创建)。 | Android |
| deleteNotificationCategoryAsync | 删除与提供的标识符关联的类别。 | Android、iOS |
| getNotificationCategoriesAsync | 获取有关所有已知通知类别的信息。 | Android、iOS |
| setNotificationCategoryAsync | 设置新的通知类别。 | Android、iOS |
| setAutoServerRegistrationEnabledAsync | 设置注册信息,以便将设备推送令牌推送到给定的注册终端节点。 | Android、iOS |
| unregisterForNotificationsAsync | 让当前设备取消订阅接收推送通知。 | Android、iOS |
挂钩
| 挂钩 | 描述 | 平台支持 |
|---|---|---|
| useLastNotificationResponse | React挂钩总是返回最近收到的通知响应(通知响应表示与通知的互动,例如点击通知)。 | Android、iOS |
实现详情
该库目前无法在Vega虚拟设备上运行。
Vega不支持推送通知、本地通知触发器和应用徽标。
支持的方法列表:
scheduleNotificationAsyncgetPresentedNotificationsAsyncdismissNotificationAsyncdismissAllNotificationsAsync
该程序包还支持Vega通知频道,但是这些频道是在设置通知时提供给scheduleNotificationAsync的,而不是像在Android系统上那样提前创建的。因此,@amazon-devices/expo-notifications中任何与频道相关的方法都未实现。
支持的版本
| NPM程序包版本 | Vega SDK版本 | Vega OS版本 | React Native版本 |
|---|---|---|---|
| ~2.0.0 | 0.23及更早版本 | 操作系统1.1 (1401010009820)及更早版本 |
0.72 |
其他资源
有关其他库的信息,请参阅支持的第三方库和服务。
Last updated: 2026年8月3日

