How can i send custom data along with push notification? - dialogflow-es

i have one use case in which I need to send custom data along with push notification in Google Assistant using ActionOnSDK , how can I send it anyone has any clue?
let notification = {
userNotification: {
title: 'A new tip is added',
},
target: {
userId: 'abcd1234-EFGH_789',
intent: 'tell_latest_tip',
// Expects a IETF BCP-47 language code (i.e. en-US)
locale: 'en-US'
},
};
request.post('https://actions.googleapis.com/v2/conversations:send', {
'auth': {
'bearer': tokens.access_token,
},
'json': true,
'body': {
'customPushMessage': notification
}
};

Related

FCM notification not sent with messaging.sendAll() function - NodeJS

I'm trying to send multiple notifications using the messaging.sendAll() function of the Firebase Admin SDK.
The way in which I write the messages to send is this:
const messages = [];
const token2 = user2Data.notificationToken;
messages.push({
notification: {
title: `title`,
body: "this is the body!",
icon: "default",
sound: "customNotificationSound.wav",
content_available: "true",
},
data: {
notificationType: "notif2",
uid1: toUser,
uid2: fromUser,
},
token: token2,
});
const token1 = user1Data.notificationToken;
messages.push({
notification: {
title: `title`,
body: "this is the body!",
icon: "default",
sound: "customNotificationSound.wav",
content_available: "true",
},
data: {
notificationType: "notif1",
uid1: toUser,
uid2: fromUser,
},
token: token1,
});
return admin.messaging().sendAll(messages).then((promMes) => {
console.log("messages are sent");
console.log(promMes);
});
I don't know why but the messages to the different devices have not been sent.
I know that because of the promise I receive after I call the method sendAll(). In fact the error I get in the log is this:
textPayload: " { success: false, error: [FirebaseMessagingError] },"
I suppose that the problem hides behind how I construct the message payload but I wouldn't know how to fix it.

format request to send dynamic link push notification nodejs

I am attempting to add dynamic links to push notifications so users tap them and open to a specific app page. I have been following the documentation and the links do not open to the page they should when I tap the push notifications but when tested in the browser the links do open to the correct page. From what I have read, there are 2 possible solutions depending on the version of firebase the project is using. The older "way" is to add a click_action property to the apns payload object, the newer way is to create a webpush object with the fcm_options property set to the value of the link. Neither of these options seems to work regardless of where the objects are placed in the request. Is my request formatted incorrectly or am I possibly missing something? Below is the current request:
const [title, body, deepLink] = titleAndBody;
let accessToken = await getAccessToken();
accessToken = "Bearer " + accessToken;
const options = {
method: "POST",
url: URL,
headers: {
Authorization: accessToken,
"Content-Type": "application/json",
},
body: JSON.stringify({
message: {
token: fcmToken,
notification: {
body: body,
title: title,
},
webpush: {
fcm_options: {
link: deepLink,
}
},
apns: {
headers: {
priority: "10",
},
payload: {
//click_action: deepLink,
aps: {
badge: 0,
mutable_content: 1,
},
},
}
},
}),
};```

Unknown name "time_to_live" at 'message':

I am trying to send push notification via firebase:
await admin.messaging().sendAll({
token: 'some token',
data: {val1: '1', val2: '2'},
time_to_live: 300,
});
I am getting next error:
{"message":"Send push notification failed, Invalid JSON payload
received. Unknown name "time_to_live" at 'message': Cannot find
field.","level":"error"}
In firebase for time_to_live the key is ttl, also I use this request body for sending push notifications in android apps:
const body = {
notification: {
title: 'xxxx',
body: "xxxxxxxx"
},
data: {
notification_message: "xxxxxx"
},
token: 'xxxxxx',
android: {
ttl: 3600,
notification: { icon: 'xxxxx', color: '#b2b2b2' }
}
}
await admin.messaging().send(body)
Use ttl key in case of time_to_live.

How to send FCM push notification to multiple devices using google apis?

I want to send push notification to multiple devices at once. Can i pass array of tokens?
Is it possible to do using google api? Please help me with this.
request(
{
method: 'POST',
uri:
'https://fcm.googleapis.com/v1/projects/projectId/messages:send',
headers: {
'content-type': 'application/json',
Authorization: `Bearer ${token}`,
'apns-priority': 'high',
content_available: true
},
body: {
message: {
token: token,
notification: {
body: 'This is an FCM notification message!',
title: 'FCM Title'
},
android: {
notification: {
sound: 'default'
}
}
}
},
json: true
},
async function(error, response, body) {
if (error) {
console.log(error);
}
console.log(body);
}
);
If you want to make the POST requests by yourself, you can prefer to:
1) Use the legacy HTTP server protocol. With that you will be able to use the "registration_ids" field which expects an array of token strings in POST body. Docs here.
2) Stick to HTTP v1 API that you're using right now, but since "token" field is expecting only string, you can first subscribe your users to a specific topic and use that topic in "topic" field in POST body.
Additionally, you may prefer to use Admin SDK to send batch notifications. You can check for further here.

Can't we use anything other than displayText from webhook response

I am new to api.ai and I was doing the webhook implementation. I've noticed that only the "speech" or "displayText" from webhook response is shown to the user. Is there any technique to use any other params from the response?
I would be glad, if anyone tell me how could I format the response text like making it bold, change font etc.
Thank you!
Note that if the client you are sending the response to (such as Facebook Messenger) does not support special formats such as bold, this is an exercise in futility.
That said, there are many richer types of responses you can send than just plain text, and if you want to do this programmatically rather than build rich responses in API.ai, I'd suggest injecting your custom server side solution between the client and API.ai rather than have it at the end of the process.
In other words:
Client interface <-> Custom solution <-> API.ai
rather than
Client <-> API.ai <-> Custom fulfillment
This gives you more customization and fulfillment options, including building entirely custom response/prompt logic without even hitting the API.ai endpoint, or further editing API returns after they are processed, for example appending a database query result to the text of an API.ai fulfillment.
Assuming you have a setup like this, in node.js a solution for sending more advanced payloads than text to Facebook Messenger would look like this:
//Send gif or image reply
function sendGifMessage(recipientId) {
var messageData = {
recipient: {
id: recipientId
},
message: {
attachment: {
type: "image",
payload: {
url: config.SERVER_URL + "FILE LOCATION"
}
}
}
};
callSendAPI(messageData);
}
// Send custom payload buttons
function sendButtonMessage(recipientId, text, buttons) {
var messageData = {
recipient: {
id: recipientId
},
message: {
attachment: {
type: "template",
payload: {
template_type: "button",
text: text,
buttons: buttons
}
}
}
};
callSendAPI(messageData);
}
// Send quickReply buttons
function sendQuickReply(recipientId, text, replies, metadata) {
var messageData = {
recipient: {
id: recipientId
},
message: {
text: text,
metadata: isDefined(metadata)?metadata:'',
quick_replies: replies
}
};
callSendAPI(messageData);
}
function callSendAPI(messageData) {
request({
uri: 'https://graph.facebook.com/v2.6/me/messages',
qs: {
access_token: config.FB_PAGE_TOKEN
},
method: 'POST',
json: messageData
}, function (error, response, body) {
if (!error && response.statusCode == 200) {
var recipientId = body.recipient_id;
var messageId = body.message_id;
if (messageId) {
console.log("Successfully sent message with id %s to recipient %s",
messageId, recipientId);
} else {
console.log("Successfully called Send API for recipient %s",
recipientId);
}
} else {
console.error("Failed calling Send API", response.statusCode, response.statusMessage, body.error);
}
});
}
Hope that helps

Resources