| ID | CS001242 | Creation date | December 18, 2008 |
| Platform | S60 5th Edition | Tested on devices | |
| Category | Web Runtime (WRT) | Subcategory | Messaging |
| Keywords (APIs, classes, methods, functions): device.getServiceObject(), Service.Messaging.Send(), Service.Messaging.GetList(), |
This code snippet shows how to send MMS messages synchronously and asynchronously and attach images to messages using the Messaging and Media Management Platform Services for S60 Web Runtime introduced in S60 5th Edition.
To obtain access to the service object for the Messaging Service API, the device.getServiceObject("Service.Messaging", "IMessaging") method is used.
To obtain access to the service object for the Media Management Service API, the device.getServiceObject("Service.MediaManagement", "IDataSource") method is used.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<script type="text/javascript" src="sendMessage.js" />
<title></title>
</head>
<body>
<div id="bodyContent" class="bodyContent">
<input type="checkbox" id="async" />
<label for="async">Send asynchronously</label><br />
<label for="phoneNumber">Phone number:</label><br />
<input type="text" id="phoneNumber" size="12" maxlength="12" />
<br />
<label for="message">Message:</label><br />
<textarea id="message" cols="40" rows="3"></textarea><br />
<label for="fileList">Attachments:</label><br />
<select size="5" id="fileList" multiple></select><br />
<input type="button" value="Send" onclick="sendMMS();" />
</div>
</body>
</html>
var messagingServiceObj = null;
var multimediaServiceObject = null;
window.onload = init;
// Initializes the widget
function init() {
try {
messagingServiceObj =
device.getServiceObject("Service.Messaging", "IMessaging");
multimediaServiceObject =
device.getServiceObject("Service.MediaManagement", "IDataSource");
//Setting the criteria for fetching images
var criteria = new Object();
criteria.Type = "FileInfo";
criteria.Filter = new Object();
criteria.Filter.FileType = "Image";
criteria.Sort = new Object();
criteria.Sort.Key = "FileSize";
result =
multimediaServiceObject.IDataSource.GetList(criteria, callback);
} catch (ex) {
alert("Error in initializing the widget");
return;
}
}
/**
* A callback function used to handle results of fetching multimedia files.
* @param transId A number representing the transaction that called the
* callback
* @param eventCode A number representing the callback return status
* @param result An object for holding the callback return value
*/
function callback(transId, eventCode, result) {
if(result.ErrorCode != 0) {
alert("Error in creating file list:" + result.ErrorCode);
return;
}
createFileList(result.ReturnValue);
document.getElementById("fileList").options[0].focus();
}
/**
* Creates a list item for every multimedia file in "iterator".
* @param iterator The list of files
*/
function createFileList(iterator) {
var fileList = document.getElementById("fileList");
//Reset to set pointer to the first element.
iterator.reset();
var item;
//Cleaning the file list.
while (fileList.length != 0) {
fileList.remove(0);
}
while ((item = iterator.getNext()) != undefined) {
var node = document.createElement("option");
//Value of option consists of the full path.
node.value = item.FileNameAndPath;
node.appendChild(document.createTextNode(item.FileName +
item.FileExtension));
fileList.appendChild(node);
}
}
function sendMMS() {
var criteria = new Object();
//Setting the type of the message
criteria.MessageType = "MMS";
var phoneNumber = document.getElementById("phoneNumber").value;
if (phoneNumber != null) {
//Setting the "To" field of the message, can't be empty
criteria.To = phoneNumber;
} else {
alert("Phone number is empty");
return;
}
var messageText = document.getElementById("message").value;
if (messageText != null) {
//Setting the body text field of the message, can't be empty
criteria.BodyText = messageText;
} else {
alert("Text is empty");
return;
}
var fileList = document.getElementById("fileList");
//Adding the selected items as attachments of the message
for (var i = 0; i < fileList.options.length; i++) {
if (fileList.options[i].selected) {
criteria.Attachment = fileList.options[i].value;
}
}
try {
if (document.getElementById("async").checked == false) {
//Send the message synchronously
var result = messagingServiceObj.IMessaging.Send(criteria);
checkError(result);
} else {
//Send the message asynchronously
messagingServiceObj.IMessaging.Send(criteria, onSendDone);
}
} catch(exception) {
alert("SendMMS error: " + exception);
}
}
// Called when asynchronous message sending has been completed
function onSendDone(transId, eventCode, result) {
checkError(result);
}
function checkError(error) {
if (error.ErrorCode != 0) {
alert("Error in sending message");
} else {
alert("Message was sent succesfully");
}
}
You can view the source file and the executable application in the attached ZIP archive. The archive is available for download at Media:Sending_mms_messages_in_WRT.zip.
No related wiki articles found