Subscribe for incoming messages

This is a piece of code showing how to register callback for incoming messages. PRIVILEGE NEEDED: http://tizen.org/privilege/message.read
#include <messages.h>
#include <stdlib.h>

messages_service_h msg_service = NULL;

//Callback to be executed each time a message comes.
void on_message_received(messages_message_h incoming_msg, void *user_data)
{
    LOGI("Message received!");
    
	char *message_txt = NULL;
    
	if(messages_get_text(incoming_msg, &message_txt) == MESSAGES_ERROR_NONE)
    {
        LOGI("Message text: %s", message_txt);
    }
    free(message_txt);
}

//Registering for incoming message callback (for example on your application startup)
if (messages_open_service(&msg_service) == MESSAGES_ERROR_NONE)
{
    LOGI("Messaging service opened!");
}
else
{
    LOGE("Could not open messaging service!");
}
if (messages_set_message_incoming_cb(msg_service, on_message_received, NULL) == MESSAGES_ERROR_NONE)
{
    LOGI("Callback registered");
}
else
{
    LOGE("Could not register callback!");
}

//When needed you can unset the callback and close the messaging service
if (messages_unset_message_incoming_cb(msg_service) == MESSAGES_ERROR_NONE)
{
    LOGI("Callback unregistered!");
}
if (messages_close_service(msg_service) == MESSAGES_ERROR_NONE)
{
    LOGI("Messaging service closed!");
    msg_service = NULL;
}

Responses

0 Replies