您好我正在尝试在两个用户之间发送推送通知A&我的Android应用程序的B.使用OneSignal网站是一种手动方式,我想通过应用程序本身发送通知,比如用户A按下按钮并向用户B发送通知。 任何帮助将不胜感激。
答案 0 :(得分:1)
要使用OneSignal发送自定义通知,您需要使用OneSignal URL的授权和通知结构可以与您共享我的代码。
https://onesignal.com/api/v1/notifications
传递这些标题
Content-Type application/json; charset=UTF-8
Authorization Basic <your-rest-client-key>
将JSON设置为您的身体
{
"app_id": "<your-app-id>",
"included_segments": ["All"],
"content_available":"true",
"data": {"foo": "bar"},
"contents": {"en": "Test_Message_Body"},
"headings": {"en": "Test_Message_Title"}
}
答案 1 :(得分:0)
这是OneSignal官方博客中使用过滤器定位特定用户的代码。这有助于我解决我的问题。
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 ZWY0YTU2YTItMjUzMC00NGY3LThiNTQtODFiY2U1NjQ5NmZj");
con.setRequestMethod("POST");
//////////////////////////////// --> Apply Search Criteria Filters Here <-- /////////////////////////
String strJsonBody = "{"
+ "\"app_id\": \"5eb5a37e-b458-11e3-ac11-000c2940e62c\","
+ "\"filters\": [{\"field\": \"tag\", \"key\": \"" + himID + "\", \"relation\": \"=\", \"value\": " +
"\"himID\"},{\"operator\": \"OR\"},{\"field\": \"amount_spent\", \"relation\": \">\",\"value\": \"0\"}],"
+ "\"data\": {\"foo\": \"bar\"},"
+ "\"contents\": {\"en\": \"One Signal Notification Test\"}"
+ "}";
///////////////////////////////////////////////////////////////////////////////////////////////////
Log.d("Query Check->"," Query Check-> jsonResponse:\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();
}
Log.d("Query Check->"," Query Check-> jsonResponse:\n" + jsonResponse);
} catch(Throwable t) {
t.printStackTrace();
}
答案 2 :(得分:0)
使用Java代码: - 其中userId是接收方的唯一注册ID
try {
OneSignal.postNotification(new JSONObject("{'contents': {'en': '"+ msg_welcome +"'}, 'include_player_ids': ['" + userId + "']}"),
new OneSignal.PostNotificationResponseHandler() {
@Override
public void onSuccess(JSONObject response) {
Log.i("OneSignalExample", "postNotification Success: " + response.toString());
}
@Override
public void onFailure(JSONObject response) {
Log.e("OneSignalExample", "postNotification Failure: " + response.toString());
}
});
} catch (JSONException e) {
e.printStackTrace();
}
使用PHP: - 其中$ device_id是接收者的唯一注册ID
<?PHP
function sendMessage($device_id,$msg_title,$msg_body,$msg_img){
$content = array(
"en" => $msg_body
);
$heading = array(
"en" => $msg_title
);
// $device_id = "da2e72a0-6af7-4102-819e-4b7db5XXXXXX";
$include_player_id = array(
$device_id
);
$fields = array(
'app_id' => "YOUR_APP_ID",
'contents' => $content,
'headings' => $heading,
'data' => array("foo" => "bar"),
'small_icon'=> "ic_launcher",
'large_icon'=> "ic_launcher",
'big_picture'=> $msg_img,
'include_player_ids' => $include_player_id
);
$fields = json_encode($fields);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://onesignal.com/api/v1/notifications");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json',
'Authorization: Basic YOUR_REST_API_KEY'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
$response = sendMessage();
$return["allresponses"] = $response;
$return = json_encode( $return);
print("\n\nJSON received:\n");
print($return);
print("\n");
?>
答案 3 :(得分:0)
我的声誉很低,所以我要回答而不是发表评论。
感谢它的成功,现在我只需要弄清楚如何针对特定用户。 –阿里·拉尔丁(Ali Lal Din)
关于如何定位特定用户,您可以删除included_segments
属性并发送include_player_ids
。