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

使用Amazon SNS与aws-amplify时前台推送通知无响应问题求助

排查Amazon SNS前台推送通知无响应问题(基于aws-amplify)

针对你遇到的Notifications.Push.onNotificationReceivedInForeground监听器失效问题,结合后台/终止状态正常的情况,按以下方向排查:

  • 检查SNS消息的Payload格式
    前台推送对Payload结构有明确要求,SNS发送时需根据平台(iOS/Android)封装正确格式:

    • 若用JSON格式发送,需将MessageStructure设为json,并分别指定各平台的Payload,示例:
      {
        "APNS": "{\"aps\":{\"alert\":\"前台测试通知\",\"sound\":\"default\"}}",
        "GCM": "{\"notification\":{\"title\":\"测试\",\"body\":\"前台推送测试\"},\"data\":{\"customKey\":\"value\"}}"
      }
      
    • 避免发送纯文本消息,部分平台前台不会自动触发监听器捕获这类消息。
  • 验证权限与平台配置

    • Android 13+需额外申请POST_NOTIFICATIONS权限,确认handlePermissions中是否包含该权限的请求;
    • iOS端检查Info.plist,确保UIBackgroundModes包含remote-notification,同时确认用户授予的通知权限是包含alert的完整权限(而非仅badge/sound);
    • 升级aws-amplify到最新稳定版,部分旧版本存在前台监听器的已知bug。
  • 调试监听器与日志

    • 在myNotificationReceivedHandler中添加日志输出,确认监听器是否真的未触发:
      const myNotificationReceivedHandler = (notification) => {
        console.log('前台通知已接收:', notification);
        // 后续处理逻辑
      };
      
    • 开启Amplify调试日志,查看推送流程中的细节:
      import { Amplify } from 'aws-amplify';
      Amplify.Logger.LOG_LEVEL = 'DEBUG';
      
      通过控制台日志确认是否有消息到达的记录,或是否存在权限/初始化错误。
  • 调整Push服务的启用时机
    确保Notifications.Push.enable()在用户授权通知权限之后执行,避免因权限未获取导致服务未正确初始化:

    useEffect(() => {
      handlePermissions().then(() => {
        Notifications.Push.enable();
        // 注册监听器
        const listenerToken = Notifications.Push.onTokenReceived(myTokenReceivedHandler);
        const listenerForeground = Notifications.Push.onNotificationReceivedInForeground(myNotificationReceivedHandler);
        const listenerOpened = Notifications.Push.onNotificationOpened(myNotificationOpenedHandler);
    
        return () => {
          listenerToken.remove();
          listenerForeground.remove();
          listenerOpened.remove();
        };
      });
    }, []);
    
  • 确认SNS端点状态
    虽然后台能接收,但仍需检查SNS中对应设备的端点状态为Active,且订阅的主题正确,避免因端点异常导致前台消息被拦截。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.10 11:04:56