使用 OneSignal 发送通知 b/w 台设备
Sending Notification b/w devices using OneSignal
您好,我正在尝试在 android 应用的两个用户 A 和 B 之间发送推送通知。使用 OneSignal 网站是一种手动方式,我想通过应用程序本身发送通知,比如用户 A 按下按钮,通知将发送给用户 B。
任何帮助将不胜感激。
要使用 OneSignal 发送自定义通知,您需要使用 OneSignal 的授权和通知结构 URL 可以与您分享我的代码。
https://onesignal.com/api/v1/notifications
传递这些headers
Content-Type application/json; charset=UTF-8
Authorization Basic <your-rest-client-key>
在下面 JSON 设置到您的 body
{
"app_id": "<your-app-id>",
"included_segments": ["All"],
"content_available":"true",
"data": {"foo": "bar"},
"contents": {"en": "Test_Message_Body"},
"headings": {"en": "Test_Message_Title"}
}
这是来自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();
}
使用Java代码:-
其中userId是receiver
的唯一注册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 是 receiver
的唯一注册 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");
?>
我的声誉很低,所以我正在回答而不是评论。
thanks got it working , now I just need to figure out how to target a specific user . – Ali Lal Din
关于如何定位特定用户,您可以删除 included_segments
属性并改为发送 include_player_ids
。
您好,我正在尝试在 android 应用的两个用户 A 和 B 之间发送推送通知。使用 OneSignal 网站是一种手动方式,我想通过应用程序本身发送通知,比如用户 A 按下按钮,通知将发送给用户 B。 任何帮助将不胜感激。
要使用 OneSignal 发送自定义通知,您需要使用 OneSignal 的授权和通知结构 URL 可以与您分享我的代码。
https://onesignal.com/api/v1/notifications
传递这些headers
Content-Type application/json; charset=UTF-8
Authorization Basic <your-rest-client-key>
在下面 JSON 设置到您的 body
{
"app_id": "<your-app-id>",
"included_segments": ["All"],
"content_available":"true",
"data": {"foo": "bar"},
"contents": {"en": "Test_Message_Body"},
"headings": {"en": "Test_Message_Title"}
}
这是来自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();
}
使用Java代码:- 其中userId是receiver
的唯一注册idtry {
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 是 receiver
的唯一注册 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");
?>
我的声誉很低,所以我正在回答而不是评论。
thanks got it working , now I just need to figure out how to target a specific user . – Ali Lal Din
关于如何定位特定用户,您可以删除 included_segments
属性并改为发送 include_player_ids
。