Worklight: Push message formatting - ios

In Worklight, I have setup Push message for iOS and it works fine. Now for testing purpose, when i am sending push via URL call then the message title comes correctly while the body (payload) part truncates all spaces and shows all words together.
For example:
http://mydomain/myApp/invoke?adapter=aaPushAdapter&procedure=sendPush&parameters=["aahad","General Title 2", "This is General message body 2"]
then , title comes as "General Title 2" and the body part comes as "ThisisGeneralmessagebody2"
My Adapter is declared as:
function sendPush(userId, msgTitle, MsgContents){
var userSubscription = WL.Server.getUserNotificationSubscription('aaPushAdapter.PushEventSource', userId);
if (userSubscription==null){
return { result: "No subscription found for user :: " + userId };
}
WL.Server.notifyAllDevices(userSubscription, {
badge: 1,
sound: "sound.mp3",
activateButtonLabel: "Read",
alert: msgTitle,
payload: {
msg : MsgContents
}
});
return { result: "Notification sent to user :: " + userId };
}
(1) Now how I can preserve this formatting ?
(2) If i have to send URL then how i format and send my message?
Please suggest. Thanks

If %20 does not work, then change all spaces to something like '|', and then unencode this in your app. Or hex encode the whole string so it's one continuous alphanumeric string.

I am not entirely sure how you are using the URL as a means to send the push notification. Do you mean that you actually go to the browser and type this text in the address bar...? You're not supposed to do that (for other than quick tests). There are backend systems that should do that for you.
Anyway, instead of the space between words, use "%20" (w/out the quotation marks) and see if the text is then displayed with spaces in the dialog.

Related

Include multiple incoming SMS messages/responses with Twilio functions

I'm working on a project now within Twilio, using Twilio Functions, where I'm trying to set up SMS messaging so that if we receive an incoming keyword, we respond with a specific message, including a URL. The plan is to have multiple incoming keywords, with different responses so if someone sends an SMS to one of our numbers, depending on that key word, we respond with a basic message and a URL. I'm trying to figure out the best way to handle this within Twilio Functions.
I have this working for a single incoming keyword/response, as seen below.
if (incomingMessage.includes('testpark')) {
twiml.message('StartMyParking:\n\nTo start your parking, please click this link: https://blahblah.com');
} else if (incomingMessage.includes('bye')) {
twiml.message('Goodbye!');
} else {
twiml.message('Please check your zone/code and try again.');
}
While that works, I want to add in more incoming words, along with responses, such as an incoming message of 'testpark2' and a response of 'StartMyParking:\n\nTo start your parking, please click this link: https://blahblah2.com'.
Then I would want to include another one with 'testpark3' and a response of 'StartMyParking:\n\nTo start your parking, please click this link: https://blahblah3.com' and so on, all within the same script.
Can someone help me understand how to achieve this?
There are a lot of ways to achieve your desired outcome, but here's the most straightforward to begin with.
Instead of creating an else if statement for every possible keyword, you could define the keyword/response pairs up front using a JavaScript Map.
The keys of the Map will be your keywords, the values of the Map will be your responses:
const keywordResponseMap = new Map([
['testpark2', 'StartMyParking:\n\nTo start your parking, please click this link: https://blahblah2.com'],
['testpark3', 'StartMyParking:\n\nTo start your parking, please click this link: https://blahblah3.com'],
['testpark', 'StartMyParking:\n\nTo start your parking, please click this link: https://blahblah.com'],
]);
const keywords = Array.from(keywordResponseMap.keys());
let keyword;
if (incomingMessage.includes('bye')) {
twiml.message('Goodbye!');
}
else if (keyword = keywords.find(k => incomingMessage.includes(k))) {
const response = keywordResponseMap.get(keyword);
twiml.message(response);
} else {
twiml.message('Please check your zone/code and try again.');
}
Also note that I'm putting the bye case up front because it is more performant than looking for the keywords in the incomingMessage, thus you avoid unnecessarily doing that processing when a user says bye.
You can use find to search for any keyword that is in the incomingMessage, then you can use the keyword that you found to retrieve the response from the map.
If your response will always be the same except for the URL, you could further optimize this by only storing the URL in the map and using string interpolation like this:
const keywordUrlMap = new Map([
['testpark2', 'https://blahblah2.com'],
['testpark3', 'https://blahblah3.com'],
['testpark', 'https://blahblah.com'],
]);
const keywords = Array.from(keywordUrlMap.keys());
let keyword;
if (incomingMessage.includes('bye')) {
twiml.message('Goodbye!');
}
else if (keyword = keywords.find(k => incomingMessage.includes(k))) {
const url = keywordUrlMap.get(keyword);
twiml.message(`StartMyParking:\n\nTo start your parking, please click this link: ${url}`);
} else {
twiml.message('Please check your zone/code and try again.');
}
It is also important to note that I'm putting testpark last in the map because testpark matches to testpark2 and testpark3. If you'd put it first, it would always resolve to testpark even with a user submits testpark2 or similar values.
Also, I'm using the Map type because it guarantees the order in which the keys are returned, which is again important for the previous point.
When you have a lot more keywords and responses, you may have to start looking at a solution to store them externally like a database, and query the database by keyword to resolve the response.
Good luck, we can't wait to see what you build!

