推送通知

您的 Action 可以在相关时刻向用户推送通知,例如在任务截止日期临近时发送提醒。

本指南介绍了如何为您的 Action 设置推送通知。

支持的表面

推送通知适用于 Android 和 iOS 设备(iOS 设备必须安装 Google 助理应用才能接收推送通知)。声控音响设备、智能显示屏或其他 surface 目前不支持此类信息。

前提条件

您的项目必须包含至少一个全局 intent,系统会在用户点按从 Google 助理收到的推送通知时调用该全局 intent。

开始使用

以下部分介绍了如何在 Action 中设置推送通知。

创建用于触发的 intent

您在此部分创建的 intent 会触发通知流程。如需创建此 intent,请按以下步骤操作:

  1. 前往 Actions 控制台,然后点击顶部菜单中的 Develop
  2. 点击左侧菜单中的意图 (Intents) 以展开该部分。
  3. 点击列表底部的 ,然后输入新 intent 的名称。
  4. Enter/Return 以创建新的 intent。
  5. 添加用于触发通知流的训练短语。以下是一些示例:

    • Notify me
    • Send notifications
    • Subscribe to notifications
  6. 点击保存

转换到系统 intent

如需设置向 Notifications 系统场景的过渡,请按以下步骤操作:

  1. 在左侧菜单中的 Scenes 下,点击您要添加通知订阅流的场景。
  2. 在场景的用户 intent 处理部分下,点击 + 以添加新的 intent 处理程序。
  3. Intent 下,选择您在上一部分中创建的意图。
  4. Transition 下,选择 Notifications 系统场景。

  5. 点击保存

配置系统场景

如需配置 Notifications 系统场景,请按以下步骤操作:

  1. 在左侧菜单中的 Scenes 下,选择新的 Notifications 系统场景。
  2. Configure intent 部分下,点击 Select intent
  3. Select intent 部分下,选择您希望在用户点按推送通知时匹配的 intent。

  4. 自定义选择启用提示部分,输入在系统要求用户订阅推送通知时向用户显示的提示。提示的形式为“Is it ok if I send push notifications for $prompt”(我能发送 $prompt 推送通知吗)。

  5. 点击保存

配置选择启用

如需配置接收推送通知,请按以下步骤操作:

  1. Scenes 下,选择 Notifications 系统场景。
  2. 条件下方,选择如果用户说“是”
  3. 启用调用 webhook 并提供事件处理程序名称,例如 subscribe_to_notifications
  4. 启用发送提示并提供简单的提示,告知用户他们将收到通知:

    candidates:
    - first simple:
      variants:
      - speech: 'Great, I'll send you notifications.'
    
  5. Transition 下,选择 EndConversation 可在用户订阅通知后结束对话。

配置停用

如需配置停用推送通知,请按以下步骤操作:

  1. 条件下方,选择如果用户说“否”
  2. 启用发送提示,并提供简单的提示来告知用户他们不会发送通知:

    candidates:
    - first simple:
      variants:
      - speech: Okay, I won't send you notifications.
    
  3. Transition 下,选择 EndConversation 可在用户选择停止接收通知后结束对话。

配置网络钩子

如需配置网络钩子,请按以下步骤操作:

  1. 在 webhook 中,添加一个用于存储 updatesUserId 的 intent 处理程序:

    app.handle('subscribe_to_notifications', conv => {
      const intentName = '<name_of_intent_to_trigger>';
      const notificationsSlot = conv.session.params['NotificationSlot_${intentName}'];
      if(notificationsSlot.permissionStatus == 'PERMISSION_GRANTED') {
        const updateUserId = notificationsSlot.additionalUserData.updateUserId;
        // Store the user ID and the notification's target intent for later use.
        // (Use a database, like Firestore, for best practice.)
      }
    });
    

发送通知

推送通知是使用 Actions API 发送给用户的。若要使用此 API,您需要在 Google Cloud 项目中激活此 API,并设置并下载 JSON 服务帐号密钥。

然后,您可以使用 Google OAuth2 客户端库,将服务帐号密钥交换为访问令牌,并使用该令牌验证您向 Actions API 发出的请求。

获取服务账号密钥

  1. 前往 Google API 控制台,从选择项目下拉菜单中选择您的项目。
  2. 点击启用,为您的项目启用 Actions API。
  3. 前往 Google Cloud 控制台凭据页面,从选择项目下拉列表中选择您的项目。
  4. 点击创建凭据 > 服务账号
  5. 输入服务帐号名称,然后点击创建
  6. 选择角色下拉列表中,选择项目 > Owner
  7. 点击继续
  8. 点击创建密钥以下载服务帐号 JSON 文件。

用密钥交换访问令牌并发送通知

如需通过 Actions API 发送推送通知,您需要用服务帐号密钥来换取访问令牌。我们建议您使用 Google API 客户端库执行此操作。在随后的一系列代码段中,我们使用了 Google API Node.js 客户端库。

  1. 安装 Google API 客户端库并请求:

    npm install googleapis request --save

  2. 使用以下代码从服务帐号密钥中获取访问令牌并发送推送通知:

    // Use the Actions API to send a Google Assistant push notification.
    let client = auth.fromJSON(require('./service-account.json'));
    client.scopes = ['https://www.googleapis.com/auth/actions.fulfillment.conversation'];
    let notification = {
      userNotification: {
        title: 'Example notification title',
      },
      target: {
        userId: '<UPDATES_USER_ID>',
        intent: 'Notifications Intent',
      },
    };
    client.authorize((err, tokens) => {
      if (err) {
        throw new Error('Auth error: ${err}');
      }
      request.post('https://actions.googleapis.com/v2/conversations:send', {
        'auth': {
          'bearer': tokens.access_token,
        },
        'json': true,
        'body': {'customPushMessage': notification, 'isInSandbox': true},
      }, (err, httpResponse, body) => {
        if (err) {
          throw new Error('API request error: ${err}');
        }
        console.log('${httpResponse.statusCode}: ' + '${httpResponse.statusMessage}');
        console.log(JSON.stringify(body));
      });
    });