如何使用自定义有效负载来个性化AoG推送通知

时间:2019-02-19 07:39:27

标签: actions-on-google

我已经实现了AoG操作,该操作正在发送推送通知(https://developers.google.com/actions/assistant/updates/notifications)。我有一个推送通知,即确实有一次征求用户许可+有一个用于自定义通知的Aog Intent处理程序。问题是我想在不同的上下文中使用推送通知,因此在创建推送通知时需要传递一些自定义有效负载,以便我可以在意图处理程序中区分什么是上下文以及如何对其进行操作。

显而易见的解决方案是使用不同的自定义意图配置多个推送通知,但这需要分别请求它们的许可,由于最终用户的经验,这是不可接受的。

我尝试采用与仓库https://github.com/actions-on-google/actionssdk-updates-nodejs/blob/master/functions/aog-webhook.js类似的方法来申请AoG每日更新,即在注册更新时添加参数:

arguments: [{name: Parameters.CATEGORY, textValue: category}]

然后在意图处理程序中捕获它

function tellRandomTip(conv, category) {
  console.log('tellRandomTip for category - ' + category);
  return getRandomTip(category)
  .then(renderTip.bind(null, conv))
  .then(() => {
    if (!conv.user.storage[DAILY_NOTIFICATION_ASKED]) {
      conv.ask(new Suggestions(DAILY_NOTIFICATION_SUGGESTION));
      conv.user.storage[DAILY_NOTIFICATION_ASKED] = true;
    }
  });
}

不幸的是,这不起作用,参数从不传递

我的实现如下:

启用了推送通知的意图定义

  {
    "description": "Generic AoG push command intent",
    "name": "AoG Push",
    "fulfillment": {
      "conversationName": "myhome-cc-google-assistant"
    },
    "intent": {
      "name": "custom.intent.AoGPush",
      "parameters": [{
        "name": "commandText",
        "type": "org.schema.type.Text"
      }],
      "trigger": {
        "queryPatterns": [
          "$org.schema.type.Text:commandText"
        ]
      }
    }
  }   

通过以下方式寻求许可

this.conv.ask(
 new UpdatePermission({
   intent: 'custom.intent.AoGPush',
   arguments: [{
   name: 'commandText',
   textValue: 'list sections' 
 }]
})

用户点击通知后,意图处理器进行处理

  async aogPushHandler (conv: ActionsSdkConversation, commandText: string) {  
      // do something with custom payload in commandText
     // unfortunatelly commandText is always empty/undefined! 
  }

最后通过代码发送推送通知

        const jwtClient = new google.auth.JWT(
            config.pushAoG.clientEmail, undefined, config.pushAoG.privateKey,
            ['https://www.googleapis.com/auth/actions.fulfillment.conversation'],
            undefined
        )

        const tokens: Credentials = await jwtClient.authorize()
        // code to retrieve target userId and intent
        const notifPayload = {
            userNotification: {
                title: title,
            },
            target: {
                userId: userId,
                intent: 'custom.intent.AoGPush',
                locale: locale
            }
        }

        const httpClient = new HttpClient()

        const payload = {
            customPushMessage: notifPayload
        }
        const bearer = 'Bearer ' + tokens.access_token

        const headers = { Authorization: bearer }
        await httpClient.sendPost('https://actions.googleapis.com/v2/conversations:send', payload, headers)
        logger.debug('push notification sent for user ' + userId)

有什么方法可以在发送通知时放置自定义有效负载,然后在我的意图处理程序(aogPushHandler)中进行检索吗?当我尝试将任意自定义有效负载添加到以上的notifPayload中时,我收到HTTP 400,看来Google对发送推送通知期间有效负载中传递的数据进行了严格的验证。

0 个答案:

没有答案