npm install @capacitor/local-notifications
import { LocalNotifications } from '@capacitor/local-notifications';
LocalNotifications.schedule({
notifications: [
{
title: '您有一条新消息',
body: '点击查看详情',
id: 1,
smallIcon: 'assets/icons/notification.png',
actionTypeId: 'view_notification_action', // 自定义的动作类型 ID
extra: {
data: { message: '具体的消息内容' } // 附带的数据
},
schedule: { // 定时设置,可选
at: new Date(Date.now() + 1000 * 5) // 5 秒后触发
}
}
]
});
import { PluginListenerHandle } from '@capacitor/core';
let notificationActionListener: PluginListenerHandle;
notificationActionListener = LocalNotifications.addListener('localNotificationActionPerformed', (notificationAction) => {
if (notificationAction.actionId === 'view_notification_action') {
console.log('用户点击了查看详情的按钮');
console.log('附带的数据:', notificationAction.notification.extra.data);
}
});
通过以上步骤,就可以实现中文本地通知。需注意的是,部分 Android 手机可能需要在系统设置中开放通知权限才能正常接收通知。