Firebase Cloud Messaging: Accessing Ui Elements In Onmessagereceived()
Solution 1:
You should avoid writing direct code in the onMessageReceived()
for UI changes in a specific activity. You can channel it through Local broadcast receivers.
When you tend to write code directly in the onMessageReceived()
your FirebaseMessagingService
class will get cluttered. For instance if you have 4 different kind of notification data and each data calls for different activity. Then your code will look something like this :
@OverridepublicvoidonMessageReceived(final RemoteMessage remoteMessage) {
if(remoteMessage.getData() != null
&& remoteMessage.getData().containsKey("notification_type")){
switch(remoteMessage.getData().containsKey("notification_type")){
case"type1":
Handlerhandler=newHandler(Looper.getMainLooper());
handler.post(newRunnable() {
//Access UI for some activity
.
.
.
});
break;
case"type2":
Handlerhandler=newHandler(Looper.getMainLooper());
handler.post(newRunnable() {
//Access UI for some activity
.
.
.
});
break;
case"type3":
Handlerhandler=newHandler(Looper.getMainLooper());
handler.post(newRunnable() {
//Access UI for some activity
.
.
.
});
break;
case"type4":
Handlerhandler=newHandler(Looper.getMainLooper());
handler.post(newRunnable() {
//Access UI for some activity
.
.
.
});
break;
}
}
}
Alternatively, if you use local broadcast then your code will be much more modular and organised. Your code will look something like this:
@OverridepublicvoidonMessageReceived(final RemoteMessage remoteMessage) {
if(remoteMessage.getData() != null
&& remoteMessage.getData().containsKey("notification_type")){
Intentintent=newIntent();
intent.putExtra("body",messageBody); // Put info that you want to send to the activityswitch(remoteMessage.getData().containsKey("notification_type")){
case"type1":
intent.setAction("ACTION_1") // For activity1break;
case"type2":
intent.setAction("ACTION_2") //For activity2break;
case"type3":
intent.setAction("ACTION_3") //For activity3break;
case"type4":
intent.setAction("ACTION_4") //For activity4break;
}
sendBroadcast(intent);
}
}
And your activity (eg. activity1) will look something like this: private BroadcastReceiver mReceiver;
@OverrideprotectedvoidonResume() {
// TODO Auto-generated method stubsuper.onResume();
IntentFilter intentFilter = newIntentFilter(
"ACTION_1"); // Put appropriate action string
mReceiver = newBroadcastReceiver() {
@OverridepublicvoidonReceive(Context context, Intent intent) {
//Get message from intentString msg_for_me = intent.getStringExtra("some_msg");
//Do your UI Changes stuff here.
}
};
//registering our receiverthis.registerReceiver(mReceiver, intentFilter);
}
@OverrideprotectedvoidonPause() {
super.onPause();
this.unregisterReceiver(this.mReceiver);//unregister receiver
}
According to me this will help you code your UI changes inside the activity itself and leads you towards a better code management.
Hope it is useful!
Post a Comment for "Firebase Cloud Messaging: Accessing Ui Elements In Onmessagereceived()"