Push Notification Service

Push Notification Service for the IoT provides a complete Push Notification Management for all the connected devices. It supports Send Push Message to all channels and all users.

Not setup your Raspberry Pi yet? Click here to follow the step-by-step guide

Import Statement

#include "App42API.h"  

Initialize

In order to use various functions available in a specific service, a developer has to initialize with App42API by passing the apiKey and secretKey which will be provided after the app creation from AppHQ dashboard.

Required Parameters

apiKey - The Application key given when the application was created. secretKey - The secret key corresponding to the application key given when the application was created.
App42API::Initialize("API_KEY", "SECRET_KEY");  

Build Service

After initialization, the developer needs to call the buildXXXService method on App42API to get the instance of the particular API that they wish to build. For example, to build an instance of PushNotificationService, buildPushNotificationService() method needs to be called.

PushNotificationService *pushService = App42API::BuildPushNotificationService();

Create Channel for App

Creates Channel for app on which user can subscribe and get the notification for that particular channel.

Required Parameters

channel - Channel name which you want to create. description - Description for that channel.
const char* channel = "News Channel";
const char* description = "Channel all about the news";
PushNotificationService::Initialize("API_KEY","SECRET_KEY");
PushNotificationService *pushService = App42API::BuildPushNotificationService();
Sample_Class *sampleClassInstance = new Sample_Class();
pushService->CreateChannel(channel,description, app42callback(Sample_Class::onPushRequestCompleted, sampleClassInstance));

void Sample_Class::onPushRequestCompleted(void *response)
{
    App42PushNotificationResponse *pushResponse = (App42PushNotificationResponse*)response;
    printf("\ncode=%d",pushResponse->getCode());
    printf("\nResponse Body=%s",pushResponse->getBody().c_str());
    if (pushResponse->isSuccess)
    {
        for(std::vector<App42PushNotification>::iterator it = pushResponse->notifications.begin(); it != pushResponse->notifications.end(); ++it)
        {
            for(std::vector<App42Channel>::iterator channel = it->channelArray.begin(); channel != it->channelArray.end(); ++channel)
            {
                printf("\n Channel Name=%s",channel->channelName.c_str());
                printf("\n Description=%s\n",channel->description.c_str());
            }
        }
    }
    else
    {
        printf("\nerrordetails:%s",pushResponse->errorDetails.c_str());
        printf("\nerrorMessage:%s",pushResponse->errorMessage.c_str());
        printf("\nappErrorCode:%d",pushResponse->appErrorCode);
        printf("\nhttpErrorCode:%d",pushResponse->httpErrorCode);
    }
}

Subscribe to Channel

User will be subscribed to the selected existing Channel.

Required Parameters

channel - Name of the channel which you want to subscribe. userName - Name of the user which you want to subscribe for the channel.
const char* userName = "Nick";
const char* channelName = "News Channel";
App42API::Initialize("API_KEY", "SECRET_KEY");
PushNotificationService *pushService = App42API::BuildPushNotificationService();
Sample_Class *sampleClassInstance = new Sample_Class();
pushService->SubscribeToChannel(channelName,userName, app42callback(Sample_Class::onPushRequestCompleted, sampleClassInstance));
void Sample_Class::onPushRequestCompleted(void *response)
{
    App42PushNotificationResponse *pushResponse = (App42PushNotificationResponse*)response;
    printf("\ncode=%d",pushResponse->getCode());
    printf("\nResponse Body=%s",pushResponse->getBody().c_str());
    if (pushResponse->isSuccess)
    {
        for(std::vector<App42PushNotification>::iterator it = pushResponse->notifications.begin(); it != pushResponse->notifications.end(); ++it)
        {
            printf("\n UserName=%s",it->userName.c_str());
            for(std::vector<App42Channel>::iterator channel = it->channelArray.begin(); channel != it->channelArray.end(); ++channel)
            {
                printf("\n Channel Name=%s",channel->channelName.c_str());
            }
        }
    }
    else
    {
        printf("\nerrordetails:%s",pushResponse->errorDetails.c_str());
        printf("\nerrorMessage:%s",pushResponse->errorMessage.c_str());
        printf("\nappErrorCode:%d",pushResponse->appErrorCode);
        printf("\nhttpErrorCode:%d",pushResponse->httpErrorCode);
    }
}

Unsubscribe from Channel

User will be subscribed to the selected existing Channel.

Required Parameters

