2017-08-28 10 views
9

私は、FCMからのプッシュ通知からメッセージを処理するためにIntentServiceを使用しています。これは、メッセージが1つずつ出てくるときに必要に応じて完全に機能しますが、デバイスがネットワークに接続されていないときに、FCMが一度に大量のメッセージを送信した後に、このシナリオでは、 Webサービスを呼び出す際に予期しない動作が発生します。IntentServiceのデータ変更

私のプッシュ通知メッセージ・ハンドラ・クラス:

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'"ですが、 "post:1234" NOTIFICATION_FIELD_ARTICLES "、値は取得中です任意の順序で交換可能であるということは、プッシュ通知のメッセージ呼び出しに依存する。

IntentServiceのドキュメントによれば、要求をキューごとに1つずつ処理します。それではなぜこれが起こったのですか?これを完全に処理する方法はありますか?

+0

ResponseConstantsクラス内の定数キー文字列を再確認できますか? –

答えて

2

IntentService - > onHandleIntentはバックグラウンドスレッドで実行されます。時間がかかる操作がある場合は、そこで実行する必要があります。 そうでない場合は、通常のサービスを使用してください。

今、onHandleIntentでは、バックグラウンドスレッドから複数回プレゼンターを注入しています。注入をコンストラクターに移動する必要があると思います。 次に、onHandleIntentでプレゼンターメソッドを呼び出します(mPresenter.prefetchData、mPresenter.getPostDetailByIdなど)。

+0

昨日、私はonCreateメソッドの注入部分をシフトしました。これは、最終日からの交換のエラーレポートを取得していないため、これが機能していると思います。 – sasuke