You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在notifee.onBackgroundEvent中点击通知打开应用(非默认pressAction)

在notifee后台事件中处理通知点击打开应用的实现

首先要明确:点击通知主体触发的是EventType.PRESS事件,而点击通知上的操作按钮触发的是EventType.ACTION_PRESS事件。你当前的代码只处理了按钮点击逻辑,只需新增对EventType.PRESS的判断,就能实现点击通知打开应用的需求。

修改后的完整代码

import notifee, { EventType } from '@notifee/react-native';
import AsyncStorage from '@react-native-async-storage/async-storage';

notifee.onBackgroundEvent(async ({ type, detail }) => {
  // 处理通知主体点击,打开应用
  if (type === EventType.PRESS) {
    // 可选:点击后移除通知栏中的该通知
    await notifee.cancelNotification(detail.notification.id);
    // 调用notifee提供的跨平台方法打开应用
    await notifee.openApp();
  }
  // 保留原有的通知按钮点击逻辑
  else if (type === EventType.ACTION_PRESS && detail.pressAction.id === 'accept') {
    await notifee.cancelNotification(detail.notification.id);
    await AsyncStorage.setItem('called', 'true');
    console.log('accepted');
  }
  else if (type === EventType.ACTION_PRESS && detail.pressAction.id === 'decline') {
    await notifee.cancelNotification(detail.notification.id);
    await AsyncStorage.setItem('called', 'false');
    console.log('declined');
  }
});

关键说明

  • notifee.openApp()是官方提供的跨平台方法,无需区分Android/iOS,直接调用即可唤起应用。
  • 取消通知的代码是可选的,如果你希望用户点击通知后,通知栏里的对应通知自动消失,就保留这行;不需要的话可以直接移除。

内容的提问来源于stack exchange,提问作者user19434921

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 16:02:14