org.symbian.tools.wrttools.doc.WebDeveloper/html/GUID-DBF41DB5-44D7-4822-A611-FBF7DA3A8F3B.html
changeset 229 716254ccbcc0
equal deleted inserted replaced
228:913c9751c067 229:716254ccbcc0
       
     1 
       
     2 <!DOCTYPE html
       
     3   PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
       
     4 <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/><meta name="copyright" content="(C) Copyright 2009"/><meta name="DC.rights.owner" content="(C) Copyright 2009"/><meta name="DC.Type" content="mobileconcept"/><meta name="DC.Title" content="Messaging Service API example"/><meta name="DC.Relation" scheme="URI" content="GUID-1F17637B-4439-4FFE-AFD8-93D84B01666A"/><meta name="DC.Relation" scheme="URI" content="GUID-7C69DDA4-16F1-4A8F-BDB2-4CB0015B4E81"/><meta name="DC.Relation" scheme="URI" content="GUID-65AAF569-D347-462B-B59A-9D7CA184AB9C"/><meta name="DC.Relation" scheme="URI" content="GUID-91579EE5-A608-401C-82B4-DCF1723EC7B5"/><meta name="DC.Format" content="XHTML"/><meta name="DC.Identifier" content="GUID-DBF41DB5-44D7-4822-A611-FBF7DA3A8F3B"/><title>Messaging Service API example </title><script type="text/javascript">
       
     5       function initPage() {}
       
     6     </script><link href="../PRODUCT_PLUGIN/book.css" rel="stylesheet" type="text/css"/><link href="css/s60/style.css" rel="stylesheet" type="text/css" media="all"/></head><body onload="initPage();"><div class="body"><div class="contentLeft prTxt"><h1 class="pageHeading" id="GUID-DBF41DB5-44D7-4822-A611-FBF7DA3A8F3B">Messaging Service API example</h1><div>
       
     7 <p/>
       
     8 <p>This section presents the full source code of a working sample widget
       
     9 for the <a href="GUID-2240F5BE-97BD-4CB3-8DB3-B5CF7C23BF54.html#GUID-2240F5BE-97BD-4CB3-8DB3-B5CF7C23BF54">Messaging Service</a>.
       
    10 You can download the <code>wgz</code> package for this widget from
       
    11 section <a href="GUID-775005BC-2FF8-45A9-BBA6-6CED6B5780A2.html#GUID-775005BC-2FF8-45A9-BBA6-6CED6B5780A2">Example widgets</a>.</p>
       
    12 <p>For general information about creating widgets, see section <a href="GUID-0E3095DB-03FF-4240-83F2-6D876AD2083A.html#GUID-0E3095DB-03FF-4240-83F2-6D876AD2083A">Widget component files</a>.</p>
       
    13 <p>For widget development and debugging purposes, this example writes its
       
    14 output to <code>c:\data\jslog_widget.log</code> using <code>console.info</code>.
       
    15 For instructions on how to enable logging in the Web browser for S60, see
       
    16 section <a href="GUID-B584CA90-543B-4AED-B134-A3A616259DB9.html#GUID-B584CA90-543B-4AED-B134-A3A616259DB9">JavaScript console</a>.</p>
       
    17 <div><h3>Info.plist</h3>
       
    18 <pre class="codeblock" id="GUID-A2EB8D22-1BB0-48BB-9696-25A307FD7695">&lt;?xml version="1.0" encoding="UTF-8"?&gt;
       
    19 &lt;!DOCTYPE plist PUBLIC "-//Nokia//DTD PLIST 1.0//EN" "http://www.nokia.com/NOKIA_COM_1/DTDs/plist-1.0.dtd"&gt;
       
    20 &lt;plist version="1.0"&gt;
       
    21 &lt;dict&gt;
       
    22   &lt;key&gt;DisplayName&lt;/key&gt;
       
    23   &lt;string&gt;MessagingSample&lt;/string&gt;
       
    24   &lt;key&gt;Identifier&lt;/key&gt;
       
    25   &lt;string&gt;com.nokia.widget.sapi.Messaging.sample&lt;/string&gt;
       
    26   &lt;key&gt;Version&lt;/key&gt;
       
    27   &lt;string&gt;1.0&lt;/string&gt;
       
    28   &lt;key&gt;MainHTML&lt;/key&gt;
       
    29   &lt;string&gt;messaging-sample.html&lt;/string&gt;
       
    30 &lt;/dict&gt;
       
    31 &lt;/plist&gt;</pre>
       
    32 </div>
       
    33 <div><h3>messaging-sample.html</h3>
       
    34 <pre class="codeblock" id="GUID-CB207FE7-3656-4F42-9320-FD5CB36DCD95">&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"&gt;
       
    35 &lt;html&gt;
       
    36 &lt;head&gt;
       
    37   &lt;meta http-equiv="content-type" content="text/html; charset=utf-8"&gt;
       
    38   &lt;script type="text/javascript" src="js/messaging-sample.js" charset="utf-8"&gt;&lt;/script&gt;
       
    39   &lt;script type="text/javascript" src="js/common.js" charset="utf-8"&gt;&lt;/script&gt;
       
    40   &lt;/head&gt;
       
    41 &lt;body onload='setup()' bgcolor="#ddeeff"&gt;
       
    42 
       
    43   &lt;form name="frm"&gt;
       
    44     &lt;h3&gt;Messaging Service API Sample Widget&lt;/h3&gt;
       
    45     &lt;input type="button" onclick="getList('img1')" value="GetList"&gt;&lt;img id="img1" src="pic/blank.png" width="25" height="25" align="center"&gt;&lt;br&gt;
       
    46     &lt;input type="button" onclick="SendMMS('img2')" value="SendMmsSync"&gt;&lt;img id="img2" src="pic/blank.png" width="25" height="25" align="center"&gt;&lt;br&gt;
       
    47     &lt;input type="button" onclick="SendASyncMMS('img3')" value="SendMmsAsync"&gt;&lt;img id="img3" src="pic/blank.png" width="25" height="25" align="center"&gt;&lt;br&gt;
       
    48     &lt;input type="button" onclick="cancelSendASyncMMS('img4')" value="cancelSendMmsAsync"&gt;&lt;img id="img4" src="pic/blank.png" width="25" height="25" align="center"&gt;&lt;br&gt;
       
    49     &lt;input type="button" onclick="Delete('img5')" value="RegisterNotification"&gt;&lt;img id="img5" src="pic/blank.png" width="25" height="25" align="center"&gt;&lt;br&gt;
       
    50     &lt;input type="button" onclick="ChangeStatus('img6')" value="DeleteMsg"&gt;&lt;img id="img6" src="pic/blank.png" width="25" height="25" align="center"&gt;&lt;br&gt;
       
    51     &lt;input type="button" onclick="RegisterNotification('img7')" value="RegisterNotification"&gt;&lt;img id="img7" src="pic/blank.png" width="25" height="25" align="center"&gt;&lt;br&gt;
       
    52     &lt;input type="button" onclick="CancelNotification('img8')" value="DeleteMsg"&gt;&lt;img id="img8" src="pic/blank.png" width="25" height="25" align="center"&gt;&lt;hr&gt;
       
    53     &lt;div class='messaging' id='messaging' bgcolor="#ddeeff" style=width:100%;height:100%;overflow:auto&gt;
       
    54     &lt;/div&gt;
       
    55   &lt;/form&gt;
       
    56 
       
    57 &lt;/body&gt;
       
    58 &lt;/html&gt;</pre>
       
    59 </div>
       
    60 <div><h3>common.js</h3>
       
    61 <pre class="codeblock" id="GUID-9630DCB3-6F70-4F07-BB14-DC8B5CA50740">// common.js
       
    62 //
       
    63 // This file contains some utility functions
       
    64 
       
    65 // Check the error code and show the information to users
       
    66 function checkError(message, resultList, divId, imgId)
       
    67 {
       
    68   var errCode = resultList.ErrorCode;
       
    69   var msg = "";
       
    70 
       
    71   if (errCode) {
       
    72     msg = message + "&lt;BR&gt;" + "Failed Error: " + errCode + "&lt;BR&gt;";
       
    73     if(resultList.ErrorMessage != undefined)
       
    74       msg += "Error Message: " + resultList.ErrorMessage;
       
    75     showIMG(imgId,"no");
       
    76   } else {
       
    77     showIMG(imgId,"yes");
       
    78   }
       
    79 
       
    80   //print error message
       
    81   if(divId != null &amp;&amp; divId != undefined)
       
    82     document.getElementById(divId).innerHTML = msg;
       
    83   console.info(msg);
       
    84 
       
    85   return errCode;
       
    86 }
       
    87 
       
    88 // Build the message by reading a iteratorable list in a recursive manner
       
    89 function showIterableList(iterator)
       
    90 {
       
    91   var msg = "";
       
    92   try
       
    93   {
       
    94     iterator.reset();
       
    95     var item;
       
    96     while (( item = iterator.getNext()) != undefined ){
       
    97       msg += showObject( item );
       
    98     }
       
    99   }
       
   100   catch(e)
       
   101   {
       
   102     alert('&lt;showIterableList&gt; ' + e);
       
   103   }
       
   104   return msg;
       
   105 }
       
   106 
       
   107 // Build the message by reading a JS object in a recursive manner
       
   108 function showObject( obj )
       
   109 {
       
   110   var txt = "";
       
   111   try {
       
   112     if ( typeof obj != 'object' )
       
   113       return "" + obj + '&lt;BR/&gt;';
       
   114     else {
       
   115       for(var key in obj) {
       
   116         txt +=  key + ":";
       
   117         txt += showObject( obj[key] );
       
   118         txt += '&lt;BR/&gt;';
       
   119       }
       
   120       txt += '&lt;BR/&gt;';
       
   121     }
       
   122   }
       
   123   catch (e)
       
   124   {
       
   125     alert("showObject: " + e);
       
   126   }
       
   127   return txt;
       
   128 }
       
   129 
       
   130 // Show the image to indicate the test result
       
   131 function showIMG(imgId, isOK)
       
   132 {
       
   133   if(imgId == null || imgId == undefined)
       
   134     return;
       
   135 
       
   136   if(isOK == "yes")
       
   137     document.getElementById(imgId).src = "pic/yes.png";
       
   138   else if(isOK == "no")
       
   139     document.getElementById(imgId).src = "pic/no.png";
       
   140   else
       
   141     document.getElementById(imgId).src = "pic/blank.png";
       
   142 }
       
   143 
       
   144 // Show elements in object by using 'alert'
       
   145 function testObject(obj)
       
   146 {
       
   147   var msg = "";
       
   148   for(var key in obj) {
       
   149     msg = msg + ":" + key + "=" + obj[key];
       
   150   }
       
   151   alert(msg);
       
   152 }
       
   153 
       
   154 // Test whether the input is numeric
       
   155 function IsNumeric(sText)
       
   156 {
       
   157   var ValidChars = "0123456789.";
       
   158   var IsNumber=true;
       
   159   var Char;
       
   160 
       
   161   for (i = 0; i &lt; sText.length &amp;&amp; IsNumber == true; i++)
       
   162   {
       
   163     Char = sText.charAt(i);
       
   164     if (ValidChars.indexOf(Char) == -1)
       
   165     {
       
   166       IsNumber = false;
       
   167     }
       
   168   }
       
   169   return IsNumber;
       
   170 }</pre>
       
   171 </div>
       
   172 <div><h3>messaging-sample.js</h3>
       
   173 <pre class="codeblock" id="GUID-40320252-5A48-451C-976A-AD31ACC3FE6C">// messaging-sample.js
       
   174 //
       
   175 // In this sample Messaging will be send, deleted, and listed.
       
   176 // Also, async operation will be canceled
       
   177 
       
   178 //SAPI Error Codes
       
   179 // 0    - Success
       
   180 // 1000 - InvalidServiceArgument
       
   181 // 1001 - UnknownArgumentName
       
   182 // 1002 - BadArgumentType
       
   183 // 1003 - MissingArgument
       
   184 // 1004 - ServiceNotSupported
       
   185 // 1005 - ServiceInUse
       
   186 // 1006 - ServiceNotReady
       
   187 // 1007 - NoMemory
       
   188 // 1008 - HardwareNotAvailable
       
   189 // 1009 - ServerBusy
       
   190 // 1010 - EntryExists
       
   191 // 1011 - AccessDenied
       
   192 // 1012 - NotFound
       
   193 // 1013 - UnknownFormat
       
   194 // 1014 - GeneralError
       
   195 // 1015 - CancelSuccess
       
   196 // 1016 - ServiceTimedOut
       
   197 // 1017 - PathNotFound
       
   198 
       
   199 // Declare the service object
       
   200 var so;
       
   201 
       
   202 // id of the div used to display information
       
   203 const DIV_ID = 'messaging';
       
   204 
       
   205 // imgid for callback1 function
       
   206 var imgid_callback1;
       
   207 
       
   208 // imgid for callback2 function
       
   209 var imgid_callback2;
       
   210 
       
   211 // Test entry point
       
   212 function setup()
       
   213 {
       
   214   try
       
   215   {
       
   216     so = device.getServiceObject("Service.Messaging", "IMessaging");
       
   217       console.info("setup: so: %s", so);
       
   218   }
       
   219   catch(e)
       
   220   {
       
   221     alert('&lt;setup&gt; ' +e);
       
   222   }
       
   223 }
       
   224 
       
   225 //Get message list
       
   226 function getList(imgId)
       
   227 {
       
   228   // Setup input params using dot syntax
       
   229   var criteria = new Object();
       
   230   criteria.Type = 'Inbox';
       
   231   criteria.Filter = new Object();
       
   232   criteria.Filter.MessageTypeList = new Array();
       
   233   criteria.Filter.MessageTypeList[0] = 'MMS';
       
   234   criteria.Filter.MessageTypeList[1] = 'SMS';
       
   235 
       
   236   try
       
   237   {
       
   238     // Messaging supports synchronous call
       
   239     var result = so.IMessaging.GetList(criteria);
       
   240     if(!checkError("IMessaging::getList",result,DIV_ID,imgId)) {
       
   241       document.getElementById(DIV_ID).innerHTML = showIterableList(result.ReturnValue);
       
   242     }
       
   243   }
       
   244   catch(e)
       
   245   {
       
   246     showIMG(imgId,"no");
       
   247     alert('getList: '+e);
       
   248   }
       
   249 }
       
   250 
       
   251 //send MMS message
       
   252 // Sync operation
       
   253 function SendMMS(imgId)
       
   254 {
       
   255     // Setup input params using dot syntax
       
   256     var criteria = new Object();
       
   257     criteria.MessageType = 'MMS';
       
   258     criteria.To = '4567809';
       
   259     criteria.BodyText ='Hello:TestSendSync: Sending message using SAPI';
       
   260     try
       
   261     {
       
   262       result = so.IMessaging.Send(criteria);
       
   263     checkError("IMessaging::SendMMS",result,DIV_ID,imgId);
       
   264     }
       
   265   catch(e)
       
   266   {
       
   267     showIMG(imgId,"no");
       
   268     alert('SendMMS: '+e);
       
   269   }
       
   270 }
       
   271 
       
   272 //send MMS message
       
   273 // Async operation
       
   274 function SendASyncMMS(imgId)
       
   275 {
       
   276   // Setup input params using dot syntax
       
   277   var criteria = new Object();
       
   278   criteria.MessageType = 'MMS';
       
   279   criteria.To = '9876543210';
       
   280   criteria.BodyText = 'TestSendASync: Sending message using SAPI';
       
   281 
       
   282   try
       
   283   {
       
   284     imgid_callback1 = imgId;
       
   285     result = so.IMessaging.Send(criteria, callback1);
       
   286     if(!checkError("IMessaging::SendASyncMMS",result,DIV_ID,imgId)) {
       
   287       showIMG(imgId,"");
       
   288     }
       
   289   }
       
   290   catch(e)
       
   291   {
       
   292     showIMG(imgId,"no");
       
   293     alert('SendASyncMMS: '+e);
       
   294   }
       
   295 }
       
   296 
       
   297 // cancel send MMS message
       
   298 // Async operation
       
   299 function cancelSendASyncMMS(imgId)
       
   300 {
       
   301   // Setup input params using dot syntax
       
   302   var criteria = new Object();
       
   303   criteria.MessageType = 'MMS';
       
   304   criteria.To = '9876543210';
       
   305   criteria.BodyText = 'TestSendASync: Sending message using SAPI';
       
   306 
       
   307   try
       
   308   {
       
   309     imgid_callback1 = imgId;
       
   310     result = so.IMessaging.Send(criteria, callback1);
       
   311     if(!checkError("IMessaging::cancelSendASyncMMS",result,DIV_ID,imgId)) {
       
   312       showIMG(imgId,"");
       
   313       var criteria2 = new Object();
       
   314       criteria2.TransactionID = result.TransactionID;
       
   315       var result2 = so.IMessaging.Cancel(criteria2);
       
   316       checkError("IMessaging::cancelSendASyncMMS",result2,DIV_ID,imgId); 
       
   317     }
       
   318   }
       
   319   catch(e)
       
   320   {
       
   321     showIMG(imgId,"no");
       
   322     alert('cancelSendASyncMMS: '+e);
       
   323   }
       
   324 }
       
   325 
       
   326 // function deletes message with specified MessageId
       
   327 // sync operation
       
   328 function Delete(imgId)
       
   329 {
       
   330   var msgIdStr = prompt("Please type the message id", "0");
       
   331   if(msgIdStr == "" || msgIdStr == null)
       
   332     return;
       
   333 
       
   334   // Setup input params using dot syntax
       
   335   var msgId = parseInt(msgIdStr);
       
   336   var criteria = new Object();
       
   337   criteria.MessageId = msgId;
       
   338 
       
   339   // function sends message
       
   340   try
       
   341   {
       
   342     var result = so.IMessaging.Delete(criteria);
       
   343     checkError("IMessaging::Delete",result,DIV_ID,imgId);
       
   344   }
       
   345   catch(e)
       
   346   {
       
   347     showIMG(imgId,"no");
       
   348     alert('Delete: '+e);
       
   349   }
       
   350 }
       
   351 
       
   352 // function changes status of message
       
   353 // Sync operation
       
   354 function ChangeStatus(imgId)
       
   355 {
       
   356   var msgIdStr = prompt("Please type the message id", "0");
       
   357   if(msgIdStr == "" || msgIdStr == null)
       
   358     return;
       
   359 
       
   360   var criteria = new Object();
       
   361   var msgId = parseInt(msgIdStr);
       
   362   criteria.MessageId = msgId;
       
   363   criteria.Status = 'Unread';
       
   364 
       
   365   try
       
   366   {
       
   367     var result = so.IMessaging.ChangeStatus(criteria);
       
   368     checkError("IMessaging::ChangeStatus",result,DIV_ID,imgId);
       
   369   }
       
   370   catch(e)
       
   371   {
       
   372     showIMG(imgId,"no");
       
   373     alert('ChangeStatus: '+e);
       
   374   }
       
   375 }
       
   376 
       
   377 // function cancels notification of new messages.
       
   378 // sync operation
       
   379 function CancelNotification(imgId)
       
   380 {
       
   381   var criteria = new Object();
       
   382   criteria.Type = 'NewMessage';
       
   383   try
       
   384   {
       
   385     var result = so.IMessaging.CancelNotification(criteria);
       
   386     checkError("IMessaging::CancelNotification",result,DIV_ID,imgId);
       
   387   }
       
   388   catch(e)
       
   389   {
       
   390     showIMG(imgId,"no");
       
   391     alert('CancelNotification: '+e);
       
   392   }
       
   393 }
       
   394 
       
   395 // function registers for notification of new messages
       
   396 // Sync operation
       
   397 // to test RegisterNotification(): 1) click on RegisterNotification 2) click on sendMMS button to get callback
       
   398 function RegisterNotification(imgId)
       
   399 {
       
   400   var criteria = new Object();
       
   401   criteria.Type = 'NewMessage';
       
   402   try
       
   403   {
       
   404     imgid_callback2 = imgId;
       
   405     // Register for notification
       
   406     result = so.IMessaging.RegisterNotification(criteria, callback2);
       
   407     if(!checkError("IMessaging::RegisterNotification",result,DIV_ID,imgId)) {
       
   408       showIMG(imgId,"");
       
   409     }
       
   410   }
       
   411   catch(e)
       
   412   {
       
   413     showIMG(imgId,"no");
       
   414     alert('RegisterNotification: '+e);
       
   415   }
       
   416 }
       
   417 
       
   418 function callback1(transId, eventCode, result)
       
   419 {
       
   420   console.info("callback1: transId: %d  eventCode: %d result.ErrorCode: %d", transId, eventCode, result.ErrorCode);
       
   421   if(!checkError("IMessaging::SendASyncMMS",result,DIV_ID,imgid_callback1)) {
       
   422     document.getElementById(DIV_ID).innerHTML = showIterableList(result.ReturnValue);
       
   423   }
       
   424 }
       
   425 
       
   426 function callback2(transId, eventCode, result)
       
   427 {
       
   428   console.info("callback2: transId: %d  eventCode: %d result.ErrorCode: %d", transId, eventCode, result.ErrorCode);
       
   429   if(!checkError("IMessaging::SendASyncMMS",result,DIV_ID,imgid_callback2)) {
       
   430     document.getElementById(DIV_ID).innerHTML = "Messaging status was changed";
       
   431   }
       
   432 }</pre>
       
   433 </div>
       
   434 </div></div></div><div class="footer"><hr/><div class="copy">© Nokia 2009.</div></div></body></html>