title: How to create notifications while app is minimized in android?
body: <p>I have been working on this code using the <code>NotificationManager</code> class to make a notification. </p>
  
  <p>However, I want to display notifications even when the app is in background. Any ideas?</p>
  
  <pre><code>  @Override
    protected void onPostExecute(String result) {
        try
        {
             Thread t=new Thread()
            {
              public void run()
              {
                  try
                  {
  
                          JSONObject jsonResponse = new JSONObject(jsonResult);
                          JSONArray jsonMainNode = jsonResponse.optJSONArray("user");
                          for (int i = 0; i &lt; jsonMainNode.length(); i++) 
                          {
                              JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
                              String name = jsonChildNode.optString("userid");
                              String number = jsonChildNode.optString("unreadmesage");
                              Thread.sleep(5000);
                              NotificationManager nm=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
                              Notification n=new Notification(R.drawable.mainicon,"New Msg From ASKCOTTON",System.currentTimeMillis());
                              //n.defaults=n.FLAG_ONLY_ALERT_ONCE+Notification.FLAG_AUTO_CANCEL;
                              Intent in=new Intent(getApplicationContext(),message.class);
                              PendingIntent pi=PendingIntent.getActivity(getApplicationContext(), 0, in, 0);
                             // n.flags |= Notification.FLAG_ONGOING_EVENT;
                              n.setLatestEventInfo(getBaseContext(), "ASKCOTTON", "You Have New Message(s)", pi);
                              nm.notify(1,n);
                          }
                  }catch(Exception e)
                  {
                      e.printStackTrace();
                  }
              }
            };t.start();
  
        }catch(Exception e)
        {
  
        }
    }
  </code></pre>