channel - Name of the channel which you want to unsubscribe. userName - Name of the user which you want to unsubscribe from the channel.
const char* userName = "Nick";
const char* channelName = "News Channel";
App42API::Initialize("API_KEY", "SECRET_KEY");
PushNotificationService *pushService = App42API::BuildPushNotificationService();
Sample_Class *sampleClassInstance = new Sample_Class();
pushService->UnsubscribeFromChannel(channelName,userName, app42callback(Sample_Class::onPushRequestCompleted, sampleClassInstance));
void Sample_Class::onPushRequestCompleted(void *response)
{
    App42PushNotificationResponse *pushResponse = (App42PushNotificationResponse*)response;
    printf("\ncode=%d",pushResponse->getCode());
    printf("\nResponse Body=%s",pushResponse->getBody().c_str());
    if (pushResponse->isSuccess)
    {
        for(std::vector<App42PushNotification>::iterator it = pushResponse->notifications.begin(); it != pushResponse->notifications.end(); ++it)
        {
            printf("\n UserName=%s",it->userName.c_str());
            for(std::vector<App42Channel>::iterator channel = it->channelArray.begin(); channel != it->channelArray.end(); ++channel)
            {
                printf("\n Channel Name=%s",channel->channelName.c_str());
            }
        }
    }
    else
    {
        printf("\nerrordetails:%s",pushResponse->errorDetails.c_str());
        printf("\nerrorMessage:%s",pushResponse->errorMessage.c_str());
        printf("\nappErrorCode:%d",pushResponse->appErrorCode);
        printf("\nhttpErrorCode:%d",pushResponse->httpErrorCode);
    }
}

Send Push Message to Channel

Send push message to a particular channel.

Required Parameters

channel - Name of the channel for which you want to send message. message - Message which you want to send.
const char* channelName = "News Channel";
const char* message = "Message which you have to send";
App42API::Initialize("API_KEY", "SECRET_KEY");
PushNotificationService *pushService = App42API::BuildPushNotificationService();
Sample_Class *sampleClassInstance = new Sample_Class();
pushService->SendPushMessageToChannel(channelName, message,app42callback(Sample_Class::onPushRequestCompleted, sampleClassInstance));
void Sample_Class::onPushRequestCompleted(void *response)
{
    App42PushNotificationResponse *pushResponse = (App42PushNotificationResponse*)response;
    printf("\ncode=%d",pushResponse->getCode());
    printf("\nResponse Body=%s",pushResponse->getBody().c_str());
    if (pushResponse->isSuccess)
    {
        for(std::vector<App42PushNotification>::iterator it = pushResponse->notifications.begin(); it != pushResponse->notifications.end(); ++it)
        {
            printf("\n Message=%s\n",it->message.c_str());
            for(std::vector<App42Channel>::iterator channel = it->channelArray.begin(); channel != it->channelArray.end(); ++channel)
            {
                printf("\n Channel Name=%s",channel->channelName.c_str());
            }
        }
    }
    else
    {
        printf("\nerrordetails:%s",pushResponse->errorDetails.c_str());
        printf("\nerrorMessage:%s",pushResponse->errorMessage.c_str());
        printf("\nappErrorCode:%d",pushResponse->appErrorCode);
        printf("\nhttpErrorCode:%d",pushResponse->httpErrorCode);
    }
}

Send Push Message to All

Send push message to all your users.

Required Parameters

message - Message which you want to send.
const char* message = "Message which you have to send";
App42API::Initialize("API_KEY", "SECRET_KEY");
PushNotificationService *pushService = App42API::BuildPushNotificationService();
Sample_Class *sampleClassInstance = new Sample_Class();
pushService->SendPushMessageToAll(message,app42callback(Sample_Class::onPushRequestCompleted, sampleClassInstance));
void Sample_Class::onPushRequestCompleted(void *response)
{
    App42PushNotificationResponse *pushResponse = (App42PushNotificationResponse*)response;
    printf("\ncode=%d",pushResponse->getCode());
    printf("\nResponse Body=%s",pushResponse->getBody().c_str());
    if (pushResponse->isSuccess)
    {
        for(std::vector<App42PushNotification>::iterator it = pushResponse->notifications.begin(); it != pushResponse->notifications.end(); ++it)
        {
            printf("\n Message=%s\n",it->message.c_str());
            printf("\n Expiry=%s\n",it->expiry.c_str());
        }
    }
    else
    {
        printf("\nerrordetails:%s",pushResponse->errorDetails.c_str());
        printf("\nerrorMessage:%s",pushResponse->errorMessage.c_str());
        printf("\nappErrorCode:%d",pushResponse->appErrorCode);
        printf("\nhttpErrorCode:%d",pushResponse->httpErrorCode);
    }
}

Send Push Message to All by Type

Send push message to all by platform type which is subscribed such as Win/Android/iOS

Required Parameters

message - Message which you want to send. deviceType - Type of device for which you want to send a message.
const char* message = "Message which you have to send";
DeviceType deviceType = IOS;
App42API::Initialize("API_KEY", "SECRET_KEY");
PushNotificationService *pushService = App42API::BuildPushNotificationService();
Sample_Class *sampleClassInstance = new Sample_Class();
pushService->SendPushMessageToAllByType(message,deviceType,app42callback(Sample_Class::onPushRequestCompleted, sampleClassInstance));

