android - IntentService 中的数据变化

标签 android intentservice

我正在使用 IntentService 处理来自 FCM 推送通知的消息。当消息一个接一个地出现但设备未连接到网络时以及设备再次连接后 FCM 一次发送大量消息时,它可以按要求完美地工作,在这种情况下,服务在处理 Intent 数据时会导致一些歧义在调用 Web 服务时导致意外行为。

我的推送通知消息处理程序类:

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

还有我的通知处理程序服务类:

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

}

在批量推送消息的情况下,我得到了 NOTIFICATION_DATA 的可互换值,即当通知字段为“NOTIFICATION_FIELD_CACHEHOMEFEEDS”时我期望的值是“post:1234”,对于字段“NOTIFICATION_FIELD_ARTICLES”是“post” : 'post-URL'”,但我得到的是提交的“NOTIFICATION_FIELD_ARTICLES”的“post:1234”,该值在任何序列中都可以互换,具体取决于推送通知的消息调用。

根据IntentService的文档,以队列的方式一个一个的处理请求。那为什么会这样。有什么方法可以完美地处理这个问题。

最佳答案

IntentService -> onHandleIntent 在后台线程上执行。如果你有耗时的操作,你应该在那里执行它。 如果不是 - 只需使用普通服务即可。

现在在 onHandleIntent 中,您从后台线程多次注入(inject)演示者 - 我认为您应该将注入(inject)移至构造函数。 然后在 onHandleIntent 中调用您的演示者方法(mPresenter.prefetchData、mPresenter.getPostDetailById 等)。

关于android - IntentService 中的数据变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45920233/

相关文章:

android - 将 Gradle 更新到 7.0.2 后,元素类型 “manifest” 必须后跟属性规范、 “>” 或 “/>” 错误

android - 从 intentservcie 回调到 jobservice

android - 当服务在后台运行时,异步任务无法正常工作(doInBackground 未执行),Android

Java/Android 绝对最快的方式来定期获取当前秒数 (0-59) 和/或毫秒数 (0-999)

Android:多个 IntentServices - stopService

android - 如何访问 IntentService 的队列?

android 有多少 Intent 服务可以并行运行

Android PhoneGap OCR 插件

android - 在 textview 中将文本显示为对角线 45

android - Google Maps API Android 在 MapFragment 中显示地址 map