App42 Cloud API

Ruby Cloud SDK

Push Notification

The service is for pushing the notifications to any device using GCM(Google Cloud Messaging). You have to upload your apikey that you received while registering for GCM and you have to store your device token with particular username. This service allows you the feature of sending message to particular channel, particular user or to all your users.For sending message to any channel, you have to create the channel and send the message to channel. The users which have subscribed to that channel will receive all the notification for that channel. For sending message to particular user, you have to pass username and message. Notification will sent to the device of registered user. The most important feature you can send your message to all your device whether it is iphone, android or blackberry.

In order to use the various functions available under a particular API, a developer will have to create an instance of ServiceAPI by passing the apiKey and the secretKey which has been received while creating an app.

	api = App42::ServiceAPI.new("<API_KEY>","<SECRET_KEY>") 

After initialization, developer needs to call the buildXXXService method on ServiceAPI instance to get the instance of the particular API that you wish to build. For example, To build an instance of PushnotificationService, buildPushnotificationService() method needs to be called.

 pushnotificationService = api.buildPushnotificationService();

Once the API instance has been retrieved, You are ready to use the functions available for that particular API.

The methods available under a particular API will return the domain object (like Push Notification in this case) as a response which will have the accessor / mutator to access the available properties for that object. You can get the response in the form of JSON as well. We have also provided the JSON response with every function detail which can be retrieved by calling the toString() on the returned object.

channel = "News Channel";
message = "Message which you have to send";

pushNotification = pushnotificationService.send_push_message_to_channel(channel, message);  /* returns the Push Notification object. */
puts "Message is #{pushNotification.message}";
channelList = Array.new();
channelList = pushNotification.channelList();

for channelObj in channelList
  puts "Channel Name is #{channelObj.name}";
end

jsonResponse = pushNotification.to_s(); /* returns the response in JSON format. */   

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

E.g. If App developer is sending a message to 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 '<channelName>' does not exist.".

channelName = "News Channel";
message = "Message which you have to send";

begin
  pushNotification = pushnotificationService.sendPushMessageToChannel(channelName, message);
rescue App42Exception => ex
  appErrorCode = ex.app_error_code;
  httpErrorCode = ex.http_error_code;

  if(appErrorCode == 1702)
    # Handle here for Not Found (Channel by the name '<channelName>' does not exist.)

  elsif(appErrorCode == 1401)
    # Handle here for Client is not authorized

  elsif(appErrorCode == 1500)
    # Handle here for Internal Server Error
  end

  jsonText = ex.getMessage();
end  
{
    "app42Fault": {
        "httpErrorCode": 400,
        "appErrorCode": 1702,
        "message": "Not Found",
        "details": "Channel by the name 'News Channel' does not exist"
    }
}							

Below are the HTTP Error Codes and their description, the function under the Pushnotification API can throw.

/*
1400 - BAD REQUEST - The Request parameters are invalid
1401 - UNAUTHORIZED - Client is not authorized
1500 - INTERNAL SERVER ERROR - Internal Server Error. Please try again	
*/		

Below are the Application Error Codes and their description, the function under the Pushnotification API can throw.

/*
1700 - BAD REQUEST - User by the name '<userName>' already registered with the device '<deviceToken>'.
1701 - BAD REQUEST - Channel by the name '<channelName>' already exist for user '<userName>'.
1702 - BAD REQUEST - Channel by the name '<channelName>' does not exist.
1703 - NOT FOUND - User by the name '<userName>' does not have any device registered.
1704 - NOT FOUND - User by the name '<userName>' already subscribed for the channel '<channelName>'.
1705 - NOT FOUND - User by the name '<userName>' not subscribed for the channel '<channelName>'.
1706 - NOT FOUND - No device is registered with the App.
1707 - NOT FOUND - No device is Subscribed to the channel '<channelName>'.
*/		

Various functions available under Pushnotification API has been explained below.

def store_device_token(userName, deviceToken, type)

Stores your device token on server with particular username

Parameters:

Username - username with which you want your device to be registered
deviceToken - device id
type - type of the device like Android, iOS , WP7

Returns:

PushNotification Object.

Response: PushNotification Object

Exception:

