作者:Missluckyyy_879 | 来源:互联网 | 2023-01-21 09:44
我正在使用IntentService来处理来自FCM的推送通知的消息.它完美的要求,当消息出现一个接一个,但是当设备没有连接到网络,并在设备重新连接FCM发送大量邮件的同时,在这种情况下服务后导致一些模棱两可的同时处理意向数据,在调用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"值"后:1234"和字段"NOTIFICATION_FIELD_ARTICLES"是"后:"后-URL'"但我收到"post:1234"提交"NOTIFICATION_FIELD_ARTICLES",该值在任何序列中都可以互换,取决于推送通知的消息调用.
根据IntentService的文档,以队列方式逐个处理请求.那么为什么会这样呢.有没有办法完美地处理这个问题.