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

在Google Apps Script中使用Chrome Notifications遇权限拒绝问题求解决

问题根源

Google Apps Script通过HtmlService生成的页面默认嵌入在<iframe>中,其源域名属于Google托管的googleusercontent.com类域名。Chrome的Notification API对跨域iframe有严格权限限制——即便你在主页面设置中允许了通知,iframe内的脚本也无法获取通知权限,最终Notification.permission会一直返回denied,这是浏览器安全策略导致的必然结果。

解决方案与变通方法

1. 用独立弹出窗口加载通知逻辑

弹出窗口属于独立浏览器上下文,不受原iframe的跨域限制,可正常请求和使用通知权限,这是最直接的解决方案:

修改Code.gs

添加参数判断逻辑,根据请求返回对应页面:

function doGet(e) {
  // 处理通知弹出页请求
  if (e.parameter.action === 'openNotification') {
    return HtmlService.createHtmlOutputFromFile("notificationPopup")
      .setTitle("通知权限设置");
  }
  // 返回原页面
  return HtmlService.createTemplateFromFile("notifications")
    .evaluate()
    .setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL);
}

创建notificationPopup.html

专门用于请求权限和发送通知的独立页面:

<!DOCTYPE html>
<html>
<body>
  <script>
    async function initNotification() {
      const permission = await Notification.requestPermission();
      if (permission === 'granted') {
        new Notification('权限已获取', { 
          body: '现在可以接收系统通知了',
          icon: 'https://www.gstatic.com/images/branding/product/1x/google_keep_2020q4_48dp.png'
        });
        // 这里可以添加后续的通知发送逻辑
      } else {
        alert('未获取通知权限,无法接收通知');
      }
    }
    initNotification();
  </script>
</body>
</html>

修改notifications.html

添加触发弹出窗口的按钮:

<!DOCTYPE html>
<html>
<body>
  <button onclick="openNotificationWindow()">请求通知权限</button>
  <script>
    function openNotificationWindow() {
      // 替换为你的GAS部署URL
      const deployUrl = 'https://script.google.com/macros/s/你的部署ID/exec';
      window.open(`${deployUrl}?action=openNotification`, '_blank', 'width=400,height=300');
    }
  </script>
</body>
</html>

2. 使用Google Workspace原生Toast通知(仅限Workspace环境)

如果你的脚本运行在Google Sheets、Docs等Workspace应用内,可以用Google提供的原生Toast通知替代系统通知,这种通知显示在应用界面内,无需浏览器权限:

// 在Code.gs中添加函数
function showToast() {
  SpreadsheetApp.getUi()
    .showToast('这是一条原生通知', '提示', 5); // 5秒后自动消失
}

在前端页面中调用该函数:

<button onclick="google.script.run.showToast()">显示原生通知</button>

3. 注意事项

  • 弹出窗口的部署URL需设置为“任何人,甚至匿名用户”可访问,否则无法正常加载。
  • 标准Notification API和chrome.notifications受相同的跨域iframe限制,都需要通过弹出窗口绕过。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.10 11:30:19