0
public class NotificationReceivedCheckDelivery extends NotificationExtenderService {
   @Override
   protected boolean onNotificationProcessing(OSNotificationReceivedResult receivedResult) {
      OverrideSettings overrideSettings = new OverrideSettings();

      overrideSettings.extender = new NotificationCompat.Extender() {
         @Override
         public NotificationCompat.Builder extend(NotificationCompat.Builder builder) {
            // Sets the background notification color to Yellow on Android 5.0+ devices.
            return builder.setColor(new BigInteger("FFFFEC4F", 16).intValue());
         }
      };

      OSNotificationDisplayedResult displayedResult = displayNotification(overrideSettings);
    Log.d("ONES",receivedResult.payload.title);
      JSONObject AdditionalData = receivedResult.payload.additionalData;
        Log.d("Adata",AdditionalData.toString());

      String uuid= null;
      try{
        //   {"uuid":"adddd"}
          uuid = AdditionalData.getString("uuid");

      }
      catch (JSONException e){
          Log.e("Error JSON","UUID",e);
      }



                // Create Object and call AsyncTask execute Method
                new FetchNotificationData().execute(uuid);

      return true;
   }



private class FetchNotificationData extends AsyncTask<String,Void, String> {

        @Override
        protected String doInBackground(String... uuids) {
            // These two need to be declared outside the try/catch
            // so that they can be closed in the finally block.
            HttpURLConnection urlConnection = null;
            BufferedReader reader = null;

            // Will contain the raw JSON response as a string.
            String forecastJsonStr = null;

            try {

                URL url = new URL("http://test.com/AppDeliveryReport?uuid="+uuids[0]);


                urlConnection = (HttpURLConnection) url.openConnection();
                urlConnection.setRequestMethod("GET");
                urlConnection.connect();

                // Read the input stream into a String
                InputStream inputStream = urlConnection.getInputStream();
                StringBuffer buffer = new StringBuffer();
                if (inputStream == null) {
                    // Nothing to do.
                    return null;
                }
                reader = new BufferedReader(new InputStreamReader(inputStream));

                String line;
                while ((line = reader.readLine()) != null) {
                    // Since it's JSON, adding a newline isn't necessary (it won't affect parsing)
                    // But it does make debugging a *lot* easier if you print out the completed
                    // buffer for debugging.
                    buffer.append(line + "\n");
                }

                if (buffer.length() == 0) {
                    // Stream was empty.  No point in parsing.
                    return null;
                }
                forecastJsonStr = buffer.toString();
                return forecastJsonStr;
            } catch (IOException e) {
                Log.e("PlaceholderFragment", "Error ", e);
                // If the code didn't successfully get the weather data, there's no point in attemping
                // to parse it.
                return null;
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            } finally {
                if (urlConnection != null) {
                    urlConnection.disconnect();
                }
                if (reader != null) {
                    try {
                        reader.close();
                    } catch (final IOException e) {
                        Log.e("PlaceholderFragment", "Error closing stream", e);
                    }
                }
            }

        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            Log.i("json", s);
        }
}
}

I want to delay calling the FetchNotificationData function with a random seconds. This is a delivery report url request function. Whenever a notification from onesignal received at the app it will call the url. I don't want to blast the server with huge request at once. So I want to delay call with random seconds so that server will have to serve few calls on a given time.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
AH.
  • 741
  • 1
  • 12
  • 27

3 Answers3

4

You can use handler to delay call to function

  new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(Splash.this, "I will be called after 2 sec", 
                                                              Toast.LENGTH_SHORT).show();
                    //Call your Function here..
                }
            }, 2000); // 2000 = 2 sec
Ali Ahmed
  • 2,130
  • 1
  • 13
  • 19
3

you can use Handler like this

  Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
               // your FetchNotificationData function
            }
        },timeInMiliSec);

just remember to import Handler as android.os, not java.util.logging

Mohamad Rostami
  • 420
  • 3
  • 17
0
 timer = new Timer();
 final int FPS = 3;
 TimerTask updateBall = new UpdateBallTask();
 timer.scheduleAtFixedRate(updateBall, 0, 1000 * FPS);

class:

class UpdateBallTask extends TimerTask {
    public void run() {
        // do work
    }
}

///OR

  final Handler handler = new Handler();
    final Runnable r = new Runnable() {
        public void run() {
           handler.postDelayed(this, 100);
           // do work
                handler.removeCallbacksAndMessages(null);          
        }
    };
   handler.postDelayed(r, 100);
Mehrdad
  • 1,477
  • 15
  • 17