void Sample_Class::onPushRequestCompleted(void *response)
{
    App42PushNotificationResponse *pushResponse = (App42PushNotificationResponse*)response;
    printf("\ncode=%d",pushResponse->getCode());
    printf("\nResponse Body=%s",pushResponse->getBody().c_str());
    if (pushResponse->isSuccess)
    {
        for(std::vector<App42PushNotification>::iterator it = pushResponse->notifications.begin(); it != pushResponse->notifications.end(); ++it)
        {
            printf("\n Message=%s\n",it->message.c_str());
            printf("\n Expiry=%s\n",it->expiry.c_str());
            printf("\n Type  = %s",it->type.c_str());
        }
    }
    else
    {
        printf("\nerrordetails:%s",pushResponse->errorDetails.c_str());
        printf("\nerrorMessage:%s",pushResponse->errorMessage.c_str());
        printf("\nappErrorCode:%d",pushResponse->appErrorCode);
        printf("\nhttpErrorCode:%d",pushResponse->httpErrorCode);
    }
}

Send Custom Push Message to Channel in Key Value or JSON Format

Send push message to channel containing JSON. On receiving this message on the device, it has to be parsed and accessed accordingly.

Required Parameters

channel - Name of the channel for which you want to send message. message - Message which you want to send.
const char* channelName = "News Channel";
map<string, string> messageMap;
messageMap["alert"] = "Hello! This is my first notification.";
messageMap["sound"] = "default";
messageMap["badge"] = "1";
App42API:initialize("API_KEY","SECRET_KEY")
local pushNotificationService  = App42API:buildPushNotificationService()
App42API::Initialize("API_KEY", "SECRET_KEY");
PushNotificationService *pushService = App42API::BuildPushNotificationService();
Sample_Class *sampleClassInstance = new Sample_Class();
pushService->SendPushMessageToChannel(channelName, messageMap,app42callback(Sample_Class::onPushRequestCompleted, sampleClassInstance));
void Sample_Class::onPushRequestCompleted(void *response)
{
    App42PushNotificationResponse *pushResponse = (App42PushNotificationResponse*)response;
    printf("\ncode=%d",pushResponse->getCode());
    printf("\nResponse Body=%s",pushResponse->getBody().c_str());
    if (pushResponse->isSuccess)
    {
        for(std::vector<App42PushNotification>::iterator it = pushResponse->notifications.begin(); it != pushResponse->notifications.end(); ++it)
        {
            printf("\n UserName=%s",it->userName.c_str());
            printf("\n Message=%s\n",it->message.c_str());
            printf("\n Expiry=%s\n",it->expiry.c_str());
            printf("\n Type  = %s",it->type.c_str());
            printf("\n DeviceToken  = %s",it->deviceToken.c_str());

            for(std::vector<App42Channel>::iterator channel = it->channelArray.begin(); channel != it->channelArray.end(); ++channel)
            {
                printf("\n Channel Name=%s",channel->channelName.c_str());
                printf("\n Description=%s\n",channel->description.c_str());
            }
        }
    }
    else
    {
        printf("\nerrordetails:%s",pushResponse->errorDetails.c_str());
        printf("\nerrorMessage:%s",pushResponse->errorMessage.c_str());
        printf("\nappErrorCode:%d",pushResponse->appErrorCode);
        printf("\nhttpErrorCode:%d",pushResponse->httpErrorCode);
    }
}

Send Push Message to User

Send push message to a particular user.

Required Parameters

userName - Name of the user which you want to send the message. message - Message which you want to send.
const char* userName = "Nick";
const char* message = "Message which you have to send";
App42API::Initialize("API_KEY", "SECRET_KEY");
PushNotificationService *pushService = App42API::BuildPushNotificationService();
Sample_Class *sampleClassInstance = new Sample_Class();
pushService->SendPushMessageToUser(userName,message,app42callback(Sample_Class::onPushRequestCompleted, sampleClassInstance));
void Sample_Class::onPushRequestCompleted(void *response)
{
    App42PushNotificationResponse *pushResponse = (App42PushNotificationResponse*)response;
    printf("\ncode=%d",pushResponse->getCode());
    printf("\nResponse Body=%s",pushResponse->getBody().c_str());
    if (pushResponse->isSuccess)
    {
        for(std::vector<App42PushNotification>::iterator it = pushResponse->notifications.begin(); it != pushResponse->notifications.end(); ++it)
        {
            printf("\n UserName=%s",it->userName.c_str());
            printf("\n Message=%s\n",it->message.c_str());
            printf("\n Expiry=%s\n",it->expiry.c_str());
        }
    }
    else
    {
        printf("\nerrordetails:%s",pushResponse->errorDetails.c_str());
        printf("\nerrorMessage:%s",pushResponse->errorMessage.c_str());
        printf("\nappErrorCode:%d",pushResponse->appErrorCode);
        printf("\nhttpErrorCode:%d",pushResponse->httpErrorCode);
    }
}

