You need to enable JavaScript to run this app. Android 9下编程实现WhatsApp、Instagram等通知自动回复的问题咨询
优惠活动
大模型
产品
解决方案
定价
更多

Android 9下编程实现WhatsApp、Instagram等通知自动回复的问题咨询

报错根因

你遇到的Result key can't be null报错,本质是当前使用的NotificationUtils.getQuickReplyAction方法无法正确匹配到WhatsApp、Instagram通知中携带有效RemoteInput的快速回复Action,拿到的Action对应的RemoteInput未包含合法的result key,因此触发参数非法异常。硬编码取下标取Action的逻辑也不可靠,不同应用、不同通知场景的Action排序会发生变化,很容易取到非快速回复的Action。

修复方案

直接自己实现快速回复Action的匹配逻辑,不要依赖第三方封装的工具类,示例代码如下:

@Override
public void onNotificationPosted(StatusBarNotification sbn) {
    // 过滤仅处理目标应用通知
    String packageName = sbn.getPackageName();
    if (!packageName.equals("com.whatsapp") && !packageName.equals("com.instagram.android")) {
        return;
    }
    Notification notification = sbn.getNotification();
    if (notification.actions == null || notification.actions.length == 0) {
        return;
    }
    // 遍历所有Action查找带RemoteInput的快速回复项
    for (Notification.Action action : notification.actions) {
        RemoteInput[] remoteInputs = RemoteInput.getRemoteInputs(action.actionIntent);
        if (remoteInputs == null || remoteInputs.length == 0) {
            continue;
        }
        RemoteInput targetInput = remoteInputs[0];
        String resultKey = targetInput.getResultKey();
        if (resultKey == null) {
            continue;
        }
        // 构造回复参数
        Intent replyIntent = new Intent();
        Bundle replyData = new Bundle();
        replyData.putString(resultKey, "Hi, there");
        RemoteInput.addResultsToIntent(remoteInputs, replyIntent, replyData);
        // 发送回复
        try {
            action.actionIntent.send(getApplicationContext(), 0, replyIntent);
            Log.i(TAG, "自动回复发送成功");
            return;
        } catch (PendingIntent.CanceledException e) {
            Log.e(TAG, "回复失败:PendingIntent已失效", e);
        }
    }
    Log.i(TAG, "当前通知未找到可用的快速回复入口");
}

注意:上述代码运行的前提是你已经在系统设置的「通知使用权」页面为你的应用开启了权限,否则无法接收到系统的通知回调。

替代实现方案

如果通知快速回复API的适配成本较高,可以采用无障碍服务的方案实现自动回复:

  • 申请无障碍权限,监听聊天页面的界面变化
  • 匹配聊天页面的输入框、发送按钮控件ID,模拟输入内容后点击发送
  • 该方案适配性更强,不受应用是否开放通知快速回复入口的限制,但需要针对应用版本更新维护控件匹配规则,且需要用户授予无障碍权限。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.19 06:15:08