title: automatically background notification firebase database
body: <p><br/>
  I developed an application to my school and one of the features is principal's updates. For this, I'm using the Firebase realtime database. I need that the app will send automatically notification(background) when a new child(message) is added.<br/><br/>
  this is my read code(swift):</p>
  
  <pre><code>var messages = [Message]()
  
  override func viewDidLoad() {
      super.viewDidLoad()
  
      observeMessages()
  }
  
  func observeMessages() {
      let dbRef = FIRDatabase.database().reference().child("messageS")
      dbRef.observe(.childAdded, with: { (snapshot) in
  
          if let dictionary = snapshot.value as? [String: AnyObject] {
              let text = Message()
              text.setValuesForKeys(dictionary)
              self.messages.insert(text, at: 0)
  
  
              DispatchQueue.main.async(execute: {
                  self.tableView.reloadData()
              })
          }
  
          }, withCancel: nil)
  }
  
  
  override func didReceiveMemoryWarning() {
      super.didReceiveMemoryWarning()
  }
  
  // MARK: - Table view data source
  
  override func numberOfSections(in tableView: UITableView) -&gt; Int {
      return 1
  }
  
  override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {
      return messages.count
  }
  
  
  override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell {
      let cell = UITableViewCell(style: .subtitle, reuseIdentifier: "cellId")
  
      let message = messages[indexPath.row]
      cell.textLabel?.text = message.title
      cell.textLabel?.textAlignment = .center
      cell.detailTextLabel?.text = message.message
      cell.detailTextLabel?.textAlignment = .right
  
      // Configure the cell...
  
      return cell
  }
  
  override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  
  
      let message = messages[indexPath.row]
      let title = message.title
      let msg = message.message
      let controller = UIAlertController(title: title, message: msg, preferredStyle: .alert)
      let cancelAction = UIAlertAction(title: "סגור", style: .cancel) { (handler) in
          // Any action when you cancel
      }
      controller.addAction(cancelAction)
      self.present(controller, animated: true, completion: nil)
  
  }
  </code></pre>
  
  <p>this is my write code(swift):</p>
  
  <pre><code>@IBOutlet weak var MessageTitle: UITextField!
  @IBOutlet weak var MessageText: UITextField!
  override func viewDidLoad() {
      super.viewDidLoad()
      // Do any additional setup after loading the view.
  }
  
  func post() {
      let title: String = MessageTitle.text!
      let message: String = MessageText.text!
  
      let post: [String: AnyObject] = ["title": title as AnyObject,
                                      "message": message as AnyObject]
      let dbRef = FIRDatabase.database().reference()
      dbRef.child("messageS").childByAutoId().setValue(post)
  
  
  
  }
  @IBAction func Publish(_ sender: AnyObject) {
      post()
  
  
  
      let MessageSent = UIAlertController(title: "נשלח", message: "ההודעה נשלחה בהצלחה", preferredStyle: UIAlertControllerStyle.alert)
      MessageSent.addAction(UIAlertAction(title: "תודה", style: UIAlertActionStyle.default, handler: nil))
      self.present(MessageSent, animated: true, completion: nil)
  
  }
  </code></pre>
  
  <p>this is my read code(android):</p>
  
  <pre><code>private Firebase mRef;
  public ArrayList&lt;String&gt; messages = new ArrayList&lt;&gt;();
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_updates);
  
      mRef = new Firebase("https://ksharet-d6a24.firebaseio.com");
      mRef.child("messageS");
      // Get ListView object from xml
      final ListView listView = (ListView) findViewById(R.id.ListView);
  
      // Create a new Adapter
      final ArrayAdapter&lt;String&gt; adapter = new ArrayAdapter&lt;&gt;(this,
              android.R.layout.simple_list_item_1, messages);
  
      // Assign adapter to ListView
      listView.setAdapter(adapter);
  
  
      mRef.addChildEventListener(new ChildEventListener() {
          @Override
          public void onChildAdded(DataSnapshot dataSnapshot, String s) {
              for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
                  Message message = postSnapshot.getValue(Message.class);
                  messages.add(0, message.getTitle() + '\n' + message.getMessage());
              }
              adapter.notifyDataSetChanged();
  
          }
  
          @Override
          public void onChildChanged(DataSnapshot dataSnapshot, String s) {
          }
  
          @Override
          public void onChildRemoved(DataSnapshot dataSnapshot) {
          }
  
          @Override
          public void onChildMoved(DataSnapshot dataSnapshot, String s) {
          }
  
          @Override
          public void onCancelled(FirebaseError firebaseError) {
          }
      });
  }
  </code></pre>
  
  <p>this is my write code(android):</p>
  
  <pre><code>private Button sendData;
  private Firebase mRef;
  private EditText messageBox = (EditText) findViewById(R.id.MessageText);
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_publish);
  
      Firebase.setAndroidContext(this);
  
      mRef = new Firebase("https://ksharet-d6a24.firebaseio.com/");
      mRef.child("message");
      sendData = (Button) findViewById(R.id.sendMessage);
  
      sendData.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
  
              String message = messageBox.getText().toString();
              //Firebase mRefChild = mRef.child("message");
              //mRefChild.setValue(message);
              mRef.push().setValue(message);
  
  
          }
      });
  }
  </code></pre>
  
  <p>what should I create or change in the code(if you can, for both iOS and Android).<br/>
  I recently purchased the Apple Developer Program membership and It'll probably be activated soon.</p>
  
  <p>thanks for the helpers.</p>