我有一个可用的反应原生应用程序,我现在正在将 Notifee 集成到其中。我目前只优先考虑 iOS 平台,因此假设仅使用 iOS 来解决问题的其余部分。预先感谢您!
在我的index.js中,在注册App组件之前,我设置了
onBackgroundEvent
事件监听器,根据文档:这是正确的
import {AppRegistry} from 'react-native';
import App from './App';
import {name as appName} from './app.json';
import notifee, {EventType} from '@notifee/react-native';
notifee.onBackgroundEvent(async ({type, detail}) => {
console.log('onBackgroundEvent', event);
if (event.type === EventType.PRESS) {
console.log('User pressed the notification.', event.detail.pressAction?.id);
}
});
AppRegistry.registerComponent(appName, () => App);
当我的应用程序处于后台状态时从服务器发送远程通知然后按下通知时,我仅看到以下日志:
LOG handleBackgroundMessage
LOG onBackgroundEvent 3 // 3 === EventType.DELIVERED
因此后台事件侦听器已正确设置,但
EventType.PRESS
未按预期触发。我只收到 EventType.DELIVERED 事件。
这是我用来显示通知的代码:
const handleBackgroundMessage = async message => {
console.log('handleBackgroundMessage');
await notifee.requestPermission();
// Display a notification
const notificationPayload = {
title: message.data.title + ' pdosprewq',
body: message.data.body,
};
await notifee.displayNotification(notificationPayload);
};
我搜索了 Github 问题和 notifee 文档,似乎没有任何记录任何额外的实现来接收
EventType.PRESS
。如有任何帮助,我们将不胜感激!
package.json:
"@notifee/react-native": "^7.7.1",
"react": "18.2.0",
"react-native": "0.71.8",
我似乎找到了解决方案/解决方法。
文档具有误导性,尤其是这些部分中的代码片段:
https://notifee.app/react-native/docs/ios/interaction#press-action
https://notifee.app/react-native/docs/events#app-open-events (我尝试了引导程序序列,但也没有收到初始通知)。
我仍然没有在后台事件处理程序中收到
EventType.PRESS
,但我终于能够在前台事件处理程序中收到它了。
const App: React.FC = () => {
useEffect(() => {
return notifee.onForegroundEvent(({type, detail}) => {
switch (type) {
case EventType.PRESS:
console.log('User pressed notification', detail.notification);
break;
}
});
}, []);
...
这似乎比后台事件处理程序慢一点,因为从应用程序加载到收到 PRESS 事件有延迟(约 1 秒)。如果有人知道更好的解决方案,或者这是处理此问题的错误方法,请告诉我!我希望这可以帮助那些正在解决同样问题的人。
实现killed状态下的后台通知。
Android - https://notifee.app/react-native/docs/events#app-open-events
IOS - https://notifee.app/react-native/docs/events#background-events
对于被杀状态。