mirror of
https://github.com/Nezumi-2711/PRM392.git
synced 2026-09-22 20:00:53 +00:00
Fix show notification when app killed and show date order
This commit is contained in:
@@ -3,15 +3,21 @@ package com.waterbase.foodify.Common;
|
||||
import android.content.Context;
|
||||
import android.net.ConnectivityManager;
|
||||
import android.net.NetworkInfo;
|
||||
import android.text.format.DateFormat;
|
||||
|
||||
import com.waterbase.foodify.Model.User;
|
||||
import com.waterbase.foodify.Remote.APIService;
|
||||
import com.waterbase.foodify.Remote.IGoogleService;
|
||||
import com.waterbase.foodify.Remote.RetrofitClient;
|
||||
|
||||
import java.util.Calendar;
|
||||
import java.util.Locale;
|
||||
|
||||
public class Common {
|
||||
public static User currentUser;
|
||||
|
||||
public static String PHONE_TEXT = "userPhone";
|
||||
|
||||
public static String topicName = "News";
|
||||
|
||||
private static final String BASE_URL = "https://fcm.googleapis.com/";
|
||||
@@ -55,4 +61,12 @@ public class Common {
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public static String getDate(long time){
|
||||
Calendar calendar = Calendar.getInstance(Locale.ENGLISH);
|
||||
calendar.setTimeInMillis(time);
|
||||
StringBuilder date = new StringBuilder(
|
||||
DateFormat.format("dd-MM-yyyy HH:mm", calendar).toString());
|
||||
return date.toString();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
package com.waterbase.foodify.Helper;
|
||||
|
||||
|
||||
import android.app.Notification;
|
||||
import android.app.NotificationChannel;
|
||||
import android.app.NotificationManager;
|
||||
import android.app.PendingIntent;
|
||||
import android.content.Context;
|
||||
import android.content.ContextWrapper;
|
||||
import android.net.Uri;
|
||||
import android.os.Build;
|
||||
|
||||
import androidx.annotation.RequiresApi;
|
||||
|
||||
import com.waterbase.foodify.R;
|
||||
|
||||
public class NotificationHelper extends ContextWrapper {
|
||||
|
||||
private static final String CHANNEL_ID = "Foodify";
|
||||
private static final String CHANNEL_NAME = "Notification";
|
||||
|
||||
private NotificationManager manager;
|
||||
|
||||
public NotificationHelper(Context base) {
|
||||
super(base);
|
||||
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
|
||||
createChannel();
|
||||
}
|
||||
|
||||
@RequiresApi(api = Build.VERSION_CODES.O)
|
||||
private void createChannel() {
|
||||
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT);
|
||||
channel.enableLights(false);
|
||||
channel.enableVibration(true);
|
||||
channel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
|
||||
|
||||
getManager().createNotificationChannel(channel);
|
||||
}
|
||||
|
||||
public NotificationManager getManager() {
|
||||
if(manager == null)
|
||||
manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
|
||||
return manager;
|
||||
}
|
||||
|
||||
@RequiresApi(api = Build.VERSION_CODES.O)
|
||||
public Notification.Builder getFoodifyChannelNotification(String title, String body, PendingIntent contentIntent, Uri soundUri)
|
||||
{
|
||||
return new Notification.Builder(getApplicationContext(), CHANNEL_ID)
|
||||
.setContentIntent(contentIntent)
|
||||
.setContentTitle(title)
|
||||
.setContentText(body)
|
||||
.setSmallIcon(R.mipmap.ic_launcher)
|
||||
.setSound(soundUri)
|
||||
.setAutoCancel(false);
|
||||
}
|
||||
|
||||
@RequiresApi(api = Build.VERSION_CODES.O)
|
||||
public Notification.Builder getFoodifyChannelNotification(String title, String body, Uri soundUri)
|
||||
{
|
||||
return new Notification.Builder(getApplicationContext(), CHANNEL_ID)
|
||||
.setContentTitle(title)
|
||||
.setContentText(body)
|
||||
.setSmallIcon(R.mipmap.ic_launcher)
|
||||
.setSound(soundUri)
|
||||
.setAutoCancel(false);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package com.waterbase.foodify.Model;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public class DataMessage {
|
||||
|
||||
public String to;
|
||||
public Map<String, String> data;
|
||||
|
||||
public DataMessage() {
|
||||
}
|
||||
|
||||
public DataMessage(String to, Map<String, String> data) {
|
||||
this.to = to;
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public String getTo() {
|
||||
return to;
|
||||
}
|
||||
|
||||
public void setTo(String to) {
|
||||
this.to = to;
|
||||
}
|
||||
|
||||
public Map<String, String> getData() {
|
||||
return data;
|
||||
}
|
||||
|
||||
public void setData(Map<String, String> data) {
|
||||
this.data = data;
|
||||
}
|
||||
}
|
||||
@@ -1,27 +0,0 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -28,10 +28,9 @@ import com.google.firebase.database.Query;
|
||||
import com.google.firebase.database.ValueEventListener;
|
||||
import com.waterbase.foodify.Common.Common;
|
||||
import com.waterbase.foodify.Database.Database;
|
||||
import com.waterbase.foodify.Model.DataMessage;
|
||||
import com.waterbase.foodify.Model.MyResponse;
|
||||
import com.waterbase.foodify.Model.Notification;
|
||||
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.OrderDetailAdapter;
|
||||
@@ -39,6 +38,9 @@ import com.waterbase.foodify.Zalopay.Api.CreateOrder;
|
||||
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import retrofit2.Call;
|
||||
import retrofit2.Callback;
|
||||
import retrofit2.Response;
|
||||
@@ -205,10 +207,14 @@ public class OrderDetail extends AppCompatActivity {
|
||||
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);
|
||||
// Notification notification = new Notification("Foodify", "Bạn có 1 đơn hàng mới " + order_number);
|
||||
// Sender content = new Sender(serverToken.getToken(), notification);
|
||||
Map<String, String> dataSend = new HashMap<>();
|
||||
dataSend.put("title", "Foodify");
|
||||
dataSend.put("message", "Bạn có 1 đơn hàng mới +" + order_number);
|
||||
DataMessage dataMessage = new DataMessage(serverToken.getToken(), dataSend);
|
||||
|
||||
mService.sendNotification(content)
|
||||
mService.sendNotification(dataMessage)
|
||||
.enqueue(new Callback<MyResponse>() {
|
||||
@Override
|
||||
public void onResponse(Call<MyResponse> call, Response<MyResponse> response) {
|
||||
|
||||
@@ -97,6 +97,7 @@ public class OrderStatus extends AppCompatActivity {
|
||||
orderViewHolder.txtOrderStatus.setText(Common.convertCodeToStatus(model.getStatus()));
|
||||
orderViewHolder.txtOrderAddress.setText(model.getAddress());
|
||||
orderViewHolder.txtOrderPhone.setText(model.getPhone());
|
||||
orderViewHolder.txtOrderDate.setText(Common.getDate(Long.parseLong(adapter.getRef(i).getKey())));
|
||||
}
|
||||
|
||||
@NonNull
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package com.waterbase.foodify.Remote;
|
||||
|
||||
import com.waterbase.foodify.Model.DataMessage;
|
||||
import com.waterbase.foodify.Model.MyResponse;
|
||||
import com.waterbase.foodify.Model.Sender;
|
||||
|
||||
import retrofit2.Call;
|
||||
import retrofit2.http.Body;
|
||||
@@ -20,5 +20,5 @@ public interface APIService {
|
||||
|
||||
|
||||
@POST("fcm/send")
|
||||
Call<MyResponse> sendNotification(@Body Sender body);
|
||||
Call<MyResponse> sendNotification(@Body DataMessage body);
|
||||
}
|
||||
|
||||
@@ -11,48 +11,87 @@ import android.net.Uri;
|
||||
import android.os.Build;
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.annotation.RequiresApi;
|
||||
import androidx.core.app.NotificationCompat;
|
||||
|
||||
import com.google.firebase.messaging.FirebaseMessagingService;
|
||||
import com.google.firebase.messaging.RemoteMessage;
|
||||
import com.waterbase.foodify.Common.Common;
|
||||
import com.waterbase.foodify.Helper.NotificationHelper;
|
||||
import com.waterbase.foodify.Home;
|
||||
import com.waterbase.foodify.OrderStatus;
|
||||
import com.waterbase.foodify.R;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Random;
|
||||
|
||||
public class MyFirebaseMessaging extends FirebaseMessagingService {
|
||||
|
||||
@Override
|
||||
public void onMessageReceived(RemoteMessage remoteMessage) {
|
||||
super.onMessageReceived(remoteMessage);
|
||||
showNotification(remoteMessage);
|
||||
Log.e("INFO", remoteMessage.getNotification().getBody());
|
||||
if (remoteMessage.getData() != null) {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
|
||||
showNotificationAPI26(remoteMessage);
|
||||
else
|
||||
showNotification(remoteMessage);
|
||||
}
|
||||
}
|
||||
|
||||
@RequiresApi(api = Build.VERSION_CODES.O)
|
||||
private void showNotificationAPI26(RemoteMessage remoteMessage) {
|
||||
Map<String, String> data = remoteMessage.getData();
|
||||
String title = data.get("title");
|
||||
String message = data.get("message");
|
||||
|
||||
//Fix click to notification go to Order list
|
||||
PendingIntent pendingIntent;
|
||||
NotificationHelper helper;
|
||||
Notification.Builder builder;
|
||||
|
||||
if(Common.currentUser != null)
|
||||
{
|
||||
Intent intent = new Intent(this, OrderStatus.class);
|
||||
intent.putExtra(Common.PHONE_TEXT, Common.currentUser.getPhone());
|
||||
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
|
||||
pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
|
||||
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
|
||||
|
||||
helper = new NotificationHelper(this);
|
||||
builder = helper.getFoodifyChannelNotification(title, message, pendingIntent, defaultSoundUri);
|
||||
|
||||
helper.getManager().notify(new Random().nextInt(), builder.build());
|
||||
} else {
|
||||
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
|
||||
helper = new NotificationHelper(this);
|
||||
builder = helper.getFoodifyChannelNotification(title, message, defaultSoundUri);
|
||||
|
||||
helper.getManager().notify(new Random().nextInt(), builder.build());
|
||||
}
|
||||
}
|
||||
|
||||
private void showNotification(RemoteMessage remoteMessage) {
|
||||
RemoteMessage.Notification notification = remoteMessage.getNotification();
|
||||
|
||||
Map<String, String> data = remoteMessage.getData();
|
||||
String title = data.get("title");
|
||||
String message = data.get("message");
|
||||
|
||||
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");
|
||||
NotificationCompat.Builder builder = new NotificationCompat.Builder(getBaseContext(), "foodStatus");
|
||||
builder.setAutoCancel(true)
|
||||
.setDefaults(Notification.DEFAULT_ALL)
|
||||
.setContentText(notification.getBody())
|
||||
.setContentText(message)
|
||||
.setContentIntent(pendingIntent)
|
||||
.setContentTitle(notification.getTitle())
|
||||
.setContentTitle(title)
|
||||
.setSound(defaultSoundUri)
|
||||
.setSmallIcon(R.mipmap.ic_launcher_round);
|
||||
|
||||
NotificationManager notificationManager=(NotificationManager)getBaseContext().getSystemService(Context.NOTIFICATION_SERVICE);
|
||||
notificationManager.notify(1,builder.build());
|
||||
NotificationManager notificationManager = (NotificationManager) getBaseContext().getSystemService(Context.NOTIFICATION_SERVICE);
|
||||
notificationManager.notify(1, builder.build());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@ import com.waterbase.foodify.R;
|
||||
|
||||
public class OrderViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
|
||||
|
||||
public TextView txtOrderId, txtOrderStatus, txtOrderPhone, txtOrderAddress;
|
||||
public TextView txtOrderId, txtOrderStatus, txtOrderPhone, txtOrderAddress, txtOrderDate;
|
||||
|
||||
private ItemClickListener itemClickListener;
|
||||
|
||||
@@ -22,6 +22,7 @@ public class OrderViewHolder extends RecyclerView.ViewHolder implements View.OnC
|
||||
txtOrderId = (TextView) itemView.findViewById(R.id.order_id);
|
||||
txtOrderStatus = (TextView) itemView.findViewById(R.id.order_status);
|
||||
txtOrderPhone = (TextView) itemView.findViewById(R.id.order_phone);
|
||||
txtOrderDate = (TextView) itemView.findViewById(R.id.order_date);
|
||||
|
||||
itemView.setOnClickListener(this);
|
||||
}
|
||||
|
||||
@@ -33,6 +33,16 @@
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/order_date"
|
||||
android:layout_marginLeft="10dp"
|
||||
android:gravity="center_vertical|start"
|
||||
android:textAllCaps="true"
|
||||
android:textStyle="italic"
|
||||
android:text="Order Date"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/order_status"
|
||||
android:layout_marginLeft="10dp"
|
||||
|
||||
Reference in New Issue
Block a user