Send Custom Push Message to User in Key Value or JSON Format

Send push message to user containing JSON or in key value format. On receiving this message on the device, it has to be parsed and accessed accordingly.

Required Parameters

userName - Name of the user to which message has to be send. message - Message which you want to send in JSON or key-value format.
const char* userName = "Nick";
map<string, string> messageMap;
messageMap["alert"] = "Hello! This is my first notification.";
messageMap["sound"] = "default";
messageMap["badge"] = "1";
App42API::Initialize("API_KEY", "SECRET_KEY");
PushNotificationService *pushService = App42API::BuildPushNotificationService();
Sample_Class *sampleClassInstance = new Sample_Class();
pushService->SendPushMessageToUser(userName, messageMap,app42callback(Sample_Class::onPushRequestCompleted, sampleClassInstance));
void Sample_Class::onPushRequestCompleted(void *response)
{
    App42PushNotificationResponse *pushResponse = (App42PushNotificationResponse*)response;
    printf("\ncode=%d",pushResponse->getCode());
    printf("\nResponse Body=%s",pushResponse->getBody().c_str());
    if (pushResponse->isSuccess)
    {
        for(std::vector<App42PushNotification>::iterator it = pushResponse->notifications.begin(); it != pushResponse->notifications.end(); ++it)
        {
            printf("\n UserName=%s",it->userName.c_str());
            printf("\n Message=%s\n",it->message.c_str());
            printf("\n Expiry=%s\n",it->expiry.c_str());
        }
    }
    else
    {
        printf("\nerrordetails:%s",pushResponse->errorDetails.c_str());
        printf("\nerrorMessage:%s",pushResponse->errorMessage.c_str());
        printf("\nappErrorCode:%d",pushResponse->appErrorCode);
        printf("\nhttpErrorCode:%d",pushResponse->httpErrorCode);
    }
}

Subscribe to Channel with Device Token

Subscribe the device to a particular channel with device token and type.

Required Parameters

userName - Username which want to subscribe. channelName - Name of channel for which user want to subscribe. deviceToken - Device token which you want to subscribe. deviceType - Device Type which you want to subscribe.
const char* channel = "News Channel" ;
const char* userName = "Nick" ;
const char* deviceToken = "Device Token";
DeviceType deviceType = IOS;
App42API::Initialize("API_KEY", "SECRET_KEY");
PushNotificationService *pushService = App42API::BuildPushNotificationService();
Sample_Class *sampleClassInstance = new Sample_Class();
pushService->SubscribeToChannel(channelName,userName,deviceToken, deviceType, app42callback(Sample_Class::onPushRequestCompleted, sampleClassInstance));
void Sample_Class::onPushRequestCompleted(void *response)
{
    App42PushNotificationResponse *pushResponse = (App42PushNotificationResponse*)response;
    printf("\ncode=%d",pushResponse->getCode());
    printf("\nResponse Body=%s",pushResponse->getBody().c_str());
    if (pushResponse->isSuccess)
    {
        for(std::vector<App42PushNotification>::iterator it = pushResponse->notifications.begin(); it != pushResponse->notifications.end(); ++it)
        {
            printf("\n UserName=%s",it->userName.c_str());
            printf("\n Type  = %s",it->type.c_str());
            printf("\n DeviceToken  = %s",it->deviceToken.c_str());
            for(std::vector<App42Channel>::iterator channel = it->channelArray.begin(); channel != it->channelArray.end(); ++channel)
            {
                printf("\n Channel Name=%s",channel->channelName.c_str());
            }
        }
    }
    else
    {
        printf("\nerrordetails:%s",pushResponse->errorDetails.c_str());
        printf("\nerrorMessage:%s",pushResponse->errorMessage.c_str());
        printf("\nappErrorCode:%d",pushResponse->appErrorCode);
        printf("\nhttpErrorCode:%d",pushResponse->httpErrorCode);
    }
}

Unsubscribe Device to Channel

Unsubscribe the device to a particular channel with device token.

Required Parameters

