推播通知

您的動作可在適當時機向使用者傳送通知,例如在工作的到期日接近時傳送提醒。

本指南說明如何為您的動作設定推播通知。

支援的介面

推播通知適用於 Android 和 iOS 裝置 (iOS 裝置必須安裝 Google 助理應用程式才能接收推播通知)。聲控音箱、智慧螢幕或其他途徑目前不支援這些功能。

必要條件

您的專案必須包含至少一個全域意圖,當使用者輕觸 Google 助理收到的推播通知時,系統就會叫用該意圖。

立即開始

以下各節說明如何在動作中設定推播通知。

建立觸發意圖

您在本節中建立的意圖會觸發通知流程。如要建立這個意圖,請按照下列步驟操作:

  1. 前往動作控制台,然後按一下頂端選單中的「開發」
  2. 按一下左側選單中的「Intents」(意圖),展開該部分。
  3. 按一下清單底部的 ,然後輸入新意圖的名稱。
  4. 按下 Enter/Return 即可建立新的意圖。
  5. 新增用於觸發通知流程的訓練詞組。例如:

    • Notify me
    • Send notifications
    • Subscribe to notifications
  6. 點按「儲存」

轉換至系統意圖

如要設定轉換至 Notifications 系統場景的轉換作業,請按照下列步驟操作:

  1. 在左選單的「場景」下方,按一下您要新增通知訂閱流程的場景。
  2. 在場景的「User intent process」部分下方,按一下「+」即可新增意圖處理常式。
  3. 在「Intent」(意圖) 下方,選取您在上一節建立的意圖。
  4. 在「轉換」下方,選取「通知」系統場景。

  5. 點按「儲存」

設定系統情境

如要設定 Notifications 系統情境,請按照下列步驟操作:

  1. 在左選單的「Scenes」下方,選取新的「通知」系統場景。
  2. 在「Configure intent」部分下方,按一下「Select intent」
  3. 在「選取意圖」專區下方,選取要在使用者輕觸推播通知時比對的意圖。

  4. 在「自訂選擇接收提示」部分,輸入在使用者要求訂閱推播通知時顯示的提示。提示訊息形式為「我可以傳送 $prompt 的推播通知嗎?」。

  5. 點按「儲存」

設定啟用

選擇接收推播通知的步驟如下:

  1. 在「Scenes」下方選取「通知」系統場景。
  2. 在「條件」下方,選取「如果使用者說「是」
  3. 啟用「呼叫 Webhook」,並提供事件處理常式名稱,例如 subscribe_to_notifications
  4. 啟用「傳送提示」,並提供簡單的提示,讓使用者瞭解應用程式會收到訊息:

    candidates:
    - first simple:
      variants:
      - speech: 'Great, I'll send you notifications.'
    
  5. 在「轉換」下方,選取「結束對話」,在使用者訂閱通知後結束對話。

設定不採用

如要設定停止推播通知,請按照下列步驟操作:

  1. 在「條件」下方,選取「如果使用者說「否」」
  2. 啟用「傳送提示」,並提供簡單的提示,讓使用者瞭解自己不會收到通知:

    candidates:
    - first simple:
      variants:
      - speech: Okay, I won't send you notifications.
    
  3. 當使用者選擇不採用通知後,選取「轉換」下方的「結束對話」即可結束對話。

設定 Webhook

如要設定 Webhook,請按照下列步驟操作:

  1. 在您的 Webhook 中,新增用來儲存 updatesUserId 的意圖處理常式:

    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. 依序按一下「Create credentials」(建立憑證) >「Service account」(服務帳戶)
  5. 輸入服務帳戶名稱,然後按一下「建立」
  6. 從「請選擇角色」下拉式選單中,依序選取「專案」>「擁有者」
  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));
      });
    });