清洁架构中的FCMMessagingService?

时间:2016-07-05 13:21:14

标签: android firebase-cloud-messaging

我正在开发一个使用Firebase云消息传递的应用。我正在为我的应用程序使用干净的架构。我想知道在哪里(在哪一层:数据,域,表示)是我的类的最佳解决方案,我的类名为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);


}

这是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 个答案:

答案 0 :(得分:3)

我认为这类课程应该进入你所谓的“演示”层。

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

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

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