Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the data from Firebase Push Notification and display it in your Android Activity?

Tags:

java

android

Sorry for this noob question, I'm new to android development. I'm currently working on a project that need to send a push notification to an android device where my app is installed. I already done this by following the quick-start tutorial by firebase and got successfully received the notification on my device.

Question: How can I retrieve the message sent by the server and display that message to my Android Activity? Thanks in advance.

Heres 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());
}
like image 554
Daryll Avatar asked Oct 19 '22 08:10

Daryll


2 Answers

If you want send a mesage the your FirebaseMessagingService for your Android Activity. You can use EventBus. An android optimized event bus que simplifies communication between Activities, Fragments, Threads, Services, etc. Less code, better quality.

Add the EventBus to your build.gradle file

compile 'org.greenrobot:eventbus:3.0.0'

Register your MainActivity.java and create 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 your MyFirebaseMessagingService.java

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    EventBus.getDefault().post(remoteMessage.getNotification().getBody());
}
like image 58
Ami Aram Avatar answered Nov 01 '22 12:11

Ami Aram


If you want to send some additional message (other than the one showed on notification), you can set the message in the fields under Advanced options -> Custom data on the firebase console.

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

After you putting it, the data will be sent to the device and will be passed to the activity that you set in the notification intent, through the intent extras.

Thus, to get the data, add this code on the activity onCreate:

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

The message will be showed if it set properly.

like image 28
Kharda Avatar answered Nov 01 '22 11:11

Kharda