How to get data from Firebase Push Notification and display it in my Android activity?

Sorry for this noob question, I'm new to Android development. I am currently working on a project that needs to send a push notification to the Android device where my application is installed. I already did this by following the quick start guide using firebase and received a successful notification on my device.

Question. How to receive the message sent by the server and display this message in my Android activity? Thanks in advance.

Here is my code. MainActivity.java

public class MainActivity extends AppCompatActivity { TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView tv = (TextView) findViewById(R.id.tv); /* * Get the data from push notification and display it in TextView * */ tv.setText("Message retrieve from Push Notification"); } 

and this is my MyFirebaseMessagingService.java

 @Override public void onMessageReceived(RemoteMessage remoteMessage) { sendNotification(remoteMessage.getNotification().getBody()); } //This method is only generating push notification //It is same as we did in earlier posts public void sendNotification(String messageBody) { Intent intent = new Intent(this, MainActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT); Intent notificationIntent = new Intent(this, MainActivity.class); notificationIntent.putExtra("message", messageBody); // set intent so it does not start a new activity notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.mipmap.ic_launcher) .setContentTitle("Peoplelink Push Notification") .setContentText(messageBody) .setAutoCancel(true) .setSound(defaultSoundUri) .setContentIntent(pendingIntent); NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(0, notificationBuilder.build()); } 
+5
source share
2 answers

If you want to send mesage your FirebaseMessagingService for your Android activity. You can use EventBus . Optimized and optimized for Android, event event bus optimization simplifies the exchange of data between actions, fragments, streams, services, etc. Less code, better quality.

Add EventBus to build.gradle file

 compile 'org.greenrobot:eventbus:3.0.0' 

Register MainActivity.java and create the onMessageEvent method.

 public class MainActivity extends AppCompatActivity { TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = (TextView) findViewById(R.id.tv); EventBus.getDefault().register(this); } @Subscribe(threadMode = ThreadMode.MAIN) public void onMessageEvent(String message) { textView.setText(message); }; 

And use EventBus.getDefault().post() in MyFirebaseMessagingService.java

 @Override public void onMessageReceived(RemoteMessage remoteMessage) { EventBus.getDefault().post(remoteMessage.getNotification().getBody()); } 
+2
source

If you want to send some kind of additional message (different from that shown in the notification), you can set the message in the Additional parameters → User data fields on the firebase console.

For example, put a message as a key and a message value as a value.

After you place it, the data will be sent to the device and will be transferred to the action that you specified in the notification intent using additional settings.

So, to get the data, add this code to the onCreate action:

 Bundle extras = getIntent().getExtras(); if (extras != null) { tv.setText(extras.getString("message", "empty message")); } 

A message will be displayed if it is installed correctly.

+1
source

All Articles