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:
@@ -27,11 +27,6 @@
|
||||
android:name=".VerifyPhone"
|
||||
android:exported="false" />
|
||||
|
||||
<service
|
||||
android:name=".Service.ListenOrder"
|
||||
android:enabled="true"
|
||||
android:exported="true" />
|
||||
|
||||
<activity
|
||||
android:name=".OrderStatus"
|
||||
android:exported="false" />
|
||||
@@ -53,7 +48,6 @@
|
||||
android:name=".SignUp"
|
||||
android:exported="false"
|
||||
android:theme="@style/Theme.Foodify.NoActionBar" />
|
||||
/>
|
||||
<activity
|
||||
android:name=".SignIn"
|
||||
android:exported="false"
|
||||
@@ -69,6 +63,21 @@
|
||||
<category android:name="android.intent.category.LAUNCHER" />
|
||||
</intent-filter>
|
||||
</activity>
|
||||
|
||||
<service android:name=".Service.MyFirebaseIdService">
|
||||
<intent-filter>
|
||||
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
|
||||
</intent-filter>
|
||||
</service>
|
||||
|
||||
<service
|
||||
android:name=".Service.MyFirebaseMessaging"
|
||||
android:exported="false">
|
||||
<intent-filter>
|
||||
<action android:name="com.google.firebase.MESSAGING_EVENT" />
|
||||
</intent-filter>
|
||||
</service>
|
||||
|
||||
</application>
|
||||
|
||||
</manifest>
|
||||
@@ -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);
|
||||
|
||||
@@ -5,10 +5,18 @@ import android.net.ConnectivityManager;
|
||||
import android.net.NetworkInfo;
|
||||
|
||||
import com.waterbase.foodify.Model.User;
|
||||
import com.waterbase.foodify.Remote.APIService;
|
||||
import com.waterbase.foodify.Remote.RetrofitClient;
|
||||
|
||||
public class Common {
|
||||
public static User currentUser;
|
||||
|
||||
private static final String BASE_URL = "https://fcm.googleapis.com/";
|
||||
|
||||
public static APIService getFCMService(){
|
||||
return RetrofitClient.getClient(BASE_URL).create(APIService.class);
|
||||
}
|
||||
|
||||
public static final String DELETE = "Xoá";
|
||||
public static final String USER_KEY = "User";
|
||||
public static final String PWD_KEY = "Password";
|
||||
|
||||
@@ -2,8 +2,6 @@ package com.waterbase.foodify;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.os.Debug;
|
||||
import android.util.Log;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuItem;
|
||||
import android.view.View;
|
||||
@@ -24,11 +22,12 @@ import com.firebase.ui.database.FirebaseRecyclerAdapter;
|
||||
import com.google.android.material.navigation.NavigationView;
|
||||
import com.google.firebase.database.DatabaseReference;
|
||||
import com.google.firebase.database.FirebaseDatabase;
|
||||
import com.google.firebase.iid.FirebaseInstanceId;
|
||||
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.Model.Token;
|
||||
import com.waterbase.foodify.ViewHolder.MenuViewHolder;
|
||||
|
||||
import io.paperdb.Paper;
|
||||
@@ -89,9 +88,15 @@ public class Home extends AppCompatActivity implements NavigationView.OnNavigati
|
||||
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);
|
||||
updateToken(FirebaseInstanceId.getInstance().getToken());
|
||||
|
||||
}
|
||||
|
||||
private void updateToken(String token) {
|
||||
FirebaseDatabase db = FirebaseDatabase.getInstance();
|
||||
DatabaseReference tokens = db.getReference("Tokens");
|
||||
Token data = new Token(token, false);
|
||||
tokens.child(Common.currentUser.getPhone()).setValue(data);
|
||||
}
|
||||
|
||||
private void loadMenu() {
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.waterbase.foodify.Model;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class MyResponse {
|
||||
public long multicast_id;
|
||||
public int success;
|
||||
public int failure;
|
||||
public int canonical_ids;
|
||||
public List<Result> results;
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.waterbase.foodify.Model;
|
||||
|
||||
public class Notification {
|
||||
public String body;
|
||||
public String title;
|
||||
|
||||
public Notification(String body, String title) {
|
||||
this.body = body;
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public String getBody() {
|
||||
return body;
|
||||
}
|
||||
|
||||
public void setBody(String body) {
|
||||
this.body = body;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package com.waterbase.foodify.Model;
|
||||
|
||||
public class Result {
|
||||
public String message_id;
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.waterbase.foodify.Model;
|
||||
|
||||
public class Sender {
|
||||
public String to;
|
||||
public Notification notification;
|
||||
|
||||
public Sender(String to, Notification notification) {
|
||||
this.to = to;
|
||||
this.notification = notification;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.waterbase.foodify.Model;
|
||||
|
||||
public class Token {
|
||||
|
||||
private String token;
|
||||
private boolean isServerToken;
|
||||
|
||||
public Token() {
|
||||
}
|
||||
|
||||
public Token(String token, boolean isServerToken) {
|
||||
this.token = token;
|
||||
this.isServerToken = isServerToken;
|
||||
}
|
||||
|
||||
public String getToken() {
|
||||
return token;
|
||||
}
|
||||
|
||||
public void setToken(String token) {
|
||||
this.token = token;
|
||||
}
|
||||
|
||||
public boolean isServerToken() {
|
||||
return isServerToken;
|
||||
}
|
||||
|
||||
public void setServerToken(boolean serverToken) {
|
||||
isServerToken = serverToken;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.waterbase.foodify.Remote;
|
||||
|
||||
import com.waterbase.foodify.Model.MyResponse;
|
||||
import com.waterbase.foodify.Model.Sender;
|
||||
|
||||
import retrofit2.Call;
|
||||
import retrofit2.http.Body;
|
||||
import retrofit2.http.Headers;
|
||||
import retrofit2.http.POST;
|
||||
|
||||
public interface APIService {
|
||||
|
||||
@Headers(
|
||||
|
||||
{
|
||||
"Content-Type:application/json",
|
||||
"Authorization:key=AAAAvBRs8TM:APA91bGQ-HvHMz4_UxiTsq4wY9H9j_ScuMRGOd03BYpQ4RKTuGpN_5CPhtQwycJaVCWMaS2T5S850QgUBhWZauOeM5aVeZsW3VGk1sXFu5GnlOgk2ev0VjzkK8fDsV4mO8D8k5uIknsC"
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@POST("fcm/send")
|
||||
Call<MyResponse> sendNotification(@Body Sender body);
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.waterbase.foodify.Remote;
|
||||
|
||||
import retrofit2.Retrofit;
|
||||
import retrofit2.converter.gson.GsonConverterFactory;
|
||||
|
||||
|
||||
public class RetrofitClient {
|
||||
private static Retrofit retrofit = null;
|
||||
|
||||
public static Retrofit getClient(String baseURL)
|
||||
{
|
||||
if(retrofit == null) {
|
||||
retrofit = new Retrofit.Builder()
|
||||
.baseUrl(baseURL)
|
||||
.addConverterFactory(GsonConverterFactory.create())
|
||||
.build();
|
||||
}
|
||||
return retrofit;
|
||||
}
|
||||
}
|
||||
@@ -1,105 +0,0 @@
|
||||
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("Tình trạng đơn hàng của bạn đã được cập nhật")
|
||||
.setContentText("Đơn hàng #"+key+" đã cập nhật thành "+ Common.convertCodeToStatus(request.getStatus()))
|
||||
.setContentIntent(contentIntent)
|
||||
.setContentInfo("Thông tin")
|
||||
.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) {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.waterbase.foodify.Service;
|
||||
|
||||
import com.google.firebase.database.DatabaseReference;
|
||||
import com.google.firebase.database.FirebaseDatabase;
|
||||
import com.google.firebase.iid.FirebaseInstanceId;
|
||||
import com.google.firebase.iid.FirebaseInstanceIdService;
|
||||
import com.waterbase.foodify.Common.Common;
|
||||
import com.waterbase.foodify.Model.Token;
|
||||
|
||||
public class MyFirebaseIdService extends FirebaseInstanceIdService {
|
||||
|
||||
@Override
|
||||
public void onTokenRefresh() {
|
||||
super.onTokenRefresh();
|
||||
String tokenRefreshed = FirebaseInstanceId.getInstance().getToken();
|
||||
updateTokenToFirebase(tokenRefreshed);
|
||||
}
|
||||
|
||||
private void updateTokenToFirebase(String tokenRefreshed) {
|
||||
FirebaseDatabase db = FirebaseDatabase.getInstance();
|
||||
DatabaseReference tokens = db.getReference("Tokens");
|
||||
Token token = new Token(tokenRefreshed, false);
|
||||
tokens.child(Common.currentUser.getPhone()).setValue(token);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package com.waterbase.foodify.Service;
|
||||
|
||||
import android.app.Notification;
|
||||
import android.app.NotificationChannel;
|
||||
import android.app.NotificationManager;
|
||||
import android.app.PendingIntent;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.media.RingtoneManager;
|
||||
import android.net.Uri;
|
||||
import android.os.Build;
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.core.app.NotificationCompat;
|
||||
|
||||
import com.google.firebase.messaging.FirebaseMessagingService;
|
||||
import com.google.firebase.messaging.RemoteMessage;
|
||||
import com.waterbase.foodify.Home;
|
||||
import com.waterbase.foodify.R;
|
||||
|
||||
public class MyFirebaseMessaging extends FirebaseMessagingService {
|
||||
|
||||
@Override
|
||||
public void onMessageReceived(RemoteMessage remoteMessage) {
|
||||
super.onMessageReceived(remoteMessage);
|
||||
showNotification(remoteMessage);
|
||||
Log.e("INFO", remoteMessage.getNotification().getBody());
|
||||
}
|
||||
|
||||
private void showNotification(RemoteMessage remoteMessage) {
|
||||
RemoteMessage.Notification notification = remoteMessage.getNotification();
|
||||
Intent intent = new Intent(this, Home.class);
|
||||
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
|
||||
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
|
||||
|
||||
NotificationCompat.Builder builder=new NotificationCompat.Builder(getBaseContext(),"foodStatus");
|
||||
builder.setAutoCancel(true)
|
||||
.setDefaults(Notification.DEFAULT_ALL)
|
||||
.setContentText(notification.getBody())
|
||||
.setContentIntent(pendingIntent)
|
||||
.setContentTitle(notification.getTitle())
|
||||
.setSound(defaultSoundUri)
|
||||
.setSmallIcon(R.mipmap.ic_launcher_round);
|
||||
|
||||
NotificationManager notificationManager=(NotificationManager)getBaseContext().getSystemService(Context.NOTIFICATION_SERVICE);
|
||||
notificationManager.notify(1,builder.build());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user