userName - Name of user who want to unsubscribe to channel. channelName - Name of channel for which user want to unsubscribe. deviceToken - Device token which you want to unsubscribe.
const char* channel = "News Channel" ;
const char* userName = "Nick" ;
const char* deviceToken = "Device Token";
App42API::Initialize("API_KEY", "SECRET_KEY");
PushNotificationService *pushService = App42API::BuildPushNotificationService();
Sample_Class *sampleClassInstance = new Sample_Class();
pushService->UnsubscribeDeviceToChannel(channelName,userName,deviceToken,app42callback(Sample_Class::onPushRequestCompleted, sampleClassInstance));
void Sample_Class::onPushRequestCompleted(void *response)
{
    App42PushNotificationResponse *pushResponse = (App42PushNotificationResponse*)response;
    printf("\ncode=%d",pushResponse->getCode());
    printf("\nResponse Body=%s",pushResponse->getBody().c_str());
    if (pushResponse->isSuccess)
    {
        for(std::vector<App42PushNotification>::iterator it = pushResponse->notifications.begin(); it != pushResponse->notifications.end(); ++it)
        {
            printf("\n UserName=%s",it->userName.c_str());
            printf("\n DeviceToken  = %s",it->deviceToken.c_str());
            for(std::vector<App42Channel>::iterator channel = it->channelArray.begin(); channel != it->channelArray.end(); ++channel)
            {
                printf("\n Channel Name=%s",channel->channelName.c_str());
            }
        }
    }
    else
    {
        printf("\nerrordetails:%s",pushResponse->errorDetails.c_str());
        printf("\nerrorMessage:%s",pushResponse->errorMessage.c_str());
        printf("\nappErrorCode:%d",pushResponse->appErrorCode);
        printf("\nhttpErrorCode:%d",pushResponse->httpErrorCode);
    }
}

Delete Device Token

Delete the device from push notification service.

Required Parameters

userName - Name of user whose device token has to delete. deviceToken - Device token which has to be deleted.
const char* userName = "Nick" ;
const char* deviceToken = "Device Token";
App42API::Initialize("API_KEY", "SECRET_KEY");
PushNotificationService *pushService = App42API::BuildPushNotificationService();
Sample_Class *sampleClassInstance = new Sample_Class();
pushService->DeleteDeviceToken(deviceToken,userName, app42callback(Sample_Class::onPushRequestCompleted, sampleClassInstance));

void Sample_Class::onPushRequestCompleted(void *response)
{
    App42PushNotificationResponse *pushResponse = (App42PushNotificationResponse*)response;
    printf("\ncode=%d",pushResponse->getCode());
    printf("\nResponse Body=%s",pushResponse->getBody().c_str());
    if (pushResponse->isSuccess)
    {
        for(std::vector<App42PushNotification>::iterator it = pushResponse->notifications.begin(); it != pushResponse->notifications.end(); ++it)
        {
            printf("\n UserName=%s",it->userName.c_str());
            printf("\n DeviceToken  = %s",it->deviceToken.c_str());
        }
    }
    else
    {
        printf("\nerrordetails:%s",pushResponse->errorDetails.c_str());
        printf("\nerrorMessage:%s",pushResponse->errorMessage.c_str());
        printf("\nappErrorCode:%d",pushResponse->appErrorCode);
        printf("\nhttpErrorCode:%d",pushResponse->httpErrorCode);
    }
}

Send Push Message to Group

Send push message to list of specific users.

Required Parameters

message - Message which you have to send. userList - List of the users for which message has to be send.
const char* message = "Message which you have to send";
std::vector<std::string>userList;
userList.push_back("Nick");
userList.push_back("John");
App42API::Initialize("API_KEY", "SECRET_KEY");
PushNotificationService *pushService = App42API::BuildPushNotificationService();
Sample_Class *sampleClassInstance = new Sample_Class();
pushService->SendPushMessageToGroup(userList, message, app42callback(Sample_Class::onPushRequestCompleted, sampleClassInstance));

void Sample_Class::onPushRequestCompleted(void *response)
{
    App42PushNotificationResponse *pushResponse = (App42PushNotificationResponse*)response;
    printf("\ncode=%d",pushResponse->getCode());
    printf("\nResponse Body=%s",pushResponse->getBody().c_str());
    if (pushResponse->isSuccess)
    {
        for(std::vector<App42PushNotification>::iterator it = pushResponse->notifications.begin(); it != pushResponse->notifications.end(); ++it)
        {
            printf("\n UserName=%s",it->userName.c_str());
            printf("\n Message=%s\n",it->message.c_str());
        }
    }
    else
    {
        printf("\nerrordetails:%s",pushResponse->errorDetails.c_str());
        printf("\nerrorMessage:%s",pushResponse->errorMessage.c_str());
        printf("\nappErrorCode:%d",pushResponse->appErrorCode);
        printf("\nhttpErrorCode:%d",pushResponse->httpErrorCode);
    }
}

Send Push to Target Users

Send push message to target users using storage query. This method fetches users from target database and collects using passed query and sends push notification to them.

Required Parameters

