Nouf Nouf - 1 year ago 82
iOS Question

OneSignal - Sending Notification Base on Tags

I was wondering if I can send notification for specific user in OneSignal by using tags instead of included_segments I want to send the notification
to specific tags without having to create segments and filter the user their base on the them

$fields = array(
'app_id' => "XXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXX",
'included_segments' => array($segments),
'data' => array("foo" => "bar"),
'contents' => $content
);

Answer Source

In below example, if level>10 or amount_spent>0 is the filter condition where level and amount_spent are the keys of the respective tag.

    try {
       String jsonResponse;

       URL url = new URL("https://onesignal.com/api/v1/notifications");
       HttpURLConnection con = (HttpURLConnection)url.openConnection();
       con.setUseCaches(false);
       con.setDoOutput(true);
       con.setDoInput(true);

       con.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
       con.setRequestProperty("Authorization", "Basic NGEwMGZmMjItY2NkNy0xMWUzLTk5ZDUtMDAwYzI5NDBlNjJj");
       con.setRequestMethod("POST");

       String strJsonBody = "{"
                      +   "\"app_id\": \"5eb5a37e-b458-11e3-ac11-000c2940e62c\","
                      +   "\"filters\": [{\"field\": \"tag\", \"key\": \"level\", \"relation\": \">\", \"value\": \"10\"},{\"operator\": \"OR\"},{\"field\": \"amount_spent\", \"relation\": \">\",\"value\": \"0\"}],"
                      +   "\"data\": {\"foo\": \"bar\"},"
                      +   "\"contents\": {\"en\": \"English Message\"}"
                      + "}";


       System.out.println("strJsonBody:\n" + strJsonBody);

       byte[] sendBytes = strJsonBody.getBytes("UTF-8");
       con.setFixedLengthStreamingMode(sendBytes.length);

       OutputStream outputStream = con.getOutputStream();
       outputStream.write(sendBytes);

       int httpResponse = con.getResponseCode();
       System.out.println("httpResponse: " + httpResponse);

       if (httpResponse >= HttpURLConnection.HTTP_OK && httpResponse < HttpURLConnection.HTTP_BAD_REQUEST) {
           Scanner scanner = new Scanner(con.getInputStream(), "UTF-8");
           jsonResponse = scanner.useDelimiter("\\A").hasNext() ? scanner.next() : "";
           scanner.close();
       }else {
          Scanner scanner = new Scanner(con.getErrorStream(), "UTF-8");
          jsonResponse = scanner.useDelimiter("\\A").hasNext() ? scanner.next() : "";
          scanner.close();
   }
   System.out.println("jsonResponse:\n" + jsonResponse);

}   catch(Throwable t) {
    t.printStackTrace();
}

Taken from : Source. Search for Send based on filters/tags - Create notification from the source mentioned

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download