How to show an alert
Using Urban Airship you can fire off an alert when you get a push message:
function messageCallback(e) {
Ti.API.info(JSON.stringify(e.data));
}
The above gives an alert but as a string of values. One of those values is called "alert" and gives the actual alert text I want to display.
How do I display that text instead of the entire string coming in?
For example, If I send an push message it would come up in the alert box as:
ALERT
{"alert" : "Hello from Urban Airship!"}
What I want is just:
ALERT
Hello from Urban Airship!
So what should I do?
I tried this:
function messageCallback(e) {
var alertdata = JSON.parse(e.data);
alert(alertdata.alert);
But the alert never fires.
1 Answer
-
Steve
Firstly, I think it's fine to use the alert() function for testing, but be careful not to let two of these dialogs appear on screen at the same time, because it will cause an exception. Instead, try Ti.UI.AlertDialog.
Regarding your question, it's really a shot in the dark, but what do you get if you run this?
function messageCallback(e) { Ti.API.info("The alert is: " + e.alert); }
If the correct message is output to the log, then use this code:
function messageCallback(e) { Ti.API.info("The alert is: " + e.alert); var alertDialog = Ti.UI.createAlertDialog({ title: "Alert", message: "The alert is: " + e.alert, buttonNames: ['OK'], cancel:0 }); alertDialog.show(); }
Let me know the result, and I will suggest something else if it doesn't work.