如何将数据从 java 服务 class 发送到 kotlin activity class?

how to send data from java service class to kotlin activity class?

这是我的 Java 服务 class FCM

public class MyFirebaseMessagingService 扩展 FirebaseMessagingService { private static final String TAG = "FCM 数据标签" ;

@Override
public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);
    // TODO(developer): Handle FCM messages here.
   
    Log.d(TAG, "From: " + remoteMessage.getFrom());

    // Check if message contains a data payload.
    if (remoteMessage.getData().size() > 0) {
        Log.d(TAG, "Message data payload: " + remoteMessage.getData());

    }

    // Check if message contains a notification payload.
    if (remoteMessage.getNotification() != null) {
        Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
        String data=  remoteMessage.getNotification().getBody();
       String details[] = (data.split("\n"));
       String username,location,serviceType,date,time,area;
        username=details[0];
        location=details[1];
        serviceType=details[2];
        date=details[3];
        time=details[4];
        area=details[5];

        Intent i=new Intent(getApplicationContext(),FragmentHome.class);
        i.putExtra("user",username);
        i.putExtra("loc",location);
        i.putExtra("type",serviceType);
        i.putExtra("Date",date);
        i.putExtra("Time",time);
        i.putExtra("Area",area);


    }

而且我不知道如何在 kotlin 中接收数据 class。

在 kotlin 的 onCreate 方法中 activity 您需要获取这些变量,如下所示 -

val user:String = intent.getStringExtra("user")
val loc:String = intent.getStringExtra("loc")
val type:String = intent.getStringExtra("type")
val date:String = intent.getStringExtra("Date")
val time:String = intent.getStringExtra("Time")
val area:String = intent.getStringExtra("Area")