message - Message which you have to send. dbName - Unique handler for storage name. collectionName - Name of collection under which message has to be send. query - Query object containing custom query for sending push message to that target users.
const char* dbName = "<Your_database_name>";
const char* collectionName = "<Your_collection_name>";
string json = "{\"name\":\"Nick\",\"age\":30,\"phone\":\"xxx-xxx-xxx\"}";
App42API::setLoggedInUser("Nick");
App42API::Initialize("API_KEY", "SECRET_KEY");
PushNotificationService *pushService = App42API::BuildPushNotificationService();
Sample_Class *sampleClassInstance = new Sample_Class();
storageService->InsertJsonDocument(dbName, collectionName, jsonDoc, app42callback(Sample_Class::onPushRequestCompleted, sampleClassInstance));
void Sample_Class::onStorageRequestCompleted(void *response)
{
    App42StorageResponse *storageResponse = (App42StorageResponse*)response;
    printf("\ncode=%d",storageResponse->getCode());
    printf("\nResponse Body=%s",storageResponse->getBody().c_str());
    if (storageResponse->isSuccess)
    {
       	const char* message= "Message which you have to send";
 		const char* key = "name";
		const char* value = "Nick";
		Query *query = QueryBuilder::BuildQuery(key, value, APP42_OP_EQUALS);
		pushService->SendPushToTargetUsers(message.c_str(), dbName, collectionName, query, this, app42callfuncND_selector(Sample_Class::onPushRequestCompleted));
    }
    else
    {
        printf("\nerrordetails:%s",storageResponse->errorDetails.c_str());
        printf("\nerrorMessage:%s",storageResponse->errorMessage.c_str());
        printf("\nappErrorCode:%d",storageResponse->appErrorCode);
        printf("\nhttpErrorCode:%d",storageResponse->httpErrorCode);
    }
}

void Sample_Class::onPushRequestCompleted(App42CallBack *sender, void *response)
{
    App42PushNotificationResponse *pushResponse = (App42PushNotificationResponse*)response;
    printf("\ncode=%d",pushResponse->getCode());
    printf("\nResponse Body=%s",pushResponse->getBody().c_str());
    if (pushResponse->isSuccess)
    {
        for(std::vector<App42PushNotification>::iterator it = pushResponse->notifications.begin(); it != pushResponse->notifications.end(); ++it)
        {
            printf("\n Message=%s\n",it->message.c_str());
            printf("\n Expiry=%s\n",it->expiry.c_str());
        }
    }
    else
    {
        printf("\nerrordetails:%s",pushResponse->errorDetails.c_str());
        printf("\nerrorMessage:%s",pushResponse->errorMessage.c_str());
        printf("\nappErrorCode:%d",pushResponse->appErrorCode);
        printf("\nhttpErrorCode:%d",pushResponse->httpErrorCode);
    }
}

Schedule Message to User

Send message to a particular user on a scheduled time.

Required Parameters

userName - Name of the user which you want to send the message. message - Message which you have to send. scheduleDate - Time on which send messsage to user.
const char* userName = "Nick"; 
const char* message = "Message which you have to send";
tm *expiryDate;
time_t t = time(NULL);
expiryDate = gmtime(&t);
App42API::Initialize("API_KEY", "SECRET_KEY");
PushNotificationService *pushService = App42API::BuildPushNotificationService();
Sample_Class *sampleClassInstance = new Sample_Class();
pushService->ScheduleMessageToUser(userName, message, expiryDate, app42callback(Sample_Class::onPushRequestCompleted, sampleClassInstance));

void Sample_Class::onPushRequestCompleted(void *response)
{
    App42PushNotificationResponse *pushResponse = (App42PushNotificationResponse*)response;
    printf("\ncode=%d",pushResponse->getCode());
    printf("\nResponse Body=%s",pushResponse->getBody().c_str());
    if (pushResponse->isSuccess)
    {
        for(std::vector<App42PushNotification>::iterator it = pushResponse->notifications.begin(); it != pushResponse->notifications.end(); ++it)
        {
            printf("\n UserName=%s",it->userName.c_str());
            printf("\n Message=%s\n",it->message.c_str());
            printf("\n Expiry=%s\n",it->expiry.c_str());
        }
    }
    else
    {
        printf("\nerrordetails:%s",pushResponse->errorDetails.c_str());
        printf("\nerrorMessage:%s",pushResponse->errorMessage.c_str());
        printf("\nappErrorCode:%d",pushResponse->appErrorCode);
        printf("\nhttpErrorCode:%d",pushResponse->httpErrorCode);
    }
}

Send Multilingual Push Notification

Send multilingual(UTF-8) push message to a particular user. Note: Same can done for sending push methods.

Required Parameters

userName - Name of the user which you want to send the message. message - Message which you want to send.
const char* userName = "Nick";
const char* message = "Message which you have to send";
map<string,string> otherMetaHeaders;
otherMetaHeaders["dataEncoding"] = "true";
App42API::Initialize("API_KEY", "SECRET_KEY");
PushNotificationService *pushService = App42API::BuildPushNotificationService();
Sample_Class *sampleClassInstance = new Sample_Class();
pushService->setOtherMetaHeaders(otherMetaHeaders);
pushService->SendPushMessageToUser(userName,message,app42callback(Sample_Class::onPushRequestCompleted, sampleClassInstance));

