Parameterized segments

Define criteria from the dashboard and fill their parameters with values from the API.

Imagine you have a news application where a user can follow multiple topics. You want to automate push notifications by sending news to the users following the topic of each piece of news.

Instead of creating one segment for each topic, we'll create just one parameterized segment.

Step 1. Set properties from the SDK

In the SDK you can register a user to multiple topics with something along these lines:

window.WonderPush = window.WonderPush || [];
WonderPush.push(['putProperties', {
  "string_followedTopics": ["economics", "politics", "soccer"],
}]);
JSONObject properties = new JSONObject();
JSONArray stringFollowedTopics = new JSONArray();
stringFollowedTopics.put("economics");
stringFollowedTopics.put("politics");
stringFollowedTopics.put("soccer");
properties.put("string_followedTopics", stringFollowedTopics);
WonderPush.putProperties(properties);
WonderPush.putProperties([
  "string_followedTopics": ["economics", "politics", "soccer"],
]);
[WonderPush putProperties:@{
  @"string_followedTopics": @[@"economics", @"politics", @"soccer"],
}];
WonderPush.putProperties({
  string_followedTopics: ["economics", "politics", "soccer"],
});
WonderPush.putProperties({
  string_followedTopics: ["economics", "politics", "soccer"],
});
WonderPush.putProperties({
  string_followedTopics: ["economics", "politics", "soccer"],
});

See the Properties section for more information.

Step 2. Create a parameterized segment

Let's head over your dashboard, and create a segment with the following criterion:

1737

See how we wrote {{topic}} as a value. That's how you create a parameter.
Note the segment's ID, we'll refer to it as mySegmentId.

727

Step 3. Send notifications

When sending a push notification, you will now use the segment's ID in targetSegmentIds, and give the segment's parameter topic a value using a JSON object in segmentParams. For example:

curl -XPOST https://management-api.wonderpush.com/v1/deliveries?accessToken="YOUR_ACCESS_TOKEN" \
       -d targetSegmentIds=mySegmentId \
       -d segmentParams='{"topic":"soccer"}' \
       -d notification='{"alert": {"text": "Hello topic!"}}'

Note: replace mySegmentId with the actual id of the segment you've created.

Using multiple parameterized segments

See Sending to one or more segments or Combining segments for more information on how you can use multiple segments at once.

You can very well use multiple parameterized target segments. The parameter-to-value mapping you give as a JSON object in the segmentParams argument will be applied the same to all segments. This is handy because all references to {{topic}} would be uniformy replaced by the provided value.
But this may also be impractical if you use the same parameterized segment multiple times, or if two parameterized segments happen to use the same parameter name for different purpose.

The solution is to instead give an array of parameter-to-value mappings in segmentParams, each mapping item corresponding to the segments in the same position in targetSegmentIds.

To further the previous example, say you want to send a notification to users following either economics or politics. Here's how:

curl -XPOST https://management-api.wonderpush.com/v1/deliveries?accessToken="YOUR_ACCESS_TOKEN" \
       -d targetSegmentIds=mySegmentId,mySegmentId \
       -d segmentParams='[{"topic":"economics"},{"topic":"politics"}]' \
       -d notification='{"alert": {"text": "Hello topic!"}}'

If you use a JSON array for segmentParams, its length must match the number of IDs given in targetSegmentIds.
The parameter-to-value mappings in the segmentParams array must correspond to the segments in the same position in targetSegmentIds.
You must use either null or {} for non-parameterized segments.