The method throws the following exception with the error code.
/*
1700 - BAD REQUEST - User by the name '<userName>' already registered with the device '<deviceToken>'
*/        
For more details click Exception
username = "Nick";
deviceToken = "Device Token";

dtype = App42::AppTab::DeviceType.new();
pushNotification = pushnotificationService.store_device_token(username, deviceToken, dtype.enum("WP7"));   /* returns the PushNotification object. */ 
puts "UserName is #{pushNotification.userName}";
puts "Type is #{pushNotification.type}";
puts "DeviceToken is #{pushNotification.deviceToken}"; 

jsonResponse = pushNotification.to_s(); /* returns the response in JSON format. (as shown below)*/  
		   
 {
    "app42": {
        "response": {
            "success": true,
            "push": {
                "userName": "Nick",
                "type": "Android",
                "deviceToken": "Device Token"
            }
        }
    }
}	
def create_channel_for_app(channel, description)

Create Channel for app on which user can subscribe and get the notification for that channel

Parameters:

channel - channel name which you want to create
description - description for that channel

Returns:

The PushNotification Object

Response: PushNotification Object

Exception:

The method throws the following exception with the error code.
/*		   
1701 - BAD REQUEST - Channel by the name '<channelName>' already exist for user '<userName>'.
*/	        
For more details click Exception
channel = "newsChannel";
description = "channel all about the news";

pushNotification = pushnotificationService.create_channel_for_app(channel, description);  /* returns the PushNotification object. */
channelList = Array.new();
channelList = pushNotification.channelList();

for channelObj in channelList
  puts "ChannelName is #{channelObj.channelName}";
  puts "Description is #{channelObj.channelDescription}";
end

jsonResponse = pushNotification.to_s(); /* returns the response in JSON format. (as shown below)*/  
{
    "app42": {
        "response": {
            "success": true,
            "push": {
                "channels": {
                    "channel": {
                        "channelName": "newsChannel",
                        "description": "channel all about the news"
                    }
                }
            }
        }
    }
} 
def subscribe_to_channel(channel, userName)

Selected User Will Be Subscribe To The Existing Channel

Parameters:

channel - the channel name which you want to subscribe
userName - username which want to subscribe

Returns:

The PushNotification Object

Response: PushNotification Object

Exception:

The method throws the following exception with the error code.
/*		   
1702 - BAD REQUEST - Channel by the name '<channelName>' does not exist.
1703 - NOT FOUND - User by the name '<userName>' does not have any device registered.
1704 - NOT FOUND - User by the name '<userName>' already subscribed for the channel '<channelName>'.
*/		       
For more details click Exception
channelName = "newsChannel";
userName = "Nick";

pushNotification = pushnotificationService.subscribe_to_channel(channelName, userName);  /* returns the PushNotification object. */
puts "UserName is #{pushNotification.userName}";
channelList = Array.new();
channelList = pushNotification.channelList();

for channelObj in channelList
  puts "channelName is #{channelObj.name}";
end

jsonResponse = pushNotification.to_s();  /* returns the response in JSON format. (as shown below)*/ 
		
{
    "app42": {
        "response": {
            "success": true,
            "push": {
                "userName": "Nick",
                "channels": {
                    "channel": {
                        "channelName": "newsChannel"
                    }
                }
            }
        }
    }
} 
def unsubscribe_from_channel(channel, userName)

Selected User Will Be Unsubscribe From The Existing Channel

Parameters:

channel - channel name which you want to unsubscribe
userName - username which want to unsubscribe

Returns:

The PushNotification Object

Response: PushNotification Object

Exception:

The method throws the following exception with the error code.
/*		   
1702 - BAD REQUEST - Channel by the name '<channelName>' does not exist.
1703 - NOT FOUND - User by the name '<userName>' does not have any device registered.
1705 - NOT FOUND - User by the name '<userName>' not subscribed for the channel '<channelName>'.
*/        
For more details click Exception
channelName = "newsChannel";
userName = "Nick";

pushNotification = pushnotificationService.unsubscribe_to_channel(channelName, userName);  /* returns the PushNotification object. */
puts "UserName is #{pushNotification.userName}";
channelList = Array.new();
channelList = pushNotification.channelList();

for channelObj in channelList
  puts "channelName is #{channelObj.name}";
