-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCreatePost.java
181 lines (143 loc) · 6.84 KB
/
CreatePost.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package com.android.BloodBank;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
import com.android.BloodBank.Model.GeoCodeLocation;
import com.android.BloodBank.Model.Post;
import com.android.BloodBank.Notifications.Client;
import com.android.BloodBank.Notifications.Data;
import com.android.BloodBank.Notifications.MyResponse;
import com.android.BloodBank.Notifications.Sender;
import com.android.BloodBank.Notifications.Token;
import com.android.BloodBank.UI.HomePageActivity;
import com.android.BloodBank.databinding.ActivityCreatePostBinding;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import com.google.firebase.firestore.DocumentReference;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.EventListener;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.FirebaseFirestoreException;
import com.google.firebase.firestore.SetOptions;
import com.google.firebase.iid.FirebaseInstanceId;
import java.text.DateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
public class CreatePost extends AppCompatActivity {
String date;
String time;
String Id;
String postMessage;
String Location;
String BloodGroup;
String Contact;
private APIService apiService;
private FirebaseUser firebaseUser;
private FirebaseFirestore db = FirebaseFirestore.getInstance();
ActivityCreatePostBinding binding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityCreatePostBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
//{notification} Api Service
apiService = Client.getClient("https://fcm.googleapis.com/").create(APIService.class);
// //for notification
updateToken(FirebaseInstanceId.getInstance().getToken());
clickListener();
}
private void gatherInformation(){
Date currentTime = Calendar.getInstance().getTime();
date = DateFormat.getDateTimeInstance().format(currentTime).toString();
long mtime = Calendar.getInstance().getTimeInMillis();
time = mtime + "";
Id = FirebaseAuth.getInstance().getCurrentUser().getUid();
postMessage = binding.MessageBody.getText().toString();
BloodGroup = binding.BloodGroup.getText().toString();
Location = binding.Location.getText().toString();
Contact = binding.ContactDetails.getText().toString();
DocumentReference docRef = db.collection("Users").document(FirebaseAuth.getInstance().getCurrentUser().getUid());
docRef.addSnapshotListener(new EventListener<DocumentSnapshot>() {
@SuppressLint("SetTextI18n")
@Override
public void onEvent(@Nullable DocumentSnapshot value, @Nullable FirebaseFirestoreException error) {
String Name = (String) value.get("userName");
sendNotification(Name,postMessage);
}
});
}
private void clickListener(){
binding.post.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
gatherInformation();
if(binding.MessageBody.getText().toString().isEmpty() || binding.BloodGroup.getText().toString().isEmpty() || binding.Location.getText().toString().isEmpty() || binding.ContactDetails.getText().toString().isEmpty())
Toast.makeText(getApplicationContext(),"Please fill all the data", Toast.LENGTH_SHORT).show();
else
{ //Adding the user phone no and type to the fireStore database
Post post = new Post(date,time,Id,postMessage,Location,BloodGroup,Contact);
db.collection("Posts").add(post);
finish();
}
}
});
}
//notification
private void sendNotification( String userName, String msg) {
DatabaseReference tokens = FirebaseDatabase.getInstance().getReference("Tokens");
tokens.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
for(DataSnapshot snapshot1: snapshot.getChildren()){
Token token = snapshot1.getValue(Token.class);
String Key = snapshot1.getKey();
if(!Key.equals(FirebaseAuth.getInstance().getCurrentUser().getUid())) {
Data data = new Data(firebaseUser.getUid(), R.drawable.ic_notification, userName + ": " + msg, "New Post", Key);
Sender sender = new Sender(data, token.getToken());
apiService.sendNotification(sender)
.enqueue(new Callback<MyResponse>() {
@Override
public void onResponse(Call<MyResponse> call, Response<MyResponse> response) {
if (response.code() == 200) {
if (response.body().success != 1) {
Toast.makeText(getApplicationContext(), "TOKEN INVALID", Toast.LENGTH_SHORT).show();
}
}
}
@Override
public void onFailure(Call<MyResponse> call, Throwable t) {
}
});
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
//notification
private void updateToken(String token){
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Tokens");
Token token1 = new Token(token);
reference.child(firebaseUser.getUid()).setValue(token1);
}
}