- When notification is sent from firebase, then it will call
onMessageReceived
method. More info https://firebase.google.com/docs/cloud-messaging/android/receive
- Same thing -
onMessageReceived
will be called.
- 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());
}
}