我只需要向我的按钮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) {
}
});
}
});
答案 0 :(得分:3)
要向使用Firebase的特定单个用户发送推送通知,您只需要 FCM注册令牌,这是接收通知的用户设备的唯一标识符。
以下是获取此令牌的Firebase FCM文档:FCM Token registration
基本上:
只有用户ID本身不足以为特定用户发送通知。
答案 1 :(得分:1)
首先,用户必须生成
String token = FirebaseInstanceId.getInstance().getToken();
然后使用userId作为键将其存储在firebase数据库中,或者您可以通过FirebaseMessaging.getInstance().subscribeToTopic("topic");
要发送通知,您必须点击此API https://fcm.googleapis.com/fcm/send,标题为“授权”您的FCM密钥,内容类型为“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密钥会被暴露并且可能被滥用。
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 dataJson = new JSONObject();
dataJson.put("text", body);
dataJson.put("title", title);
dataJson.put("priority", "high");
json.put("notification", 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();
}
}