1-问题是当用户向卖家发送订单时,没有出现通知并且IDE没有出现错误 问题出在哪里?
2-此处是此处的代码:
在用户类别中:
在此处输入代码:
方法调用
final String timestamp = ""+System.currentTimeMillis();
prepareNotificationMessage(timestamp);
方法是:
private void prepareNotificationMessage(String orderId){
//prepare data for notification
String NOTIFICATION_TOPIC = "/topics/"+Constants.FCM_TOPIC;//same as subscriped by User
String NOTIFICATION_TITLE = "New Order"+orderId;
String NOTIFICATION_MESSAGING = "تهانينا لديك طلب جديد";
String NOTIFICATION_TYPE = "NewOrder";
//prepare json
JSONObject notificationJo = new JSONObject();
JSONObject notificationBodyJo = new JSONObject();
try {
//what to send
notificationBodyJo.put("notificationType",NOTIFICATION_TYPE);
notificationBodyJo.put("buyerUid",firebaseAuth.getUid());
notificationBodyJo.put("sellerUid",shopUid);
notificationBodyJo.put("orderId",orderId);
notificationBodyJo.put("notificationTitle",NOTIFICATION_TITLE);
notificationBodyJo.put("notificationMessage",NOTIFICATION_MESSAGING);
//where to send
notificationJo.put("to",NOTIFICATION_TOPIC);
notificationJo.put("data",notificationBodyJo);
}
catch (Exception e){
Toast.makeText(this,""+e.getMessage(),Toast.LENGTH_SHORT).show();
}
sendFcmNotification(notificationJo,orderId);
}
这里的代码是卖方:
方法调用
prepareNotificationMessage(orderId,message);
方法:
private void prepareNotificationMessage(String orderId,String message){
//when user places order ,send notification to seller
//prepare data for notification
String NOTIFICATION_TOPIC = "/topics/"+ Constants.FCM_TOPIC;//same as subscriped by User
String NOTIFICATION_TITLE = "Your Order"+orderId;
String NOTIFICATION_MESSAGING = ""+message;
String NOTIFICATION_TYPE = "OrderStatusChanged";
//prepare json
JSONObject notificationJo = new JSONObject();
JSONObject notificationBodyJo = new JSONObject();
try {
//what to send
notificationBodyJo.put("notificationType",NOTIFICATION_TYPE);
notificationBodyJo.put("buyerUid",orderBy);
notificationBodyJo.put("sellerUid",firebaseAuth.getUid());
notificationBodyJo.put("orderId",orderId);
notificationBodyJo.put("notificationTitle",NOTIFICATION_TITLE);
notificationBodyJo.put("notificationMessage",NOTIFICATION_MESSAGING);
//where to send
notificationJo.put("to",NOTIFICATION_TOPIC);
notificationJo.put("data",notificationBodyJo);
}catch (Exception e){
Toast.makeText(this,""+e.getMessage(),Toast.LENGTH_SHORT).show();
}
sendFcmNotification(notificationJo);
}
private void sendFcmNotification(JSONObject notificationJo) {
//send Volley Request
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest("https://fcm.googleapis.com/fcm/send", notificationJo, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
//after sending fcm start order details activity,sent
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
//id failed sending fcm, still start order details activity
}
}){
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
//put required headers
Map<String,String >headers = new HashMap<>();
headers.put("Content-Type","application/json");
headers.put("Authorization","key="+Constants.FCM_KEY);
return headers;
}
};
//enque the Volley request
Volley.newRequestQueue(this).add(jsonObjectRequest);
}
这里是MyFirebaseMessaging类的代码:
public class MyFirebaseMessaging extends FirebaseMessagingService {
private static final String NOTIFICATION_CHANNEL_ID = "MY_NOTIFICATION_CHANNEL_ID";
//required for andriod 8 and bove
private FirebaseAuth firebaseAuth;
private FirebaseUser firebaseUser;
@Override
public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
//all notification will be received here
firebaseAuth = FirebaseAuth.getInstance();
firebaseUser = firebaseAuth.getCurrentUser();
//get data from notification
String notificationType = remoteMessage.getData().get("notificationType");
if (notificationType.equals("NewOrder")){
String buyerUid = remoteMessage.getData().get("buyerUid");
String sellerUid = remoteMessage.getData().get("sellerUid");
String orderId = remoteMessage.getData().get("orderId");
String notificationTitle = remoteMessage.getData().get("notificationTitle");
String notificationDescription = remoteMessage.getData().get("notificationMessage");
if (firebaseUser != null && firebaseAuth.getUid().equals(sellerUid)){
//user is signed in and same user to which notification is sent
showNotification(orderId,sellerUid,buyerUid,notificationTitle
,notificationDescription,notificationType);
}
}
if (notificationType.equals("OrderStatusChanged")){
String buyerUid = remoteMessage.getData().get("buyerUid");
String sellerUid = remoteMessage.getData().get("sellerUid");
String orderId = remoteMessage.getData().get("orderId");
String notificationTitle = remoteMessage.getData().get("notificationTitle");
String notificationDescription = remoteMessage.getData().get("notificationMessage");
if (firebaseUser != null && firebaseAuth.getUid().equals(buyerUid)){
//user is signed in and same user to which notification is sent
showNotification
(orderId,sellerUid,buyerUid,notificationTitle,notificationDescription,notificationType);
}
}
}
private void showNotification(String orderId,String sellerUid,String
buyerUid,String notificationTitle,String notificationDescription,String notificationType){
//notification
NotificationManager notificationManager = (NotificationManager)getSystemService
(Context.NOTIFICATION_SERVICE);
//id for notification ,random
int notificationID = new Random().nextInt(3000);
//check if android version is 8 and above
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
setupNotificationChannel(notificationManager);
}
//handle notification click ,start order activity
Intent intent = null;
if (notificationType.equals("NewOrder")){
//open orderDetailsSellerActivity
intent = new Intent(this, OrderDetailsSellerActivity.class);
intent.putExtra("orderId",orderId);
intent.putExtra("orderBy",buyerUid);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
}else if (notificationType.equals("OrderStatusChanged")){
intent = new Intent(this, OrderDetailsUsersActivity.class);
intent.putExtra("orderId",orderId);
intent.putExtra("orderTo",sellerUid);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
}
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_ONE_SHOT);
//large Icon
Bitmap largeIcon = BitmapFactory.decodeResource(getResources(),R.drawable.shop);
//sound of notification
Uri notificationSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new
NotificationCompat.Builder(this,NOTIFICATION_CHANNEL_ID);
notificationBuilder.setSmallIcon(R.drawable.shop)
.setLargeIcon(largeIcon)
.setContentTitle(notificationTitle)
.setContentText(notificationDescription)
.setSound(notificationSoundUri)
.setAutoCancel(true)
.setContentIntent(pendingIntent);
//show notification
notificationManager.notify(notificationID,notificationBuilder.build());
}
@RequiresApi(api = Build.VERSION_CODES.O)
private void setupNotificationChannel(NotificationManager notificationManager) {
CharSequence channelName = "Some Sample Text";
String channelDescription = "Channel Description here";
NotificationChannel notificationChannel = new
NotificationChannel(NOTIFICATION_CHANNEL_ID,channelName,NotificationManager.IMPORTANCE_HIGH);
notificationChannel.setDescription(channelDescription);
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.enableVibration(true);
if (notificationManager != null){
notificationManager.createNotificationChannel(notificationChannel);
}
}
}