2017-08-28 15 views
9

Tôi đang sử dụng IntentService để xử lý tin nhắn của mình từ thông báo đẩy từ FCM. Nó hoạt động hoàn hảo theo yêu cầu khi thông báo đến từng cái một nhưng khi thiết bị không được kết nối với mạng và sau khi thiết bị kết nối lại FCM, hãy gửi số lượng lớn thư cùng một lúc và dịch vụ kịch bản này gây ra một số mơ hồ trong khi xử lý dữ liệu ý định gây ra hành vi không mong muốn khi gọi các dịch vụ web.Thay đổi dữ liệu trong IntentService

My push-notification handler nhắn Class:

public class PushMessageHandler extends FirebaseMessagingService { 

private final static String TAG = "PushMessageHandler"; 

@Override 
public void onMessageReceived(RemoteMessage remoteMessage) { 
    super.onMessageReceived(remoteMessage); 
    if (remoteMessage.getData() != null){ 
     Log.d(TAG, String.valueOf(remoteMessage.getData())); 
     Intent notificationService = new Intent(this, NotificationService.class); 
     notificationService.putExtra(ResponseConstants.NOTIFICATION_FIELD,remoteMessage.getData().get(ResponseConstants.NOTIFICATION_FIELD)); 
     notificationService.putExtra(ResponseConstants.NOTIFICATION_DATA,remoteMessage.getData().get(ResponseConstants.NOTIFICATION_DATA)); 
     notificationService.putExtra(ResponseConstants.NOTIFICATION_TYPE,remoteMessage.getData().get(ResponseConstants.NOTIFICATION_TYPE)); 
     try { 
      notificationService.putExtra(ResponseConstants.NOTIFICATION_IMAGE,remoteMessage.getData().get(ResponseConstants.NOTIFICATION_IMAGE)); 
      notificationService.putExtra(ResponseConstants.NOTIFICATION_TITLE, remoteMessage.getData().get(ResponseConstants.NOTIFICATION_TITLE)); 
     } catch (Exception e){ 
      Crashlytics.logException(e); 
     } 
     try { 
      notificationService.putExtra(ResponseConstants.DATASETS,remoteMessage.getData().get(ResponseConstants.DATASETS)); 
     } catch (Exception e){ 
      Crashlytics.logException(e); 
     } 
     startService(notificationService); 
    } else { 
     Log.d(TAG, "Notification data is null"); 
    } 
    } 
} 

Và thông báo lớp dịch vụ xử lý của tôi:

public class NotificationService extends IntentService implements NotificationContract.View { 

@Inject 
public NotificationPresenter mNotificationPresenter; 

private NotificationContract.Presenter mPresenter; 
private static final String TAG = "NotificationService"; 
private Intent mIntent; 


public NotificationService() { 
    super("NotificationService"); 
} 

@Override 
protected void onHandleIntent(@Nullable Intent intent) { 
    mIntent = intent; 
    DaggerNotificationPresenterComponent.builder() 
      .notificationViewModule(new NotificationViewModule(this)) 
      .remoteDataSourceComponent(MyApplication.getInstance().providesRemoteDataSource()) 
      .localDataSourceComponent(MyApplication.getInstance().providesLocalDataSource()) 
      .build().inject(this); 
} 

@Override 
public synchronized void setPresenter(NotificationContract.Presenter presenter) { 

this.mPresenter = presenter; 

final String notificationField = mIntent.getStringExtra(ResponseConstants.NOTIFICATION_FIELD); 
Log.d(TAG, notificationField); 

Handler handler = new Handler(getMainLooper()); 
handler.post(new Runnable() { 
    @Override 
    public void run() { 
     switch (notificationField.trim()){ 
      case Constants.NOTIFICATION_FIELD_CACHEHOMEFEEDS : 
       mPresenter.prefetchData(Integer.parseInt(
        mIntent.getStringExtra(ResponseConstants.NOTIFICATION_DATA)), 
        new JSONObject(mIntent.getStringExtra(ResponseConstants.DATASETS))); 
       break; 
      case Constants.NOTIFICATION_FIELD_UPDATEFEEDS : 
       mPresenter.getPostDetailById(Integer.parseInt(
        mIntent.getStringExtra(ResponseConstants.NOTIFICATION_DATA)), 
        new JSONObject(mIntent.getStringExtra(ResponseConstants.DATASETS))); 
       break; 
      case Constants.NOTIFICATION_FIELD_ARTICLES : 
       mPresenter.getPostDetailsPostUrl(mIntent.getStringExtra(ResponseConstants.NOTIFICATION_DATA)); 
       break; 
      case Constants.NOTIFICATION_FIELD_POSTDELETED : 
       mPresenter.deleteFeed(Integer.parseInt(
         mIntent.getStringExtra(ResponseConstants.NOTIFICATION_DATA))); 
       break; 
     } 
    } 
}); 
} 

} 

Trong trường hợp thư hàng loạt đẩy, tôi nhận được giá trị hoán đổi cho nhau của NOTIFICATION_DATA tức là giá trị mà tôi mong đợi khi trường thông báo là "NOTIFICATION_FIELD_CACHEHOMEFEEDS" là "post: 1234" và cho trường "NOTIFICATION_FIELD_ARTICLES" là "post: 'post-URL'" nhưng tôi nhận được "post: 1234" cho đã đệ trình " NOTIFICATION_FIELD_ARTICLES ", giá trị đang nhận được hoán đổi cho nhau trong bất kỳ trình tự nào phụ thuộc vào việc gọi tin nhắn của thông báo đẩy.

Theo tài liệu hướng dẫn của IntentService xử lý các yêu cầu một theo cách xếp hàng. Vậy tại sao điều này lại xảy ra. Có phương pháp nào để xử lý điều này một cách hoàn hảo hay không.

+0

Bạn có thể kiểm tra kỹ các chuỗi khóa không đổi trong lớp ResponseConstants không? –

Trả lời

2

IntentService -> onHandleIntent được thực hiện trên một chuỗi nền. Nếu bạn có hoạt động tốn thời gian, bạn nên thực hiện nó ở đó. Nếu không - chỉ cần sử dụng Dịch vụ thông thường.

Bây giờ trong onHandleIntent bạn đang tiêm trình bày nhiều lần từ chủ đề nền - tôi nghĩ rằng bạn nên di chuyển tiêm đến hàm tạo. Sau đó, trong onHandleIntent gọi phương thức trình bày của bạn (mPresenter.prefetchData, mPresenter.getPostDetailById, v.v.).

+0

Vâng, tôi đã chuyển phần tiêm trong phương thức onCreate ngày hôm qua và tôi nghĩ rằng điều này đang hoạt động vì không nhận được báo cáo lỗi về trao đổi từ ngày hôm qua. – sasuke

Các vấn đề liên quan