Step 1: Create service

1. Configure the service

To configure the service, replace the parameters at the beginning of the file with your service parameters:

service_id = "example.com" // replace with your own registered service ID
service_secret = "secretkey" // replace with your own services' service secret

#Recipient of the notification – url encoded nid
nid = "nidvalue"

#This example uses the nid for pushing the notification
url = "https://alpha.one.ovi.com/nnapi/1.0/nid/"
url += quote_plus(nid)

The first two variables define the Service ID and Service Secret for the developed service. These parameters are mandatory when sending notifications to client applications. The Service Secret is used to authenticate the REST API requests sent by the service.

You need to replace the example values above with the values that you received when you registered your service.

The last parameter defines the recipient of the notification.

2. Create handlers and openers for the HTTP request

The next step is to create the handlers for opening the URL and authenticating the service with the Notification Server:

https_handler = urllib2.HTTPSHandler()
auth_handler = urllib2.HTTPDigestAuthHandler() 
auth_handler.add_password("null", url, service_id, service_secret)

An opener for building and sending the HTTP requests is then created:

opener = urllib2.build_opener(https_handler, auth_handler)
urllib2.install_opener(opener)

3. Specify the parameters of the POST Notifications API method

The parameters of the POST Notifications API method are then created. You can change the text of the payload by updating the payload string below.

data = {"payload":"Test Notification from REST API", "wakeup":"true"}

4. Run the service

The last step is to run the service:

try:
	f = urllib2.urlopen(url, urlencode(data))
except HTTPError, e:
	print "HTTP Error: " + str(e.code)      
except URLError, e:
	print "URL Error: " + str(e.reason)
else:
	response = f.read()
	status_code = f.getcode()
	#If response is empty it means that the recipient application is unreachable. See the HTTP status code to check the reason
	if response == "":
		print "Error: Application not reachable. Received HTTP status code " + str(status_code)
	else:
		print response + “ Received HTTP status code  “ + str(status_code)
		f.close()