要在不使用 OneSignal 仪表板的情况下发送通知,您可以将标签附加到用户并通过使用 URL https://onesignal.com/api/v1/notifications 进行 POST 向他们发送通知
只能使用 OneSignal 仪表板创建分段,但可以使用 SDK/API 创建标签。
添加标题"Content-Type":"application/json" 和"Authorization":"Basic <REST API Key>"
在正文中添加
{
"app_id":"<App Id>",
"headings":{
"en":"Title Here"
},
"contents":{
"en":"Description Here"
},
"filters":[
{
"field":"tag",
"key":"level",
"relation":"=",
"value":"10"
},
{
"operator":"OR"
},
{
"field":"amount_spent",
"relation":">",
"value":"0"
}
]
}
然后发出 JSON 对象请求以完成该过程。
您可以参考以下代码来了解如何将标头附加到 JSON 对象请求以及如何将标签附加到您的用户。 (此请求是使用 android volley 发出的)
String url = "https://onesignal.com/api/v1/notifications";
JSONObject jsonBody;
try {
jsonBody = new JSONObject(
"{'app_id':'app-id'," +
"'headings': {'en': 'title'}, " +
"'contents': {'en': 'message'}," +
"'filters':[{'field':'tag','key':'"+id+"','relation':'=','value':'true'}]}"
);
//request a json object response
JsonObjectRequest jsonRequest = new JsonObjectRequest(Request.Method.POST, url, jsonBody, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
//now handle the response
Toast.makeText(Activity.this, "Notification successfully sent", Toast.LENGTH_SHORT).show();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
//handle the error
Toast.makeText(Activity.this, "An error occurred", Toast.LENGTH_SHORT).show();
error.printStackTrace();
}
})
{ //adding header to the request
@Override
public Map<String, String> getHeaders() {
Map<String, String> params = new HashMap<String, String>();
params.put("Authorization", "Basic <REST API KEY>");
params.put("Content-type", "application/json");
return params;
}
};
// Add the request to the queue
Volley.newRequestQueue(Activity.this).add(jsonRequest);
} catch (JSONException e) {
e.printStackTrace();
}
为用户添加标签
JSONObject tags = new JSONObject();
try {
tags.put("key","value");
//for the above JSON request I have used the following key value pair
// tags.put(id,true);
// where id is a string containing the userId
//true is a boolean value
} catch (JSONException e) {
e.printStackTrace();
}
OneSignal.sendTags(tags);
这应该会成功完成您的查询。