void Sample_Class::onPushRequestCompleted(void *response)
{
    App42PushNotificationResponse *pushResponse = (App42PushNotificationResponse*)response;
    printf("\ncode=%d",pushResponse->getCode());
    printf("\nResponse Body=%s",pushResponse->getBody().c_str());
    if (pushResponse->isSuccess)
    {
        for(std::vector<App42PushNotification>::iterator it = pushResponse->notifications.begin(); it != pushResponse->notifications.end(); ++it)
        {
            printf("\n UserName=%s",it->userName.c_str());
            printf("\n Message=%s\n",it->message.c_str());
            printf("\n Expiry=%s\n",it->expiry.c_str());
        }
    }
    else
    {
        printf("\nerrordetails:%s",pushResponse->errorDetails.c_str());
        printf("\nerrorMessage:%s",pushResponse->errorMessage.c_str());
        printf("\nappErrorCode:%d",pushResponse->appErrorCode);
        printf("\nhttpErrorCode:%d",pushResponse->httpErrorCode);
    }
}

Unsubscribe Device

Unsubscribe the device from push notification service.

Required Parameters

userName - Name of the user which you want unsubscribe from push-notifications. deviceToken - Device token which you want to unsubscribe.
const char* userName = "Nick";
const char* deviceToken = "Device Token";
App42API::Initialize("API_KEY", "SECRET_KEY");
PushNotificationService *pushService = App42API::BuildPushNotificationService();
Sample_Class *sampleClassInstance = new Sample_Class();
pushService->UnsubscribeDevice(deviceToken, userName,app42callback(Sample_Class::onPushRequestCompleted, sampleClassInstance));
void Sample_Class::onPushRequestCompleted(void *response)
{
    App42PushNotificationResponse *pushResponse = (App42PushNotificationResponse*)response;
    printf("\ncode=%d",pushResponse->getCode());
    printf("\nResponse Body=%s",pushResponse->getBody().c_str());
    if (pushResponse->isSuccess)
    {
        for(std::vector<App42PushNotification>::iterator it = pushResponse->notifications.begin(); it != pushResponse->notifications.end(); ++it)
        {
            printf("\n UserName=%s",it->userName.c_str());
            printf("\n Type=%s\n",it->type.c_str());
            printf("\n DeviceToken=%s\n",it->deviceToken.c_str());
        }
    }
    else
    {
        printf("\nerrordetails:%s",pushResponse->errorDetails.c_str());
        printf("\nerrorMessage:%s",pushResponse->errorMessage.c_str());
        printf("\nappErrorCode:%d",pushResponse->appErrorCode);
        printf("\nhttpErrorCode:%d",pushResponse->httpErrorCode);
    }
}

Resubscribe Device

Resubscribe device for push notification service after unsubscribing.

Required Parameters

userName - Name of the user which you want resubscribe from push-notifications. deviceToken - Device token which you want to resubscribe.
const char* userName = "Nick";
const char* deviceToken = "Device Token";
App42API::Initialize("API_KEY", "SECRET_KEY");
PushNotificationService *pushService = App42API::BuildPushNotificationService();
Sample_Class *sampleClassInstance = new Sample_Class();
pushService->ResubscribeDevice(deviceToken, userName,app42callback(Sample_Class::onPushRequestCompleted, sampleClassInstance));
void Sample_Class::onPushRequestCompleted(void *response)
{
    App42PushNotificationResponse *pushResponse = (App42PushNotificationResponse*)response;
    printf("\ncode=%d",pushResponse->getCode());
    printf("\nResponse Body=%s",pushResponse->getBody().c_str());
    if (pushResponse->isSuccess)
    {
        for(std::vector<App42PushNotification>::iterator it = pushResponse->notifications.begin(); it != pushResponse->notifications.end(); ++it)
        {
            printf("\n UserName=%s",it->userName.c_str());
            printf("\n Type=%s\n",it->type.c_str());
            printf("\n DeviceToken=%s\n",it->deviceToken.c_str());
        }
    }
    else
    {
        printf("\nerrordetails:%s",pushResponse->errorDetails.c_str());
        printf("\nerrorMessage:%s",pushResponse->errorMessage.c_str());
        printf("\nappErrorCode:%d",pushResponse->appErrorCode);
        printf("\nhttpErrorCode:%d",pushResponse->httpErrorCode);
    }
}

Delete All Devices

Delete all devices of a particular user.

Required Parameters

