title: how to create Notification SERVICE for my chat application?
body: <p>So I have created a message application which allows users to communicate with each other. I am using Firebase to do this. now the problem is I want create a notification in the background (when the app is in the background or closed). similar to messenger/whatsapp where you get a notification for unread messages.</p>
  
  <p>how can i create this?</p>
  
  <p>chat-room.java</p>
  
  <pre><code>public class Chat_Room  extends AppCompatActivity{
  
  private Button btn_send_msg;
  private EditText input_msg;
  private TextView chat_conversation;
  
  private String user_name,room_name;
  private DatabaseReference root ;
  private DatabaseReference Mnotification;
  private String temp_key;
  
  
  NotificationCompat.Builder notification;
  private static final int  uniqueID = 1995;
  
  @Override
  protected void onCreate(@Nullable Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.chat_room);
  
      btn_send_msg = (Button) findViewById(R.id.btn_send);
      input_msg = (EditText) findViewById(R.id.msg_input);
      chat_conversation = (TextView) findViewById(R.id.textView);
  
  
  
  
  
  
  
      user_name = getIntent().getExtras().get("user_name").toString();
      room_name = getIntent().getExtras().get("room_name").toString();
      setTitle(" Room - "+room_name);
  
      root = FirebaseDatabase.getInstance().getReference().child(room_name);
      Mnotification = FirebaseDatabase.getInstance().getReference().child("notifications");
  
      btn_send_msg.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View view) {
  
              Map&lt;String,Object&gt; map = new HashMap&lt;String, Object&gt;();
              temp_key = root.push().getKey();
              root.updateChildren(map);
  
              DatabaseReference message_root = root.child(temp_key);
              Map&lt;String,Object&gt; map2 = new HashMap&lt;String, Object&gt;();
              map2.put("name",user_name);
              map2.put("msg",input_msg.getText().toString());
  
              message_root.updateChildren(map2);
              //not();
              //test();
  
          }
      });
  
      root.addChildEventListener(new ChildEventListener() {
          @Override
          public void onChildAdded(DataSnapshot dataSnapshot, String s) {
  
              append_chat_conversation(dataSnapshot);
  
          }
  
          @Override
          public void onChildChanged(DataSnapshot dataSnapshot, String s) {
  
              append_chat_conversation(dataSnapshot);
  
          }
  
          @Override
          public void onChildRemoved(DataSnapshot dataSnapshot) {
  
          }
  
          @Override
          public void onChildMoved(DataSnapshot dataSnapshot, String s) {
  
          }
  
          @Override
          public void onCancelled(DatabaseError databaseError) {
  
          }
  
      });
  
  
  
  
  }
  
  
  public void test() {
  
      String tkn = FirebaseInstanceId.getInstance().getToken();
      String text = chat_msg;
     not();
      Log.d("App", "Token[" + tkn + "]");
  
  
  
  
  
  }
  
  
  public void not(){
  
  
  
      notification = new NotificationCompat.Builder(this);
      notification.setAutoCancel(true);
  
      notification.setSmallIcon(R.drawable.downloadfile);
      notification.setTicker("This is the tocken");
      notification.setWhen(System.currentTimeMillis());
      notification.setContentTitle(user_name);
      notification.setContentText(chat_msg);
  
  
      Intent intent = new Intent(Chat_Room.this, Message.class);
      PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
      notification.setContentIntent(pendingIntent);
  
  
      NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
      nm.notify(uniqueID, notification.build());
  
  
  
  
  
  }
  
  private String chat_msg,chat_user_name;
  
  private void append_chat_conversation(DataSnapshot dataSnapshot) {
  
      Iterator i = dataSnapshot.getChildren().iterator();
  
      while (i.hasNext()){
  
          chat_msg = (String) ((DataSnapshot)i.next()).getValue();
          chat_user_name = (String) ((DataSnapshot)i.next()).getValue();
  
  
  
  
  
  
  
  
  
  
  
  
          chat_conversation.append(chat_user_name +" : "+chat_msg +" \n");
          input_msg.setText("");
          test();
  
  
  
  
  
      }
  
  
  
  }
  </code></pre>
  
  <p>}</p>