Samsung Electronics logo

Sending Messages

To create engaging applications with various messaging features, you must learn to create and send messages:

  1. Retrieve the messaging service using the getMessageServices() method.

    The messaging service determine which message types you are handling (SMS, MMS, or email). In the following example, the SMS service is retrieved.

    webapis.messaging.getMessageServices("messaging.sms", serviceListCB, errorCallback);
  2. In the success event handler of the getMessageServices() method, use the Message interface to define the content and attributes of the message, and then send the message using the sendMessage() method of the MessageService interface.

    If the message is not ready to be sent yet, save the message draft using the addDraftMessage() method of the MessageStorage interface.

    function serviceListCB(services)
       /* Define SMS message */
       var msg = new webapis.Message("messaging.sms",
                                      plainBody: "I will arrive in 10 minutes."},
                                      to: ["+34666666666", "+34888888888"]
       if (msgReady)
          /* Send SMS message */
          services[0].sendMessage(msg, messageSent, messageFailed);
          /* Save a draft */
          services[0].messageStorage.addDraftMessage(msg, successCallback, errorCallback); 

    In case you are sending MSM or email messages with attachments, add the attachments as an array of MessageAttachment objects:

    var msg = new webapis.Message("");
    msg.attachments = [new webapis.MessageAttachment("images/myimage.png", "image/png"), 
                       new webapis.MessageAttachment("docs/mydoc.pdf","text/pdf")];
  3. Define the message sending success event handler called if the message is sent successfully.

    For messaging technologies, such as SMS, where the message is sent individually to every message recipient, the success callback must be invoked individually for each recipient.

    function messageSent(recipients) 
       for (var i = 0; i < recipients.length; i++) 
          console.log("The SMS has been sent to " + recipients[i]);

    Define error event handlers for all possible errors and exceptions that can occur causing the message delivery to fail.