userName - Name of the user which you want to delete the message.
const char* userName = "Nick";
App42API::Initialize("API_KEY", "SECRET_KEY");
PushNotificationService *pushService = App42API::BuildPushNotificationService();
Sample_Class *sampleClassInstance = new Sample_Class();
pushService->DeleteAllDevices(userName, app42callback(Sample_Class::onPushRequestCompleted, sampleClassInstance));
void Sample_Class::onPushRequestCompleted(void *response)
{
    App42PushNotificationResponse *pushResponse = (App42PushNotificationResponse*)response;
    printf("\ncode=%d",pushResponse->getCode());
    printf("\nResponse Body=%s",pushResponse->getBody().c_str());
    if (pushResponse->isSuccess)
    {
        for(std::vector<App42PushNotification>::iterator it = pushResponse->notifications.begin(); it != pushResponse->notifications.end(); ++it)
        {
            printf("\n UserName=%s",it->userName.c_str());
            printf("\n DeviceToken=%s\n",it->deviceToken.c_str());
        }
    }
    else
    {
        printf("\nerrordetails:%s",pushResponse->errorDetails.c_str());
        printf("\nerrorMessage:%s",pushResponse->errorMessage.c_str());
        printf("\nappErrorCode:%d",pushResponse->appErrorCode);
        printf("\nhttpErrorCode:%d",pushResponse->httpErrorCode);
    }
}

Exception Handling

The functions available under Push Notification API can throw some exceptions in abnormal conditions. Example of the same has been given below.

If an app developer is sending a message to a channel which is not in database, the function will throw the App42Exception (as shown below) with message as “Not Found” and the appErrorCode as “1702” and the details as “Channel by the name ‘News Channel’ does not exist”.

const char* channelName = "News Channel";
const char* message = "Message which you have to send";
App42API::Initialize("API_KEY", "SECRET_KEY");
PushNotificationService *pushService = App42API::BuildPushNotificationService();
Sample_Class *sampleClassInstance = new Sample_Class();
pushService->SendPushMessageToChannel(channelName, message, app42callback(Sample_Class::onPushRequestCompleted, sampleClassInstance));
void Sample_Class::onPushRequestCompleted(void *response)
{
    App42PushNotificationResponse *pushResponse = (App42PushNotificationResponse*)response;
    printf("\ncode=%d",pushResponse->getCode());
    printf("\nResponse Body=%s",pushResponse->getBody().c_str());
    if (pushResponse->isSuccess)
    {
        //Handle success Response here
    }
    else
    {
        printf("\nerrordetails:%s",pushResponse->errorDetails.c_str());
        printf("\nerrorMessage:%s",pushResponse->errorMessage.c_str());
        printf("\nappErrorCode:%d",pushResponse->appErrorCode);
        printf("\nhttpErrorCode:%d",pushResponse->httpErrorCode);
        int appErrorCode = pushResponse->appErrorCode;
        if(appErrorCode == 1702)
        {
            // Handle here for Not Found (Channel by the name '@channelName' does not exist.) not exist.)
        }
        else if(appErrorCode == 1401)
        {
            // handle here for Client is not authorized
        }
        else if(appErrorCode == 1500)
        {
            // handle here for Internal Server Error
        }
    }
}

Error Codes

Functions in PushNotification API might throw exceptions with following HTTP and Application Error Codes (along with their descriptions):

1400 - BAD REQUEST - The Request parameters are invalid. 1401 - UNAUTHORIZED - Client is not authorized. 1500 - INTERNAL SERVER ERROR - Internal Server Error. Please try again. 1700 - BAD REQUEST - User by the name '@userName' already registered with the device '@deviceToken'. 1701 - BAD REQUEST - Channel by the name '@channelName' already exist 1702 - NOT FOUND - Channel by the name '@channelName' does not exist. 1703 - NOT FOUND - User by the name '@userName' does not have any device registered 1704 - BAD REQUEST - User by the name '@userName' already subscribed for the channel '@channelName' 1705 - NOT FOUND - User by the name '@userName' not subscribed for any channel 1706 - NOT FOUND - No device is registered with the App 1707 - NOT FOUND - No device is Subscribed to the channel '@channelName'. 1708 - NOT FOUND - Channel by the name '@channelName' device '@deviceToken' does not subscribe. 1709 - NOT FOUND - User by the name '@userName' device token '@deviceToken' does not registered. 1710 - NOT FOUND - No device registered for user list '@userList' 1711 - NOT FOUND - No target user found for given query 1712 - NOT FOUND - In Active Users with date rnage '@startDate'and '@endDate' does not exist. 1713 - BAD REQUEST - User by the name '@userName' already unsubscribe with the device '@deviceToken' 1714 - BAD REQUEST - User by the name '@userName' already subscribe with the device '@deviceToken' 1715 - BAD REQUEST - Your previous request already in process.Please Try after sometime.