mirror of
https://github.com/Nezumi-2711/PRM392.git
synced 2026-09-22 13:48:29 +00:00
Notification when get new order / Delete Category & Food
This commit is contained in:
@@ -5,6 +5,7 @@
|
||||
|
||||
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
|
||||
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
|
||||
|
||||
<application
|
||||
android:allowBackup="true"
|
||||
android:dataExtractionRules="@xml/data_extraction_rules"
|
||||
@@ -15,8 +16,14 @@
|
||||
android:supportsRtl="true"
|
||||
android:theme="@style/Theme.FoodifyServer"
|
||||
tools:targetApi="31">
|
||||
<uses-library android:name="org.apache.http.legacy" android:required="false"/>
|
||||
<service
|
||||
android:name=".Service.ListenOrder"
|
||||
android:enabled="true"
|
||||
android:exported="true"></service>
|
||||
|
||||
<uses-library
|
||||
android:name="org.apache.http.legacy"
|
||||
android:required="false" />
|
||||
<!--
|
||||
TODO: Before you run your application, you need a Google Maps API key.
|
||||
|
||||
|
||||
@@ -297,6 +297,7 @@ public class FoodList extends AppCompatActivity {
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
alertDialog.setView(add_menu_layout);
|
||||
alertDialog.setIcon(R.drawable.ic_baseline_shopping_cart_24);
|
||||
|
||||
|
||||
@@ -29,8 +29,12 @@ import androidx.appcompat.app.AppCompatActivity;
|
||||
import androidx.recyclerview.widget.LinearLayoutManager;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
|
||||
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.google.firebase.storage.FirebaseStorage;
|
||||
import com.google.firebase.storage.OnProgressListener;
|
||||
import com.google.firebase.storage.StorageReference;
|
||||
@@ -40,6 +44,7 @@ import com.squareup.picasso.Picasso;
|
||||
import com.waterbase.foodifyServer.Model.Category;
|
||||
import com.waterbase.foodifyServer.Common.Common;
|
||||
import com.waterbase.foodifyServer.Interface.ItemClickListener;
|
||||
import com.waterbase.foodifyServer.Service.ListenOrder;
|
||||
import com.waterbase.foodifyServer.ViewHolder.MenuViewHolder;
|
||||
|
||||
import java.util.UUID;
|
||||
@@ -113,6 +118,10 @@ public class Home extends AppCompatActivity implements NavigationView.OnNavigati
|
||||
|
||||
loadMenu();
|
||||
|
||||
//Call service
|
||||
Intent service = new Intent(Home.this, ListenOrder.class);
|
||||
startService(service);
|
||||
|
||||
}
|
||||
|
||||
private void showDialog() {
|
||||
@@ -288,6 +297,24 @@ public class Home extends AppCompatActivity implements NavigationView.OnNavigati
|
||||
}
|
||||
|
||||
private void deleteCategory(String key) {
|
||||
|
||||
//Get all food in category
|
||||
DatabaseReference foods = database.getReference("Foods");
|
||||
Query foodInCategory = foods.orderByChild("menuId").equalTo(key);
|
||||
foodInCategory.addListenerForSingleValueEvent(new ValueEventListener() {
|
||||
@Override
|
||||
public void onDataChange(DataSnapshot dataSnapshot) {
|
||||
for(DataSnapshot postSnapShot: dataSnapshot.getChildren()) {
|
||||
postSnapShot.getRef().removeValue();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCancelled(DatabaseError databaseError) {
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
categories.child(key).removeValue();
|
||||
Toast.makeText(this, "Xoá thành công!", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,105 @@
|
||||
package com.waterbase.foodifyServer.Service;
|
||||
|
||||
import android.app.Notification;
|
||||
import android.app.NotificationChannel;
|
||||
import android.app.NotificationManager;
|
||||
import android.app.PendingIntent;
|
||||
import android.app.Service;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.os.Build;
|
||||
import android.os.IBinder;
|
||||
|
||||
import androidx.core.app.NotificationCompat;
|
||||
|
||||
import com.google.firebase.database.ChildEventListener;
|
||||
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.waterbase.foodifyServer.Model.Request;
|
||||
import com.waterbase.foodifyServer.OrderStatus;
|
||||
import com.waterbase.foodifyServer.R;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
public class ListenOrder extends Service implements ChildEventListener {
|
||||
|
||||
FirebaseDatabase db;
|
||||
DatabaseReference orders;
|
||||
|
||||
@Override
|
||||
public void onCreate() {
|
||||
super.onCreate();
|
||||
db = FirebaseDatabase.getInstance();
|
||||
orders = db.getReference("Requests");
|
||||
}
|
||||
|
||||
@Override
|
||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||
orders.addChildEventListener(this);
|
||||
return super.onStartCommand(intent, flags, startId);
|
||||
}
|
||||
|
||||
public ListenOrder() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public IBinder onBind(Intent intent) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
|
||||
Request request = dataSnapshot.getValue(Request.class);
|
||||
if(request.getStatus().equals("0"))
|
||||
showNotification(dataSnapshot.getKey(), request);
|
||||
}
|
||||
|
||||
private void showNotification(String key, Request request) {
|
||||
Intent intent = new Intent(getBaseContext(), OrderStatus.class);
|
||||
PendingIntent contentIntent = PendingIntent.getActivity(getBaseContext(), 0, intent, 0);
|
||||
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
|
||||
NotificationChannel channel=
|
||||
new NotificationChannel("newOrder","newOrder",NotificationManager.IMPORTANCE_DEFAULT);
|
||||
NotificationManager notificationManager=getSystemService(NotificationManager.class);
|
||||
notificationManager.createNotificationChannel(channel);
|
||||
|
||||
}
|
||||
|
||||
NotificationCompat.Builder builder = new NotificationCompat.Builder(getBaseContext(), "newOrder");
|
||||
|
||||
builder.setAutoCancel(true)
|
||||
.setDefaults(Notification.DEFAULT_ALL)
|
||||
.setTicker("CBNV")
|
||||
.setContentInfo("Đơn hàng mới")
|
||||
.setContentText("Bạn có đơn hàng mới #" + key)
|
||||
.setSmallIcon(R.mipmap.ic_launcher)
|
||||
.setContentIntent(contentIntent);
|
||||
NotificationManager manager = (NotificationManager) getBaseContext().getSystemService(Context.NOTIFICATION_SERVICE);
|
||||
//Show many notification
|
||||
int randomInt = new Random().nextInt(9999-1)+1;
|
||||
manager.notify(randomInt, builder.build());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onChildRemoved(DataSnapshot dataSnapshot) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCancelled(DatabaseError databaseError) {
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user