Double encoding of HTML with third party integration tool

We integrate a third-party app, using a javascript code that we need to send an email and name.
This email and name come from a previous step registration form.
Because we need to print on the page that email and name we encode it.
some.survey({
email: "test#test.pt",
name: "Mon & Sons",
properties: {"one":"123","two":"345"}
});
The issue is that the third party before printing is encoding again our string, showing in the browser like: "Mon & Sons".
Does anyone know how to get around this?
Here's a way (based on https://stackoverflow.com/a/784698):
function DecodeHTML(txt) {
var el = document.createElement("div");
el.innerHTML = txt;
return el.innerText;
};
DecodeHTML("Mon & Sons");
//"Mon & Sons"
Edit: watch out for XSS attacks, however. It is possible that the third-party app is doing this to escape any ' into ' and " into ", so you may want to build on the above idea to prevent this, depending on your needs. For example, if you're going to display this information on a HTML page, do so by only setting innerText of an element in that page, e.g. nameelement.innerText = nametext;
sbgib was almost right about how this would work. So, we now output the string as encoded, and decode it with Javascript, before sending it to the external service.
function DecodeHTML(txt) {
var el = document.createElement("div");
el.innerHTML = txt;
return el.innerText;
}
some.survey({
email: "test#test.pt",
name: DecodeHTML("Mon & Sons"),
properties: {"one":"123","two":"345"}
});

navigator.share() doesn't work as expected in iOS email

I'm trying to implement a share button on my website, but I'm having problems with iOS Safari.
var title = "Subject";
var text = "Body";
var url = "https://google.com";
if (navigator.share) {
navigator.share({
title: title,
text: text,
// url: url,
})
.then(() => console.log('Successful share'))
.catch((error) => console.log('Error sharing', error));
}
else{
window.open("mailto:?subject="+encodeURIComponent(title)+"&body="+encodeURIComponent(text)+" "+encodeURIComponent(url), '_blank');
}
It works fine on android, but if I try to send an email from an iPad the subject is not set, and when I send the email the subject becomes the whole message. I suspect the iOS' mail app doesn't receive the data in the same way as the android's gmail app.
Is it a known issue? Is there a workaround?
Thank you!
If you only pass the title navigator.share({ title, }) you'll find that in Apple's Mail, it becomes the body of the email, not the subject. This is contrary to Gmail on Android which uses the title to populate the subject.
Unfortunately, the spec doesn't spell out how the title should be used:
title member The title of the document being shared. May be ignored by the target.
See this article for more nuances and examples of navigator.share in iOS/Mac OS.`

Google Sheet `MailApp.sendEmail`: how to create multi-line message body?

I am looking for a simple answer. I have created a script to send an email through Google Sheet Script Editor:
function mailtest() { MailApp.sendEmail("name.surname#etteachers.com",
"Availability Change",
"Hi Please note that...."); }
The body of the email however keeps all the text in one row starting at the top of the email body. How do I get the part "Please note that" two rows below "Hi" in the email?
Add two new lines to your message like this:
function mailtest() {
MailApp.sendEmail(""name.surname#etteachers.com", "Availability Change", "Hi\n\nPlease note that....");
}

Use forge.tabs.open on a URL returned from an API call

I'm hitting an API that return a list of URLs, so I'm looking to iterate through them, generate links, and let the user browse to those links. I think I'm supposed to use forge.tabs.open to create a Modal view when the user taps a link. Here's the code:
$("#feed").append('<p>'+item.data.title+'</p>');
And the viewLink function:
var viewLink = function(linkurl, linktitle) {
forge.logging.log(linkurl);
forge.logging.log(linktitle);
forge.tabs.openWithOptions({
url: linkurl,
title: linktitle,
buttonText: "close"
});
};
It doesn't work on iOS and doesn't generate an error. When I run it in my browser, I get this error:
Uncaught SyntaxError: Unexpected token :
Any ideas what I'm doing wrong?
The trigger.io code you posted looks fine to me. When I see the "unexpected token" syntax error I immediately think: single quote, double quote, or character encoding.
Do any of the linktitle's have a "weird" character? Maybe you need to escape or encodeURIComponent or decodeURIComponent it?

Resources