请选择 进入手机版 | 继续访问电脑版
  • 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

android - IntentService 中的数据变化

[复制链接]
菜鸟教程小白 发表于 2022-11-6 16:17:09 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

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

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

public class PushMessageHandler extends FirebaseMessagingService {

private final static String TAG = "ushMessageHandler";

@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”并且字段“NOTIFICATION_FIELD_ARTICLES”为“post”时,我期望的值是“post: 1234” : 'post-URL'"但我得到的是 "post:1234"提交的 "NOTIFICATION_FIELD_ARTICLES",该值在任何顺序中都可以互换,这取决于推送通知的消息调用。

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



Best Answer-推荐答案


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

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

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

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap