Save message

Learn how to save messages from conversations only for a user.

Settings

  1. Login to the CometChat Dashboard.
  2. Select one of your apps.
  3. On the Extensions page add the Save message extension.

How does it work?

Save message extension provides you the ability to:

  1. Save messages
  2. Unsave messages
  3. Fetch all the saved messages by the user.

Saved messages are private and are visible to the user who has saved them.

1. Save a message

To save a message, use the callExtension method provided by the SDK to make an HTTP POST request with the parameters as shown below.
You need to pass the msgId of the message that has to be saved.

CometChat.callExtension('save-message', 'POST', 'v1/save', {
    "msgId": 111
}).then(response => {
    // { success: true }
})
.catch(error => {
    // Error occured
});
import org.json.simple.JSONObject;

JSONObject body=new JSONObject();
body.put("msgId", 111);

CometChat.callExtension("save-message", "POST", "/v1/save", body,
 new CometChat.CallbackListener < JSONObject > () {
    @Override
    public void onSuccess(JSONObject jsonObject) {
        //On Success
    }
    @Override
    public void onError(CometChatException e) {
        //On Failure
    }
});
CometChat.callExtension(slug: "save-message", type: .post, endPoint: "v1/save", body: ["msgId": 111] as [String : Any], onSuccess: { (response) in
         // Success
      }) { (error) in
         // Error occured
      }

2. Unsave a message

To unsave a message, use the callExtension method provided by the SDK to make an HTTP DELETE request with the parameters as shown below.
You need to pass the msgId of the message that needs to be unsaved.

CometChat.callExtension('save-message', 'DELETE', 'v1/unsave', {
    "msgId": 111
}).then(response => {
    // { success: true }
})
.catch(error => {
    // Error occured
});
import org.json.simple.JSONObject;

JSONObject body=new JSONObject();
body.put("msgId", 111);

CometChat.callExtension("save-message", "DELETE", "/v1/unsave", body,
 new CometChat.CallbackListener < JSONObject > () {
    @Override
    public void onSuccess(JSONObject jsonObject) {
        //On Success
    }
    @Override
    public void onError(CometChatException e) {
        //On Failure
    }
});
CometChat.callExtension(slug: "save-message", type: .delete, endPoint: "v1/unsave", body: ["msgId": 111] as [String : Any], onSuccess: { (response) in
         // Success
      }) { (error) in
         // Error occured
      }

3. Fetch saved messages

To fetch the saved messages for a user, use the callExtension method provided by the SDK to make an HTTP GET request with the query parameters as shown below.

const URL = `v1/fetch`;
CometChat.callExtension('save-message', 'GET', URL, null).then(response => {
    // {savedMessages: []}
})
.catch(error => {
    // Error occured
});
String URL = "/v1/fetch";

CometChat.callExtension("save-message", "GET", URL, null,
new CometChat.CallbackListener < JSONObject > () {
    @Override
    public void onSuccess(JSONObject jsonObject) {
        // {savedMessages: []}
    }
    @Override
    public void onError(CometChatException e) {
        // Some error occured
    }
});
CometChat.callExtension(slug: "save-message", type: .get, endPoint: "/v1/fetch", body: nil, onSuccess: { (response) in
        // {savedMessages: []}
      }) { (error) in
        // Some error occured
      }
    }

Did this page help you?