fcm主题通知

mhd8tkvw  于 2021-07-06  发布在  Java
关注(0)|答案(0)|浏览(173)

fcm(按主题)

**1. 问题是当用户向卖家发送订单时,不会出现通知和ide错误

问题和解决办法在哪里**

代码

myfirebasemessaging类

代码如下:

public class MyFirebaseMessaging extends FirebaseMessagingService {

    privat e 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

显示通知(orderid、sellerId、buyerId、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);
}
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题