干净架构中的FCMMessagingService?

Ate*_*ica 5 android firebase-cloud-messaging

我正在开发使用Firebase Cloud Messaging的应用程序。我正在为我的应用程序使用干净的体系结构。我想知道哪里(在哪一层:数据,域,表示形式)是放置我的名为MyFirebaseMessagingService和MyFirebaseInstanceServiceID的类的最佳解决方案?这些是我的类:myFirebaseMessagingService:

public class myFirebaseMessagingService extends FirebaseMessagingService {

private static final String TAG="MyFirebaseMsgService";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);
    Log.d("onMessageReceived", "Pozvana funkcija onMessageReceived");
    Log.d(TAG, "From " + remoteMessage.getFrom());
    Log.d(TAG, "Body " + remoteMessage.getNotification().getBody());
    Log.d(TAG, "Location " + remoteMessage.getNotification().getClickAction());
    Log.d(TAG, "Value " + remoteMessage.getData().get("click_action"));
    sendNotification(remoteMessage);
    Log.d("Function called", "sendNotification");


}

private void sendNotification(RemoteMessage remoteMessage) {
    Intent intent = new Intent(myFirebaseMessagingService.this, MainActivity.class);
    intent.putExtra("click_action", "goToFragment1");
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);


    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notification=new NotificationCompat.Builder(this)
            .setSmallIcon(logo)
            .setContentText(remoteMessage.getNotification().getBody())
            .setContentTitle("Title")
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);
    NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(0, notification.build());
    String message=remoteMessage.getNotification().getBody();
    DataBaseHelper db=new DataBaseHelper(this);
    db.insertMsg(message);
    intent.putExtra("poruka",message );
    Log.d("Log>Poruka", message);


}
Run Code Online (Sandbox Code Playgroud)

这是myFirebaseInstanceServiceID:

public class myFirebaseInstanceServiceID extends FirebaseInstanceIdService {
private static final String TAG = "MyFirebaseIIDService";

@Override
public void onTokenRefresh() {
    super.onTokenRefresh();
    String refreshedToken = FirebaseInstanceId.getInstance().getToken();
    Log.d(TAG, "Refreshed token: " + refreshedToken);

    // TODO: Implement this method to send any registration to your app's servers.
    sendRegistrationToServer(refreshedToken);
}
private void sendRegistrationToServer(String token) {
    // Add custom implementation, as needed.
}
Run Code Online (Sandbox Code Playgroud)

Raf*_*yan 6

我认为这种类应该进入您所谓的“表示”层。

问题是你只提到了这 3 层,但根据Bob 叔叔的图表,最后一层不仅可以包含表示部分,还可以包含所有“特定于框架”的代码。

在我看来,与 Firebase 的通信完全是框架特定的部分(可能是内容提供程序、Retrofit 调用等......)。

旁注:在您的代码中,您直接从服务使用 DataBaseHelper,也许您应该传递一个将通过接口使用 DataBaseHelper 的用例。这样,如果您的 DatabaseHelper 实现发生变化,无需修改您的服务。