OneSignal - 基于标签发送通知

时间:2017-08-17 12:27:54

标签: android ios push-notification onesignal

我想知道我是否可以使用标签而不是included_segments在OneSignal中发送特定用户的通知我想发送通知 到特定的标签,而不必创建段,并根据用户过滤用户

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

1 个答案:

答案 0 :(得分:3)

在下面的示例中,如果level> 10或amount_spent> 0是过滤条件,其中level和amount_spent是相应标记的键。

    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();
}

取自:Source。 从提及的来源搜索 基于过滤器/标签发送 - 创建通知