Reputation: 53
Hello I'm trying to send Push notification between two users A & B of my android app. Using the OneSignal Website is a manual way and I want to send notification via the app itself , say user A Presses a button and a notification is sent to user B. Any help will be appreciated.
Upvotes: 3
Views: 3920
Reputation: 1
I have low reputation so I am answering instead commenting.
thanks got it working , now I just need to figure out how to target a specific user . – Ali Lal Din
On how to target a specific user, you can remove included_segments
attribute and send include_player_ids
instead.
Upvotes: 0
Reputation: 1295
Using Java Code:- where userId is the unique registration id of reciever
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();
}
Using PHP :- where $device_id is the unique registration id of reciever
<?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");
?>
Upvotes: 1
Reputation: 53
This is the code from OneSignal official blog to target specific users by using filters. This helped me solve my problem.
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();
}
Upvotes: 1
Reputation: 3203
For sending custom notification using OneSignal you need to Authorization and notification structure with OneSignal URL may share my code with you.
https://onesignal.com/api/v1/notifications
Passing these headers
Content-Type application/json; charset=UTF-8
Authorization Basic <your-rest-client-key>
set below JSON into your 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"}
}
Upvotes: 1