1

I am using Firebas mesaging service class. and Its working properly when app is running in foreground but when i clear this application from recent i am not able to access the push notification data for setting it on textview. I want to store data in database when app is running in background. I had Used Database to store the value directly but its not working.

 public void onMessageReceived(RemoteMessage message) {


    database=new SQLiteHelper(getApplicationContext());
    database.open();
    String image = message.getNotification().getIcon();
    String title = message.getNotification().getTitle();
    String text = message.getNotification().getBody();
    String sound = message.getNotification().getSound();
    int id = 0;
    database.addQuotes(text,"Xyz");

}

In MainActivity.java in Oncreate method

  Bundle extras = getIntent().getExtras();
    if (extras != null) {
        final String message = extras.getString("message");
        quote_text.setText(message);
        Toast.makeText(this, "A", Toast.LENGTH_SHORT).show();
    }

But message shows null data.

Vishal Gedia
  • 207
  • 2
  • 8

2 Answers2

1

based on this:

I want to store data in database when app is running in background

The service calls which extends FirebaseMessagingService has a method called onMessageReceived which is called when a push notification is received on the front-end (app) add your message to DB there!

@Override
  public void onMessageReceived(RemoteMessage remoteMessage) {
 ..... 
// parse and add your message to Db here! 
MadScientist
  • 2,134
  • 14
  • 27
  • 1
    When App is in Background and you are using notification message type instead of data message type as in the OP case, onMessageReceived() is never called, so he can't access the notification message in the onMessageReceived(); – ultrasamad Mar 21 '17 at 11:37
  • Exactly where i was confused, thus i started the answer with quotes saying "i want to store data in db when app is runningg in background" – MadScientist Mar 21 '17 at 14:00
1

Normally when your app is in the background, Android directs notification messages to the system tray. A user tap on the notification opens the app launcher by default.

This includes messages that contain both notification and data payload (and all messages sent from the Notifications console). In these cases, the notification is delivered to the device's system tray, and the data payload is delivered in the extras of the intent of your launcher Activity.

But you can refer this answer on stackoverflow if you want to get notification data-messages in your app while in background.

You may also refer this

Community
  • 1
  • 1
Mukesh Kumar Singh
  • 4,512
  • 2
  • 22
  • 30