How can i send push notification to specific users just knowing the userID inside Button OnClickListener? Firebase

user8593304 picture user8593304 · Oct 2, 2017 · Viewed 9.5k times · Source

I just need to send a push notification to a specific users inside my Button OnClickListener. Is it possible with userId and all information of this specific user?

this is my Button OnClickListener() code

richiedi_invito.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                databaseReference = FirebaseDatabase.getInstance().getReference();

                databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        lista_richieste = (ArrayList) dataSnapshot.child("classi").child(nome).child("lista_richieste").getValue();
                        verifica_richieste = (String) dataSnapshot.child("classi").child(nome).child("richieste").getValue();




                        if (!lista_richieste.contains(userID)){
                            ArrayList lista_invito = new ArrayList();
                            lista_invito.add(userID);
                            if (verifica_richieste.equals("null")){
                                databaseReference.child("classi").child(nome).child("richieste").setValue("not_null");
                                databaseReference.child("classi").child(nome).child("lista_richieste").setValue(lista_invito);


                            }
                            else{
                                lista_richieste.add(userID);
                                databaseReference.child("classi").child(nome).child("lista_richieste").setValue(lista_richieste);

                            }


                            //invitation code here



                            Fragment frag_crea_unisciti = new CreaUniscitiFrag();
                            FragmentManager fragmentManager= getFragmentManager();
                            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                            fragmentTransaction.replace(R.id.fragment_container, frag_crea_unisciti);
                            fragmentTransaction.addToBackStack(null);
                            fragmentTransaction.commit();

                            Toast.makeText(getActivity(), "Richiesta di entrare inviata correttamente", Toast.LENGTH_SHORT).show();

                        }else{
                            Snackbar.make(layout,"Hai già richiesto di entrare in questa classe",Snackbar.LENGTH_SHORT).show();


                    }
                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {

                    }
                });




            }
        });

Answer

Arrabidas92 picture Arrabidas92 · Oct 2, 2017

To send a push notification to a specific single user with Firebase, you only need is FCM registration token which is the unique identifier of the user device to receive the notification.

Here is Firebase FCM documentation to get this token : FCM Token registration

Basically :

  • You get a FCM token for the user
  • Then you store this FCM token on your server or database by associating this FCM token with the user ID for example.
  • When you need to send a notification, you can retrieve this FCM token stored on your server or database with the user id and use Firebase Cloud Functions. Here is a specific case study to send a notification for a specific user : Cloud Functions

Only the user id itself isn't enough to send a notification for a specific user.