PushRadar and curl

The latest version of PushRadar's API is v1, released August 21, 2017.

Welcome to PushRadar's API documentation for curl. Curl is used in command line scripts to transfer data to or from a server using various protocols. This article covers calling the /broadcast endpoint of the PushRadar API to publish notifications to a channel in realtime.

(cURL)Prerequisites

To try out the code samples, you will need a tool that supports curl. Git Bash is ideal for testing. You can find out more about curl at https://curl.haxx.se

(server)"Hello World!" example

The following example demonstrates broadcasting a "Hello World!" message on a channel called test-channel. Replace your-secret-key with your actual PushRadar secret, which may be obtained from the API page of your dashboard.

  • curl
curl -X POST https://api.pushradar.com/v1/broadcast
     -H "Content-type: application/json"
     -d '{"api_secret": "your-secret-key", "channel": "test-channel",
          "notification": {"message": "Hello World!"}}'

In the above example, the -X option specifies the request type (followed by PushRadar's API endpoint), -H sets a header stating that the content is encoded as JSON, and -d specifies the actual notification content.

(client)Receiving notifications

To find out how to receive notifications by subscribing to channels, check out the documentation for our JavaScript client library.

(feature)Action targeting

The following example demonstrates sending a notification to users who have taken a specific action, in this case, users who have purchased item #11. If you include more than one action identifier in the target_actions array, PushRadar will send the notification to users who have taken any of the given actions.

  • curl
curl -X POST https://api.pushradar.com/v1/broadcast
     -H "Content-type: application/json"
     -d '{"api_secret": "your-secret-key", "channel": "test-channel",
          "notification": {"message": "You have purchased item 11."},
          "target_actions": ["purchased-item-#11"]}'

With the PushRadar JavaScript client library, you can track actions that users have taken using the action method:

  • JavaScript
// ... var radar = new PushRadar(...)
radar.action("purchased-item-#11");
Reverse action targeting

To send a notification to those who have not taken a certain action, you can use the following code. If you include more than one action identifier in the target_not_actions array, PushRadar will only send the notification to clients who have not taken any of the given actions.

  • curl
curl -X POST https://api.pushradar.com/v1/broadcast
     -H "Content-type: application/json"
     -d '{"api_secret": "your-secret-key", "channel": "test-channel",
          "notification": {"message": "You have not purchased item 12."},
          "target_not_actions": ["purchased-item-#12"]}'
(feature)User targeting

User targeting allows you to broadcast notifications to specific users, identified by their user ID. Pass in the desired user IDs as members of the target_user_ids array. For this example, we will send a new message to the user with ID usr100 in our system:

  • curl
curl -X POST https://api.pushradar.com/v1/broadcast
     -H "Content-type: application/json"
     -d '{"api_secret": "your-secret-key", "channel": "user:new-message",
          "notification": {"message": "Hey there, from Sam!", "sender_name": "Sam"},
          "target_user_ids": ["usr100"]}'

Make sure to set user IDs on each website page load using the PushRadar JavaScript client library:

  • JavaScript
// Set user ID on initialization
var radar = new PushRadar("your-api-key", "usr100");

// ... or ... radar.setUserID("usr100");

radar.subscribe("user:new-message", function(notification) {
    alert("New message from " + notification.sender_name); // "New message from Sam"
});
(feature)Browser targeting

You can target notifications to users who are currently using specific web browsers by passing in browser identifiers as members of the target_browsers array. In the following example, we broadcast a notification to users currently using Firefox, Chrome or Internet Explorer:

Supported browsers are:   "chrome", "firefox", "ie", "edge", "safari" and "opera"

  • curl
curl -X POST https://api.pushradar.com/v1/broadcast
     -H "Content-type: application/json"
     -d '{"api_secret": "your-secret-key", "channel": "test-channel",
          "notification": {"data": "This notification is browser-targeted."},
          "target_browsers": ["firefox", "chrome", "ie"]}'
(feature)Geo-targeting (ISO 3166-1 alpha-2 )

You can easily broadcast notifications to clients located in certain countries by specifying two-letter ISO country codes in the target_countries array. In this example, we broadcast a notification to clients who are currently located in the UK or France:

  • curl
curl -X POST https://api.pushradar.com/v1/broadcast
     -H "Content-type: application/json"
     -d '{"api_secret": "your-secret-key", "channel": "test-channel",
          "notification": {"data": "You are in the UK or France!"},
          "target_countries": ["GB", "FR"]}'
(feature)IP address targeting

PushRadar supports IP address targeting. When using IP targeting, only clients with the given IP address(es) specified will receive your notification. Below is an example targeting a client with IP address 216.3.128.12. You can include IPv4 and IPv6 addresses in the target_ips array.

  • curl
curl -X POST https://api.pushradar.com/v1/broadcast
     -H "Content-type: application/json"
     -d '{"api_secret": "your-secret-key", "channel": "test-channel",
          "notification": {"data": "This notification is IP-targeted."},
          "target_ips": ["216.3.128.12"]}'