mirror of
https://github.com/Nezumi-2711/PRM392.git
synced 2026-09-22 20:00:53 +00:00
Fix Notification
This commit is contained in:
@@ -9,6 +9,7 @@ import androidx.recyclerview.widget.RecyclerView;
|
||||
|
||||
import android.content.DialogInterface;
|
||||
import android.os.Bundle;
|
||||
import android.util.Log;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.MenuItem;
|
||||
import android.view.View;
|
||||
@@ -17,13 +18,22 @@ import android.widget.LinearLayout;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
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.Query;
|
||||
import com.google.firebase.database.ValueEventListener;
|
||||
import com.rengwuxian.materialedittext.MaterialEditText;
|
||||
import com.waterbase.foodify.Common.Common;
|
||||
import com.waterbase.foodify.Database.Database;
|
||||
import com.waterbase.foodify.Model.MyResponse;
|
||||
import com.waterbase.foodify.Model.Notification;
|
||||
import com.waterbase.foodify.Model.Order;
|
||||
import com.waterbase.foodify.Model.Request;
|
||||
import com.waterbase.foodify.Model.Sender;
|
||||
import com.waterbase.foodify.Model.Token;
|
||||
import com.waterbase.foodify.Remote.APIService;
|
||||
import com.waterbase.foodify.ViewHolder.CartAdapter;
|
||||
|
||||
import java.text.NumberFormat;
|
||||
@@ -32,6 +42,9 @@ import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
import info.hoang8f.widget.FButton;
|
||||
import retrofit2.Call;
|
||||
import retrofit2.Callback;
|
||||
import retrofit2.Response;
|
||||
|
||||
public class Cart extends AppCompatActivity {
|
||||
|
||||
@@ -47,11 +60,16 @@ public class Cart extends AppCompatActivity {
|
||||
List<Order> cart = new ArrayList<>();
|
||||
CartAdapter adapter;
|
||||
|
||||
APIService mService;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_cart);
|
||||
|
||||
//Init
|
||||
mService = Common.getFCMService();
|
||||
|
||||
//Firebase
|
||||
database = FirebaseDatabase.getInstance();
|
||||
requests = database.getReference("Requests");
|
||||
@@ -116,12 +134,15 @@ public class Cart extends AppCompatActivity {
|
||||
|
||||
//Summit to Firebase
|
||||
//We will using System.CurrentMilli to key
|
||||
requests.child(String.valueOf(System.currentTimeMillis())).setValue(request);
|
||||
String order_number = String.valueOf(System.currentTimeMillis());
|
||||
requests.child(order_number).setValue(request);
|
||||
|
||||
sendNotificationOrder(order_number);
|
||||
|
||||
//Delete Cart
|
||||
new Database(getBaseContext()).cleanCart();
|
||||
Toast.makeText(Cart.this, "Đặt hàng thành công!", Toast.LENGTH_SHORT).show();
|
||||
finish();
|
||||
// new Database(getBaseContext()).cleanCart();
|
||||
// Toast.makeText(Cart.this, "Đặt hàng thành công!", Toast.LENGTH_SHORT).show();
|
||||
// finish();
|
||||
}
|
||||
});
|
||||
|
||||
@@ -135,6 +156,47 @@ public class Cart extends AppCompatActivity {
|
||||
alertDialog.show();
|
||||
}
|
||||
|
||||
private void sendNotificationOrder(String order_number) {
|
||||
DatabaseReference tokens = FirebaseDatabase.getInstance().getReference("Tokens");
|
||||
Query data = tokens.orderByChild("serverToken").equalTo(true); //Get all node with isServerToken is true;
|
||||
data.addValueEventListener(new ValueEventListener() {
|
||||
@Override
|
||||
public void onDataChange(DataSnapshot dataSnapshot) {
|
||||
for(DataSnapshot postSnapShot: dataSnapshot.getChildren()){
|
||||
|
||||
Token serverToken = postSnapShot.getValue(Token.class);
|
||||
|
||||
//Create raw payload to send
|
||||
Notification notification = new Notification("Foodify", "Bạn có 1 đơn hàng mới " + order_number);
|
||||
Sender content = new Sender(serverToken.getToken(), notification);
|
||||
|
||||
mService.sendNotification(content)
|
||||
.enqueue(new Callback<MyResponse>() {
|
||||
@Override
|
||||
public void onResponse(Call<MyResponse> call, Response<MyResponse> response) {
|
||||
if(response.body().success == 1){
|
||||
Toast.makeText(Cart.this, "Đặt hàng thành công!", Toast.LENGTH_SHORT).show();
|
||||
finish();
|
||||
} else {
|
||||
Toast.makeText(Cart.this, "Hệ thống bị lỗi. Vui lòng thử lại sau!", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(Call<MyResponse> call, Throwable t) {
|
||||
Log.e("ERROR", t.getMessage());
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCancelled(DatabaseError databaseError) {
|
||||
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void loadListFood() {
|
||||
cart = new Database(this).getCarts();
|
||||
adapter = new CartAdapter(cart, this);
|
||||
|
||||
Reference in New Issue
Block a user