특정 활동의 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 변경 사항을 코딩하고 더 나은 코드 관리로 이끌 수 있습니다.
희망이 있습니다. 유용합니다.
받은 메시지로 로컬 브로드 캐스트를 활동에 보내고 여기에 약간의 변경을 할 수 있습니다. –