I have zero knowledge of Java and Android development. I put this together in a couple of weeks. The code works. But I look forward to some feedback and to know how I can improve it.


I need to send push notifications to a group of users.

I am using OneSignal with the OneSignal Cordova SDK.

When the first user click the action button, I cancel the previously sent notification from all others users devices (the action button is handled in js with the cordova plugin).

To cancel the notification from the device I send a background notification with the id of the notification to cancel to that group of users.

I wrote a service in Java that extend the OneSignal NotificationExtenderService.

It checks the notification type. If it's a normal notification, I will make a POST request to my API to save some data about the notification, including the androidNotificationId (that I will need later if I have to cancel it).

If the notification is silent, I get the id of the notification that has to be cancelled, and I delete it using the notificationManager.cancel() method.


public class NotificationService extends NotificationExtenderService {

  protected boolean onNotificationProcessing(OSNotificationReceivedResult receivedResult) {

    Context ctx = getApplicationContext();

    JSONObject additionalData = receivedResult.payload.additionalData;
    final String notificationType = additionalData.optString("notification_type");

    if (notificationType.equals("silent")) {
      // If notification type is 'silent' then it's a background notification
      try {
        int notificationIdToCancel = additionalData.getInt("notification_id_to_cancel");

        NotificationManager notificationManager = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);

        return true; // in this way the background notification is not displayed to the device

      } catch (JSONException e) {
        //some exception handler code.

      return true;

    } else {

      // Active notification - to be displayed
      final String tableNumber = additionalData.optString("table_number");
      final String customId = additionalData.optString("notification_id_custom");
      final String projectId = additionalData.optString("project_id");

      // Override notification builder - for design only
      OverrideSettings overrideSettings = new OverrideSettings();
      overrideSettings.extender = new NotificationCompat.Extender() {
        public NotificationCompat.Builder extend(NotificationCompat.Builder builder) {
          // Sets the background notification color to Green on Android 5.0+ devices.
          return builder.setColor(Color.parseColor("#7BE1B1"))

      OSNotificationDisplayedResult displayedResult = displayNotification(overrideSettings);
      Log.d("OneSignal", "Notification displayed with id: " + displayedResult.androidNotificationId);

     // Make POST request - with notification data
       URL url = new URL("http://MYAPI.com/save_notification");

          // Init connection
          URLConnection con = url.openConnection();
          HttpURLConnection http = (HttpURLConnection)con;
          http.setRequestMethod("POST"); // PUT is another valid option

          // Data to send to API
          String dataApi = "{\"notification_id_custom\":\"" + customId + "\",\"project_id\":\"" + projectId + "\",\"android_notification_id\":\"" + displayedResult.androidNotificationId + "\"}";

          // Send JSON request
          byte[] out = dataApi.getBytes(StandardCharsets.UTF_8);
          int length = out.length;

          http.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
          try(OutputStream os = http.getOutputStream()) {

            // Get response from API - for debug only
            int responseCode = http.getResponseCode();
            System.out.println("POST Response Code : " + responseCode);

            if (responseCode == HttpURLConnection.HTTP_OK) { //success
              BufferedReader in = new BufferedReader(new InputStreamReader(
              String inputLine;
              StringBuffer response = new StringBuffer();

              while ((inputLine = in.readLine()) != null) {
            } else {
              System.out.println("POST request not worked");

            return false; // in this way the notification is going to be displayed in the device
        }catch(IOException ex){
          // exception handling here
      }catch(MalformedURLException ex){
        //do exception handling here
      return false;


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.