Push Notification from Android without using OneSignal Dashboard
I have come across various articles regarding people asking the method to be able to push notifications (using OneSignal SDK/API) directly from the android device without having to use the OneSignal Dashboard. The OneSignal documentation page about creating notifications (https://documentation.onesignal.com/reference/create-notification) has several methods listed to complete the task. But the way android developers have to implement is using a JSON Object request. The documentation has very good examples of how to add the JSON body.
However, The methods like sending to segments, & tags requires the OneSignal REST API KEY passed as a header during the JSON request.
But no examples regarding how to add the REST API KEY as HTTP header in JSON requests are present on the documentation page which has led many newbies' to face trouble while adding this amazing SDK to their android application. Here's the way to complete the task using Android's Volley Library:
- Follow OneSignal's documentation to set-up your project.
- Once your project is set, add the Android Volley Library dependency in the Gradle's (App: Module) dependency section.
- Create a new JSON request using the Volley Library on your required activity. You can refer to this code below to make a POST using 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();
}
Using Segments for notification push also requires OneSignal's Dashboard. It can be avoided by passing tags to targeted users in the code.
JSONObject tags = new JSONObject();
try {
tags.put("key","value");
//for the above JSON request the following key value pair have been used
// 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);
This completes the method of sending notifications to android devices without having to use the OneSignal Dashboard.
I'm curious