2016-07-05 81 views
3

我正在開發一款應用程序,使用Firebase雲消息傳遞。我正在爲我的應用使用乾淨的架構。我想知道哪裏(在哪一層:數據,域,演示文稿)是我的類被稱爲MyFirebaseMessagingService和MyFirebaseInstanceServiceID的最佳解決方案? 這些都是我的課: myFirebaseMessagingService:清潔架構中的FCMMessagingService?

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); 


} 

這是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. 
} 

回答

1

我覺得這種類應該進入你叫什麼「表現」層。

問題是你只提到這三層,但根據Uncle Bob's diagram最後一層不僅可以包含表示部分,而且可以包含所有「框架特定」代碼。

在我看來,與Firebase進行通信完全是一個特定於框架的部分(可能是內容提供者,翻新調用等)。

備註:在你的代碼中,你直接從你的服務中使用DataBaseHelper,也許你應該通過一個UseCase來通過一個接口使用DataBaseHelper。這樣,如果您的DatabaseHelper實現發生更改,則無需修改您的服務。