How to send Push notifications - Google Assistant
Asked Answered
R

1

8

   # 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

The issues encountered are :

ISSUE 1 :

app.askForUpdatePermission(INTENT) : is not updating the permission to send push notification for the intent but it says the permission is granted. and in case tried to execute the 'final_permission' intent again, its say the permission is granted .

As when I try to execute app.isPermissionGranted() , it returned false.

ISSUE 2 : The Server 'https://actions.googleapis.com/v2/conversations:send' returned 500 or 400 Error randomly.

I am using free plan of FireBase (Spark) Is it because of that ?

Ry answered 5/12, 2017 at 5:44 Comment(8)
If you can show the code you used to do both, we can probably help you better.Logography
Thanks , i added the codeRy
If you're using Firebase you need to setup billing to make outbound network calls but the googleapis.com domain should work without needing billing because its inside Google's networkNeom
okay let me check , thanksRy
Even i checked again I couldn't find any issue , why the server is returning 500 or 400 errorRy
Did you succeed ? In my case after : app.askForUpdatePermission('check_overdue_tasks'); the simulator is returning 'Sorry, I didn't get any response' . Any idea ? should we use API V2 for notification ?Along
No, maybe its because of beta version. the issues still persist.Ry
Test it on a real device. You will get notification. Make sure you have done proper setup as per (developers.google.com/actions/assistant/updates) link.Aunt
A
0

Make sure you have set exact intentname like setup_update and text should be exact for Suggestion chips. Otherwise your assistant won't recognize text.

Make sure you have enable webhook for specific intent if you had handle using code.

Hope you read and follow their Action On Google document.

Test it on a real device. You will get a notification.

Aunt answered 26/12, 2017 at 11:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.