mirror of
https://github.com/Nezumi-2711/PRM392.git
synced 2026-09-22 13:48:29 +00:00
Fix show notification when app killed and show date order
This commit is contained in:
@@ -4,6 +4,7 @@ import android.graphics.Bitmap;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.Matrix;
|
||||
import android.graphics.Paint;
|
||||
import android.text.format.DateFormat;
|
||||
|
||||
import com.waterbase.foodifyServer.Model.Request;
|
||||
import com.waterbase.foodifyServer.Model.User;
|
||||
@@ -12,10 +13,15 @@ import com.waterbase.foodifyServer.Remote.FCMRetrofitClient;
|
||||
import com.waterbase.foodifyServer.Remote.IGeoCoordinates;
|
||||
import com.waterbase.foodifyServer.Remote.RetrofitClient;
|
||||
|
||||
import java.util.Calendar;
|
||||
import java.util.Locale;
|
||||
|
||||
public class Common {
|
||||
public static User currentUser;
|
||||
public static Request currentRequest;
|
||||
|
||||
public static String PHONE_TEXT = "userPhone";
|
||||
|
||||
public static String topicName = "News";
|
||||
|
||||
public static final String UPDATE = "Cập nhật";
|
||||
@@ -59,4 +65,12 @@ public class Common {
|
||||
|
||||
return scaledBitmap;
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
+67
@@ -0,0 +1,67 @@
|
||||
package com.waterbase.foodifyServer.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.foodifyServer.R;
|
||||
|
||||
public class NotificationHelper extends ContextWrapper {
|
||||
|
||||
private static final String CHANNEL_ID = "FoodifyServer";
|
||||
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.foodifyServer.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.foodifyServer.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,30 +0,0 @@
|
||||
package com.waterbase.foodifyServer.Model;
|
||||
|
||||
public class Sender {
|
||||
public String to;
|
||||
public Notification notification;
|
||||
|
||||
public Sender() {
|
||||
}
|
||||
|
||||
public Sender(String to, Notification notification) {
|
||||
this.to = to;
|
||||
this.notification = notification;
|
||||
}
|
||||
|
||||
public String getTo() {
|
||||
return to;
|
||||
}
|
||||
|
||||
public void setTo(String to) {
|
||||
this.to = to;
|
||||
}
|
||||
|
||||
public Notification getNotification() {
|
||||
return notification;
|
||||
}
|
||||
|
||||
public void setNotification(Notification notification) {
|
||||
this.notification = notification;
|
||||
}
|
||||
}
|
||||
@@ -11,7 +11,6 @@ import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.util.Log;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.MenuItem;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.Toast;
|
||||
@@ -25,15 +24,16 @@ import com.google.firebase.database.FirebaseDatabase;
|
||||
import com.google.firebase.database.ValueEventListener;
|
||||
import com.jaredrummler.materialspinner.MaterialSpinner;
|
||||
import com.waterbase.foodifyServer.Common.Common;
|
||||
import com.waterbase.foodifyServer.Interface.ItemClickListener;
|
||||
import com.waterbase.foodifyServer.Model.DataMessage;
|
||||
import com.waterbase.foodifyServer.Model.MyResponse;
|
||||
import com.waterbase.foodifyServer.Model.Notification;
|
||||
import com.waterbase.foodifyServer.Model.Request;
|
||||
import com.waterbase.foodifyServer.Model.Sender;
|
||||
import com.waterbase.foodifyServer.Model.Token;
|
||||
import com.waterbase.foodifyServer.Remote.APIService;
|
||||
import com.waterbase.foodifyServer.ViewHolder.OrderViewHolder;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import retrofit2.Call;
|
||||
import retrofit2.Callback;
|
||||
import retrofit2.Response;
|
||||
@@ -83,10 +83,15 @@ public class OrderStatus extends AppCompatActivity {
|
||||
@Override
|
||||
protected void onBindViewHolder(@NonNull OrderViewHolder viewHolder, int i, @NonNull Request model) {
|
||||
viewHolder.txtOrderId.setText(adapter.getRef(i).getKey());
|
||||
|
||||
viewHolder.txtOrderDate.setText(Common.getDate(Long.parseLong(adapter.getRef(i).getKey())));
|
||||
|
||||
viewHolder.txtOrderStatus.setText(Common.coverCodeToStatus(model.getStatus()));
|
||||
viewHolder.txtOrderAddress.setText(model.getAddress());
|
||||
viewHolder.txtOrderPhone.setText(model.getPhone());
|
||||
|
||||
|
||||
|
||||
//Event Button
|
||||
viewHolder.btnEdit.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
@@ -216,10 +221,14 @@ public class OrderStatus extends AppCompatActivity {
|
||||
Token token = postSnapShot.getValue(Token.class);
|
||||
|
||||
//Make raw payload
|
||||
Notification notification = new Notification("Foodify", "Đơn của bạn " + localKey + " đã được cập nhật!");
|
||||
Sender content = new Sender(token.getToken(), notification);
|
||||
// Notification notification = new Notification("Foodify", "Đơn của bạn " + localKey + " đã được cập nhật!");
|
||||
// Sender content = new Sender(token.getToken(), notification);
|
||||
Map<String, String> dataSend = new HashMap<>();
|
||||
dataSend.put("title", "EDMT Dev");
|
||||
dataSend.put("message", "Đơn của bạn " + localKey + " đã được cập nhật!");
|
||||
DataMessage dataMessage = new DataMessage(token.getToken(), dataSend);
|
||||
|
||||
mService.sendNotification(content)
|
||||
mService.sendNotification(dataMessage)
|
||||
.enqueue(new Callback<MyResponse>() {
|
||||
@Override
|
||||
public void onResponse(Call<MyResponse> call, Response<MyResponse> response) {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package com.waterbase.foodifyServer.Remote;
|
||||
|
||||
import com.waterbase.foodifyServer.Model.DataMessage;
|
||||
import com.waterbase.foodifyServer.Model.MyResponse;
|
||||
import com.waterbase.foodifyServer.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);
|
||||
}
|
||||
|
||||
@@ -8,11 +8,13 @@ import android.widget.Toast;
|
||||
|
||||
import com.rengwuxian.materialedittext.MaterialEditText;
|
||||
import com.waterbase.foodifyServer.Common.Common;
|
||||
import com.waterbase.foodifyServer.Model.DataMessage;
|
||||
import com.waterbase.foodifyServer.Model.MyResponse;
|
||||
import com.waterbase.foodifyServer.Model.Notification;
|
||||
import com.waterbase.foodifyServer.Model.Sender;
|
||||
import com.waterbase.foodifyServer.Remote.APIService;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import info.hoang8f.widget.FButton;
|
||||
import retrofit2.Call;
|
||||
import retrofit2.Callback;
|
||||
@@ -40,13 +42,18 @@ public class SendMessage extends AppCompatActivity {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
//Create message
|
||||
Notification notification = new Notification(edtTitle.getText().toString(), edtMessage.getText().toString());
|
||||
// Notification notification = new Notification(edtTitle.getText().toString(), edtMessage.getText().toString());
|
||||
//
|
||||
// Sender toTopic = new Sender();
|
||||
// toTopic.to = new StringBuilder("/topics/").append(Common.topicName).toString();
|
||||
// toTopic.notification = notification;
|
||||
|
||||
Sender toTopic = new Sender();
|
||||
toTopic.to = new StringBuilder("/topics/").append(Common.topicName).toString();
|
||||
toTopic.notification = notification;
|
||||
|
||||
mService.sendNotification(toTopic)
|
||||
Map<String,String> dataSend = new HashMap<>();
|
||||
dataSend.put("title",edtTitle.getText().toString());
|
||||
dataSend.put("message",edtMessage.getText().toString());
|
||||
DataMessage dataMessage = new DataMessage(new StringBuilder("/topics/").append(Common.topicName).toString(),dataSend);
|
||||
|
||||
mService.sendNotification(dataMessage)
|
||||
.enqueue(new Callback<MyResponse>() {
|
||||
@Override
|
||||
public void onResponse(Call<MyResponse> call, Response<MyResponse> response) {
|
||||
|
||||
+55
-15
@@ -11,46 +11,86 @@ 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.foodifyServer.Common.Common;
|
||||
import com.waterbase.foodifyServer.Helper.NotificationHelper;
|
||||
import com.waterbase.foodifyServer.Home;
|
||||
import com.waterbase.foodifyServer.OrderStatus;
|
||||
import com.waterbase.foodifyServer.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());
|
||||
}
|
||||
}
|
||||
|
||||
+2
-1
@@ -13,7 +13,7 @@ import com.waterbase.foodifyServer.R;
|
||||
|
||||
public class OrderViewHolder extends RecyclerView.ViewHolder{
|
||||
|
||||
public TextView txtOrderId, txtOrderStatus, txtOrderPhone, txtOrderAddress;
|
||||
public TextView txtOrderId, txtOrderStatus, txtOrderPhone, txtOrderAddress, txtOrderDate;
|
||||
|
||||
public Button btnEdit, btnRemove, btnDetail, btnDirection;
|
||||
|
||||
@@ -24,6 +24,7 @@ public class OrderViewHolder extends RecyclerView.ViewHolder{
|
||||
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);
|
||||
|
||||
btnEdit = (Button) itemView.findViewById(R.id.btnEdit);
|
||||
btnDetail = (Button)itemView.findViewById(R.id.btnDetail);
|
||||
|
||||
@@ -37,6 +37,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