2017-12-30 46 views
0

내 Android 앱에서 firebase 클라우드 메시징을 사용하여 일부 데이터를 앱에 보냅니다. Firebase 클라우드 메시징 : onMessageReceived()의 UI 요소 액세스

onMessageReceived() 통해 메시지를 수신

, 난 ( MainActivityMainActivity 현재 표시로 고정 기준을 갖는다)을 MainActivity 현재 볼 수 있는지 여부를 확인하고, 작업이 표시되는 경우, I는 활동에 약간의 수정을 만든다.

주 스레드에서 onMessageReceived()이 호출되지 않았으므로 Handler을 사용하여 주 스레드의 코드를 실행합니다.

@Override 
public void onMessageReceived(final RemoteMessage remoteMessage) { 
    Handler handler = new Handler(Looper.getMainLooper()); 
    handler.post(new Runnable() { 
     //Access UI 
    }); 
} 

이 방법에 문제가 있습니까? 그렇지 않은 경우 클라우드 메시지를 수신 할 때 현재 보이는 UI를 업데이트하는 다른 방법이 있습니까?

+2

받은 메시지로 로컬 브로드 캐스트를 활동에 보내고 여기에 약간의 변경을 할 수 있습니다. –

답변

0

특정 활동의 UI 변경에 대해서는 onMessageReceived()에 직접 코드를 작성하지 마십시오. 로컬 브로드 캐스트 리시버를 통해 채널을 연결할 수 있습니다.

코드 onMessageReceived()에 코드를 직접 작성하는 경우 FirebaseMessagingService 클래스가 복잡해질 수 있습니다. 예를 들어, 네 가지 종류의 알림 데이터가 있고 각 데이터가 다른 활동을 호출하는 경우 그런 다음 코드는 다음과 같이 표시됩니다.

@Override 
public void onMessageReceived(final RemoteMessage remoteMessage) { 
    if(remoteMessage.getData() != null 
     && remoteMessage.getData().containsKey("notification_type")){ 
     switch(remoteMessage.getData().containsKey("notification_type")){ 
      case "type1": 
       Handler handler = new Handler(Looper.getMainLooper()); 
       handler.post(new Runnable() { 
        //Access UI for some activity 
        . 
        . 
        . 
       }); 
       break; 
      case "type2": 
       Handler handler = new Handler(Looper.getMainLooper()); 
       handler.post(new Runnable() { 
        //Access UI for some activity 
        . 
        . 
        . 
       }); 
       break; 
      case "type3": 
       Handler handler = new Handler(Looper.getMainLooper()); 
       handler.post(new Runnable() { 
        //Access UI for some activity 
        . 
        . 
        . 

       }); 
       break; 
      case "type4": 
       Handler handler = new Handler(Looper.getMainLooper()); 
       handler.post(new Runnable() { 
        //Access UI for some activity 
        . 
        . 
        . 

       }); 
       break; 

     } 
    } 

} 

로컬 브로드 캐스트를 사용하면 코드가 모듈화되고 체계적으로 구성됩니다.

@Override 
public void onMessageReceived(final RemoteMessage remoteMessage) { 
    if(remoteMessage.getData() != null 
     && remoteMessage.getData().containsKey("notification_type")){ 
     Intent intent = new Intent(); 
     intent.putExtra("body",messageBody); // Put info that you want to send to the activity 
     switch(remoteMessage.getData().containsKey("notification_type")){ 
      case "type1": 
       intent.setAction("ACTION_1") // For activity1 
       break; 
      case "type2": 
       intent.setAction("ACTION_2") //For activity2 
       break; 
      case "type3": 
       intent.setAction("ACTION_3") //For activity3 
       break; 
      case "type4": 
       intent.setAction("ACTION_4") //For activity4 
       break; 
     } 
     sendBroadcast(intent); 
    } 

} 

그리고 활동 (. 예를 들어 activity1에) 같이 보일 것입니다 : 코드는 다음과 같이 보일 것이다 개인 브로드 캐스트 리시버 mReceiver을;

@Override 
protected void onResume() { 
    // TODO Auto-generated method stub 
    super.onResume(); 

    IntentFilter intentFilter = new IntentFilter(
      "ACTION_1"); // Put appropriate action string 

    mReceiver = new BroadcastReceiver() { 

     @Override 
     public void onReceive(Context context, Intent intent) { 
      //Get message from intent 
      String msg_for_me = intent.getStringExtra("some_msg"); 
      //Do your UI Changes stuff here. 

     } 
    }; 
    //registering our receiver 
    this.registerReceiver(mReceiver, intentFilter); 
} 

@Override 
protected void onPause() { 
    super.onPause(); 
    this.unregisterReceiver(this.mReceiver);//unregister receiver 
} 

제게 따르면 활동 내에서 UI 변경 사항을 코딩하고 더 나은 코드 관리로 이끌 수 있습니다.

희망이 있습니다. 유용합니다.