Notifee EventType.PRESS 在 iOS 后台状态下按下通知时未触发。

3
我有一个运行中的react-native应用程序,现在正在集成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",
1个回答

1

我似乎找到了一个解决方案/变通方法。

文档有误导性,特别是这些部分的代码片段:

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秒)。如果有人知道更好的解决方案,或者如果这种处理方式是不正确的,请让我知道!我希望这能帮助那些遇到同样问题的人。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接