如何发送推送通知 - Google智能助理

问题描述 投票:7回答:1

   # 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)
        });
    }
});


############################################################################################

我完全按照文档中说明的步骤操作:

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

遇到的问题是:

问题1:

app.askForUpdatePermission(INTENT):不更新为intent发送推送通知的权限,但它表示授予了权限。如果再次尝试执行'final_permission'意图,则表示授予权限。

当我尝试执行app.isPermissionGranted()时,它返回false。

问题2:服务器'https://actions.googleapis.com/v2/conversations:send'随机返回500或400错误。

我正在使用FireBase(Spark)的免费计划是不是因为那个?

firebase push-notification actions-on-google google-home dialogflow
1个回答
0
投票

确保你设置了像intentname这样的精确setup_update,文字应该与Suggestion chips.完全相同,否则你的助手将无法识别文字。

如果您使用代码处理,请确保已启用特定意图的webhook。

希望您阅读并遵循他们的Action On Google文档。

在真实设备上测试它。你会收到通知。

© www.soinside.com 2019 - 2024. All rights reserved.