mirror of
https://github.com/Nezumi-2711/PRM392.git
synced 2026-09-22 13:48:29 +00:00
Show notification when order status change
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
package="com.waterbase.foodify">
|
||||
|
||||
<uses-permission android:name="android.permission.INTERNET" />
|
||||
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
|
||||
|
||||
<application
|
||||
android:allowBackup="true"
|
||||
@@ -16,6 +17,11 @@
|
||||
android:theme="@style/Theme.Foodify"
|
||||
tools:replace="android:theme"
|
||||
tools:targetApi="31">
|
||||
<service
|
||||
android:name=".Service.ListenOrder"
|
||||
android:enabled="true"
|
||||
android:exported="true"></service>
|
||||
|
||||
<activity
|
||||
android:name=".OrderStatus"
|
||||
android:exported="false" />
|
||||
|
||||
@@ -4,4 +4,13 @@ import com.waterbase.foodify.Model.User;
|
||||
|
||||
public class Common {
|
||||
public static User currentUser;
|
||||
|
||||
public static String convertCodeToStatus(String status) {
|
||||
if(status.equals("0"))
|
||||
return "Placed";
|
||||
else if(status.equals("1"))
|
||||
return "On my way";
|
||||
else
|
||||
return "Shipped";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,6 +25,7 @@ import com.squareup.picasso.Picasso;
|
||||
import com.waterbase.foodify.Common.Common;
|
||||
import com.waterbase.foodify.Interface.ItemClickListener;
|
||||
import com.waterbase.foodify.Model.Category;
|
||||
import com.waterbase.foodify.Service.ListenOrder;
|
||||
import com.waterbase.foodify.ViewHolder.MenuViewHolder;
|
||||
|
||||
|
||||
@@ -73,6 +74,10 @@ public class Home extends AppCompatActivity implements NavigationView.OnNavigati
|
||||
View headerView = navigationView.getHeaderView(0);
|
||||
TextView navUserName = (TextView) headerView.findViewById(R.id.txtFullName);
|
||||
navUserName.setText(Common.currentUser.getName());
|
||||
|
||||
//Register Service
|
||||
Intent service = new Intent(Home.this, ListenOrder.class);
|
||||
startService(service);
|
||||
}
|
||||
|
||||
private void loadMenu() {
|
||||
|
||||
@@ -39,6 +39,11 @@ public class OrderStatus extends AppCompatActivity {
|
||||
recyclerView.setHasFixedSize(true);
|
||||
layoutManager = new LinearLayoutManager(this);
|
||||
recyclerView.setLayoutManager(layoutManager);
|
||||
|
||||
if(getIntent() == null)
|
||||
loadOrders(Common.currentUser.getPhone());
|
||||
else
|
||||
loadOrders(getIntent().getStringExtra("userPhone"));
|
||||
|
||||
loadOrders(Common.currentUser.getPhone());
|
||||
|
||||
@@ -61,7 +66,7 @@ public class OrderStatus extends AppCompatActivity {
|
||||
@Override
|
||||
protected void populateViewHolder(OrderViewHolder orderViewHolder, Request model, int i) {
|
||||
orderViewHolder.txtOrderId.setText(adapter.getRef(i).getKey());
|
||||
orderViewHolder.txtOrderStatus.setText(converCodeToStatus(model.getStatus()));
|
||||
orderViewHolder.txtOrderStatus.setText(Common.convertCodeToStatus(model.getStatus()));
|
||||
orderViewHolder.txtOrderAddress.setText(model.getAddress());
|
||||
orderViewHolder.txtOrderPhone.setText(model.getPhone());
|
||||
}
|
||||
@@ -69,15 +74,6 @@ public class OrderStatus extends AppCompatActivity {
|
||||
recyclerView.setAdapter(adapter);
|
||||
}
|
||||
|
||||
private String converCodeToStatus(String status) {
|
||||
if(status.equals("0"))
|
||||
return "Placed";
|
||||
else if(status.equals("1"))
|
||||
return "On my way";
|
||||
else
|
||||
return "Shipped";
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
|
||||
switch (item.getItemId()) {
|
||||
|
||||
@@ -0,0 +1,105 @@
|
||||
package com.waterbase.foodify.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.foodify.Common.Common;
|
||||
import com.waterbase.foodify.Model.Request;
|
||||
import com.waterbase.foodify.OrderStatus;
|
||||
import com.waterbase.foodify.R;
|
||||
|
||||
public class ListenOrder extends Service implements ChildEventListener {
|
||||
|
||||
FirebaseDatabase db;
|
||||
DatabaseReference requests;
|
||||
|
||||
public ListenOrder() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public IBinder onBind(Intent intent) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate() {
|
||||
super.onCreate();
|
||||
db = FirebaseDatabase.getInstance();
|
||||
requests = db.getReference("Requests");
|
||||
}
|
||||
|
||||
@Override
|
||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||
requests.addChildEventListener(this);
|
||||
return super.onStartCommand(intent, flags, startId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
|
||||
Request request = dataSnapshot.getValue(Request.class);
|
||||
showNotification(dataSnapshot.getKey(), request);
|
||||
}
|
||||
|
||||
private void showNotification(String key, Request request) {
|
||||
Intent intent=new Intent(getBaseContext(), OrderStatus.class);
|
||||
intent.putExtra("userPhone",request.getPhone()); //we need put user phone
|
||||
PendingIntent contentIntent=PendingIntent
|
||||
.getActivity(getBaseContext(),0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
|
||||
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
|
||||
NotificationChannel channel=
|
||||
new NotificationChannel("foodStatus","foodStatus",NotificationManager.IMPORTANCE_DEFAULT);
|
||||
NotificationManager notificationManager=getSystemService(NotificationManager.class);
|
||||
notificationManager.createNotificationChannel(channel);
|
||||
|
||||
}
|
||||
|
||||
NotificationCompat.Builder builder=new NotificationCompat.Builder(getBaseContext(),"foodStatus");
|
||||
builder.setAutoCancel(true)
|
||||
.setDefaults(Notification.DEFAULT_ALL)
|
||||
.setWhen(System.currentTimeMillis())
|
||||
.setTicker("food")
|
||||
.setContentInfo("your order was updated")
|
||||
.setContentText("Order #"+key+" was updated to "+ Common.convertCodeToStatus(request.getStatus()))
|
||||
.setContentIntent(contentIntent)
|
||||
.setContentInfo("Info")
|
||||
.setSmallIcon(R.mipmap.ic_launcher);
|
||||
|
||||
NotificationManager notificationManager=(NotificationManager)getBaseContext().getSystemService(Context.NOTIFICATION_SERVICE);
|
||||
notificationManager.notify(1,builder.build());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onChildRemoved(DataSnapshot dataSnapshot) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCancelled(DatabaseError databaseError) {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -53,6 +53,7 @@ dependencies {
|
||||
implementation 'com.google.firebase:firebase-storage:10.2.0'
|
||||
implementation 'com.github.jd-alexander:android-flat-button:v1.1'
|
||||
implementation 'com.rengwuxian.materialedittext:library:2.1.4'
|
||||
implementation 'com.jaredrummler:material-spinner:1.1.0'
|
||||
}
|
||||
|
||||
apply plugin: 'com.google.gms.google-services'
|
||||
@@ -0,0 +1,15 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<com.google.android.material.card.MaterialCardView
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_margin="8dp"
|
||||
app:cardElevation="4dp"
|
||||
>
|
||||
|
||||
<com.jaredrummler.materialspinner.MaterialSpinner
|
||||
android:id="@+id/statusSpinner"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"/>
|
||||
</com.google.android.material.card.MaterialCardView>
|
||||
Reference in New Issue
Block a user