简体   繁体   English

如何发送推送通知-Google Assistant

[英]How to send Push notifications - Google Assistant

  # index.js const functions = require('firebase-functions'); const DialogflowApp = require('actions-on-google').DialogflowApp; exports.handler = functions.https.onRequest((req, res) => { const app = new DialogflowApp({ request: req, response: res }); console.log("Request", req); console.log('Request Processing '); function responseHandler(app) { let intent = app.getIntent(); console.log("INFO Intent : ", intent); switch (intent) { case 'input.welcome': console.log("INFO : UserId: ", app.getUser().userId); app.ask("Welcome to notify Applcation") break; case 'finish_permission': if (app.isPermissionGranted()) { console.log("INFO : UserId: ", app.getUser().userId); app.ask("Ok, I'll start alerting you"); } else { app.ask("Ok, I won't alert you"); } break; case 'check_overdue_tasks': if (app.isPermissionGranted()) { console.log("INFO : UserId: ", app.getUser().userId); app.ask("Ok, I'll start alerting you"); } else { app.ask("Ok, I won't alert you"); } break; case 'setup_update': app.askForUpdatePermission('check_overdue_tasks'); break; } } app.handleRequest(responseHandler); }) ################################################## send ############################################## var request = require('request') const google = require('googleapis'); const key = require('../config/Agent33-e4a3b7e88308.json'); let notif = { userNotification: { title: 'Pay Parking tickets', }, target: { userId: 'ABwppHF74yXbA9Z1ptgyOVwwkU8p9meRgs3H51Aw6_AqQZTzUgFzdz1twy6ki1aI-CjziWJPlqSdJUdbzQ', intent: 'check_overdue_tasks' } } let jwtClient = new google.auth.JWT( key.client_email, null, key.private_key, ['https://www.googleapis.com/auth/actions.fulfillment.conversation'], null ); jwtClient.authorize(function (err, tokens) { if (err) { console.log("ERROR on jwt CLIENT"); } else { console.log("Token : ", JSON.stringify(tokens) + "\\n Notification Msg : " + JSON.stringify(notif)); request.post('https://actions.googleapis.com/v2/conversations:send', { 'auth': { 'bearer': tokens.access_token }, 'json': true, 'body': { 'customPushMessage': notif, 'isInSandbox': true } }, function (err, httpResponse, body) { console.log(httpResponse.statusCode + ': ' + httpResponse.statusMessage) }); } }); ############################################################################################ 

I exactly followed the steps illustrated in documentation : 我完全按照文档中说明的步骤进行操作:

https://developers.google.com/actions/assistant/updates https://developers.google.com/actions/assistant/updates

The issues encountered are : 遇到的问题是:

ISSUE 1 : 问题1:

app.askForUpdatePermission(INTENT) : is not updating the permission to send push notification for the intent but it says the permission is granted. app.askForUpdatePermission(INTENT):未更新用于发送意图的推送通知的权限,但表示已授予该权限。 and in case tried to execute the 'final_permission' intent again, its say the permission is granted . 如果再次尝试执行“ final_permission”意图,则表示已授予许可。

As when I try to execute app.isPermissionGranted() , it returned false. 当我尝试执行app.isPermissionGranted()时,它返回false。

ISSUE 2 : The Server ' https://actions.googleapis.com/v2/conversations:send ' returned 500 or 400 Error randomly. 问题2:服务器“ https://actions.googleapis.com/v2/conversations:send ”随机返回500或400错误。

I am using free plan of FireBase (Spark) Is it because of that ? 我正在使用FireBase(Spark)的免费计划吗?

Make sure you have set exact intentname like setup_update and text should be exact for Suggestion chips. 确保设置了准确的intentname例如setup_update并且文本对于“ Suggestion chips.应该正确Suggestion chips. Otherwise your assistant won't recognize text. 否则您的助手将无法识别文本。

Make sure you have enable webhook for specific intent if you had handle using code. 如果可以使用代码处理,请确保已针对特定意图启用了webhook。

Hope you read and follow their Action On Google document. 希望您阅读并遵循他们的Google行动文档。

Test it on a real device. 在真实设备上进行测试。 You will get a notification. 您会收到通知。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM