0

Imagine the following scenarios:

  1. My app is not running at present and a push notification is sent from Firebase
  2. My app is running on foreground and a push notification is sent
  3. My app is running but was sent to background by switching to another app, a notification is sent

In which of the above scenarios, my MainActivity is called?

The context of the scenario: I want to maintain certain application level values in android.app.Application implementation which are usually set in onCreate function of MainActivity. So if a notification is received when my app is not running, does these values empty?

Nagarjun
  • 2,346
  • 19
  • 28

2 Answers2

1
  1. When notification is sent from firebase, then it will call onMessageReceived method. More info https://firebase.google.com/docs/cloud-messaging/android/receive
  2. Same thing - onMessageReceived will be called.
  3. Same thing - onMessageReceived will be called.

You have to setup service in your application and service will be listening for notifications. Service will be running even if you kill your application.

Now when you click on your message, then it will open your MainActivity or whatever activity you set to notification intent. See example below.

There are different ways to put your application in background. If you press back button, it will kill MainActivity. If you press home button, it will not kill MainActivity.

And application onCreate method is called only when your application is started and was not in background. If application is in background, then application onCreate method will not be fired.

MainActivity onCreate method will be called when you press on notification message or you start your app from background after you closed it with back button.

For the values: I dont know where you set your values, but if you set your values in onMessageReceived method of the firebase service, then your values be initialized when MainActivity starts. If your application is not running in background (it means that your application is closed and you wiped away it from background), then your values might be empty before onMessageReceived method called.

Here is one example of my firebase service, where you can see that i am using also MainActivity for action (Intent i = new Intent(this, MainActivity.class);) when user clicks on message:

 public class FirebaseMessagingService  extends com.google.firebase.messaging.FirebaseMessagingService {
String TAG = "Notifications";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {


    Log.d(TAG, "From: " + remoteMessage.getFrom());

    // Check if message contains a data payload.
    if (remoteMessage.getData().size() > 0) {
        Log.d(TAG, "Message data payload: " + remoteMessage.getData());
    }

    // Check if message contains a notification payload.
    if (remoteMessage.getNotification() != null) {
        Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
    }

    Intent i = new Intent(this, MainActivity.class);
    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    PendingIntent pendingIntent = PendingIntent.getActivity(this,0,i,PendingIntent.FLAG_UPDATE_CURRENT);

    Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
            .setAutoCancel(true)
            .setContentTitle(remoteMessage.getNotification().getTitle())
            .setContentText(remoteMessage.getNotification().getBody())
            .setColor(0xffff7700)
            .setSmallIcon(R.drawable.ic_launcher)
            .setVibrate(new long[] { 100, 100, 100, 100})
            .setLights(0xffff7700, 300, 1000)
            .setSound(alarmSound)
            .setPriority(Notification.PRIORITY_MAX)
            .setContentIntent(pendingIntent);

    NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    manager.notify(new Random().nextInt(),builder.build());
}


}
Muno
  • 749
  • 2
  • 9
  • 19
0
  @Override
public void handleIntent(Intent intent) {
    super.handleIntent(intent);
    intent.getExtras().get("body");
      //do something, trigger notification builder here...
}

**onMessageReceived() method will be executed only app is in foreground for firebase_instance_messageservice. For background process use intent_handler (shown above) **

You can get values from each keys using intent.getExtras().get("yourkey"); and perform action while receiving push notification.

Related answer for background execution are following. These will be helpful: Background process for firebase push notification

Firebase docs

SARATH V
  • 500
  • 1
  • 7
  • 33