gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 23:29:33 24 4
gpt4 key购买 nike

我只需要向Button OnClickListener中的特定用户发送推送通知。 userId和该特定用户的所有信息是否可能?

这是我的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) {

}
});




}
});

最佳答案

首先,用户必须生成String token = FirebaseInstanceId.getInstance().getToken();然后以userId为键将其存储在Firebase数据库中,或者您可以通过 FirebaseMessaging.getInstance().subscribeToTopic("topic");将用户订阅到任何主题
要发送通知,您必须点击以下API:https://fcm.googleapis.com/fcm/send
使用 header “Authorization”将您的FCM key 和Content-Type设置为“application/json”时,请求正文应为:

{ 
"to": "/topics or FCM id",
"priority": "high",
"notification": {
"title": "Your Title",
"text": "Your Text"
},
"data": {
"customId": "02",
"badge": 1,
"sound": "",
"alert": "Alert"
}
}
或者您可以使用不推荐使用的okHttp方法,因为您的FCM key 将被公开并且可能被滥用。
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();
}
}
注意:不建议使用此解决方案,因为它公开了公开的FIREBASE API key

关于java - 我如何只知道Button OnClickListener内的userID即可向特定用户发送推送通知?火力基地,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46526559/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com