Question
I'm developing a Tizen native application that needs to access SMS messages. I'm using the Message API and specifically trying to retrieve all messages from the inbox using messages_foreach_message().
According to the Tizen documentation, this API should fetch all SMS messages in the inbox. However, in my implementation, it only retrieves the last received SMS. Here's my current code:
void messages_search_callback(messages_message_h msg, int index, int result_count, int total_count, void *user_data)
{
dlog_print(DLOG_INFO, "inside message search callback");
}
int ret = messages_foreach_message(Message_service_handler msh, MESSAGES_MBOX_INBOX,
MESSAGES_TYPE_SMS, NULL, NULL, 0, 0, messages_search_callback, NULL);
Could someone help me understand:
- What should be the proper implementation of the callback function to iterate through all SMS messages?
- Why is it only retrieving the last message instead of all messages?
Answer
Problem Understanding
The issue occurs when trying to retrieve all SMS messages from the inbox using messages_foreach_message(). The callback function is being called, but only for the last message instead of all messages in the inbox.
Solution Methods
-
Complete Callback Implementation: The callback function needs proper implementation to handle each message. Currently, it only logs a message without processing the actual message content.
-
Message Storage: You need to store the messages as they are received in the callback. The callback is called for each message, so you should maintain a data structure to collect them.
-
Reference Sample Code: Review the SMS sample application in Tizen Studio for a complete implementation example.
Code Examples
Here's an improved implementation of the callback function:
// Define a structure to store message data
typedef struct {
messages_message_h *messages;
int count;
} message_data;
void messages_search_callback(messages_message_h msg, int index, int result_count, int total_count, void *user_data)
{
message_data *data = (message_data *)user_data;
// Resize the array to accommodate new message
data->messages = realloc(data->messages, (data->count + 1) * sizeof(messages_message_h));
// Store the message handle
data->messages[data->count] = msg;
data->count++;
dlog_print(DLOG_INFO, "Processed message %d of %d", index + 1, total_count);
}
// Usage example
message_data inbox_messages = {NULL, 0};
int ret = messages_foreach_message(msh, MESSAGES_MBOX_INBOX, MESSAGES_TYPE_SMS,
NULL, NULL, 0, 0, messages_search_callback, &inbox_messages);
Additional Tips
- The SMS sample application in Tizen Studio (located in the Sample App List) provides a complete implementation reference.
- Remember to free allocated memory when you're done processing the messages.
- Check error codes returned by
messages_foreach_message()to ensure proper operation. - For the latest documentation, refer to the Samsung Tizen OS website (samsungtizenos.com) as the developer.tizen.org content has been migrated there.