end

jsonResponse = pushNotification.to_s();  /* returns the response in JSON format. (as shown below)*/ 
{
    "app42": {
        "response": {
            "success": true,
            "push": {
                "userName": "Nick",
                "channels": {
                    "channel": {
                        "channelName": "newsChannel"
                    }
                }
            }
        }
    }
} 
def send_push_message_to_channel(channel, message)

send push message to channel containing string

Parameters:

channel - channel name which you want to send your json message
message - push message

Returns:

The PushNotification Object

Response: PushNotification Object

Exception:

The method throws the following exception with the error code.
/*		   
1702 - BAD REQUEST - Channel by the name '<channelName>' does not exist.
1707 - NOT FOUND - No device is Subscribed to the channel '<channelName>'.
*/	        
For more details click Exception
channel = "newsChannel";
message = "Mary Kom won the bronze medal in Olympics";

pushNotification = pushnotificationService.send_push_message_to_channel(channel, message);  /* returns the PushNotification object. */
puts "Message is #{pushNotification.message}";
channelList = Array.new();
channelList = pushNotification.channelList();

for channelObj in channelList
  puts "channelName is #{channelObj.name}";
end

jsonResponse = pushNotification.to_s();  /* returns the response in JSON format. (as shown below)*/ 
  		
{
    "app42": {
        "response": {
            "success": true,
            "push": {
                "message": "Mary Kom won the bronze medal in Olympics",
                "channels": {
                    "channel": {
                        "channelName": "newsChannel"
                    }
                }
            }
        }
    }
}   
def send_push_message_to_all(message)

Send push message to all your users

Parameters:

message - push message

Returns:

The PushNotification Object

Response: PushNotification Object

Exception:

The method throws the following exception with the error code.
/*		   
1706 - NOT FOUND - No device is registered with the App.
*/	        
For more details click Exception
message = "vijay kumar shoots the silver medal";

pushNotification = pushNotificationService.send_push_message_to_all(message); /* returns the PushNotification object. */
puts "Message is #{pushNotification.message}";

jsonResponse = pushNotification.to_s();  /* returns the response in JSON format. (as shown below)*/ 
{
    "app42": {
        "response": {
            "success": true,
            "push": {
                "message": "vijay kumar shoots the silver medal"
            }
        }
    }
}   
def send_push_message_to_user(userName, message)

Send Push Message To paticular user in string format

Parameters:

userName - username which you want to send message
message - push message

Returns:

The PushNotification Object

Response: PushNotificationObject

Exception:

The method throws the following exception with the error code.
/*		   
1703 - NOT FOUND - User by the name '<userName>' does not have any device registered.
*/	        
For more details click Exception
userName = "Nick";
message = "Hi Nick! you have won 10 points";
						
pushNotification = pushNotificationService.send_push_message_to_user(userName, message); /* returns the PushNotification object. */
puts "User Name is #{pushNotification.userName}";
puts "Message is #{pushNotification.message}";

jsonResponse = pushNotification.to_s(); /* returns the response in JSON format. (as shown below)*/ 
{
    "app42": {
        "response": {
            "success": true,
            "push": {
                "userName": "Nick",
                "message": "Hi Nick! you have won 10 points"
            }
        }
    }
}  
def send_push_message_to_all_by_type(message, type)

Send Push Message to particular all by type

Parameters:

message - push Message
type - type of the device like Android, iOS , WP7

Returns:

The PushNotification Object

Response: PushNotification Object

Exception:

The method throws the following exception with the error code.
/*		   
1706 - NOT FOUND - No device is registered with the App.
*/	        
For more details click Exception
message = "vijay kumar shoots the silver medal";

dtype = App42::AppTab::DeviceType.new();
pushNotification = pushNotificationService.send_push_message_to_all_by_type(message, dtype.enum("WP7"));
puts "Message is #{pushNotification.message}";
puts "Type is #{pushNotification.type}";

jsonResponse = pushNotification.to_s(); 
{
    "app42": {
        "response": {
            "success": true,
            "push": {
                "message": "vijay kumar shoots the silver medal",
                "expiry": "2012-10-15T11:03:45.425Z",
                "type": "ANDROID"
            }
        }
    }
}