我如何只知道Button OnClickListener内的userID即可向特定用户发送推送通知?火力基地

0 java android firebase firebase-authentication firebase-notifications

我只需要向Button内的特定用户发送推送通知即可OnClickListeneruserId此特定用户能否获得全部信息?

这是我的Button OnClickListener()代码

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) {

                    }
                });




            }
        });
Run Code Online (Sandbox Code Playgroud)

kun*_*r97 6

首先,用户必须生成 String token = FirebaseInstanceId.getInstance().getToken(); 并将其存储在 Firebase 数据库中,以 userId 为键,或者您可以通过以下方式为用户订阅任何主题 FirebaseMessaging.getInstance().subscribeToTopic("topic");

要发送通知,您必须点击此 API:https : //fcm.googleapis.com/fcm/send

使用标题“授权”,您的 FCM 密钥和内容类型为“应用程序/json”,请求正文应为:

{ 
  "to": "/topics or FCM id",
  "priority": "high",
  "notification": {
    "title": "Your Title",
    "text": "Your Text"
  },
  "data": {
    "customId": "02",
    "badge": 1,
    "sound": "",
    "alert": "Alert"
  }
}
Run Code Online (Sandbox Code Playgroud)

或者您可以使用不推荐的 okHttp 方法,因为您的 FCM 密钥将被公开并可能被滥用。

public class FcmNotifier {

    public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");

    public static void sendNotification(final String body, final String title) {
        new AsyncTask<Void, Void, Void>() {
            @Override
            protected Void doInBackground(Void... params) {
                try {
                    OkHttpClient client = new OkHttpClient();
                    JSONObject json = new JSONObject();
                    JSONObject notifJson = new JSONObject();
                    JSONObject dataJson = new JSONObject();
                    notifJson.put("text", body);
                    notifJson.put("title", title);
                    notifJson.put("priority", "high");
                    dataJson.put("customId", "02");
                    dataJson.put("badge", 1);
                    dataJson.put("alert", "Alert");
                    json.put("notification", notifJson);
                    json.put("data", dataJson);
                    json.put("to", "/topics/topic");
                    RequestBody body = RequestBody.create(JSON, json.toString());
                    Request request = new Request.Builder()
                            .header("Authorization", "key=your FCM key")
                            .url("https://fcm.googleapis.com/fcm/send")
                            .post(body)
                            .build();
                    Response response = client.newCall(request).execute();
                    String finalResponse = response.body().string();
                    Log.i("kunwar", finalResponse);
                } catch (Exception e) {

                    Log.i("kunwar",e.getMessage());
                }
                return null;
            }
        }.execute();
    }
}
Run Code Online (Sandbox Code Playgroud)

注意:不推荐使用此解决方案,因为它会将 Firebase API 密钥暴露给公众


Arr*_*s92 5

要将推送通知发送给具有Firebase的特定单个用户,您只需要FCM注册令牌,这是接收通知的用户设备的唯一标识符。

这是获得此令牌的Firebase FCM文档:FCM令牌注册

基本上:

  • 您会为用户获得FCM令牌
  • 然后,通过将此FCM令牌与用户ID相关联,将该FCM令牌存储在服务器或数据库上。
  • 当您需要发送通知时,您可以使用用户ID检索存储在服务器或数据库中的FCM令牌,并使用Firebase Cloud Functions。这是为特定用户发送通知的特定案例研究:Cloud Functions

仅用户ID本身不足以发送特定用户的通知。