I finished my first android app written by Kotlin language recently. Today I want to record what I learned when I setup Firebase for remote notification.
There are two cases to disucuss.
1. When the app is in foreground.
This case is easier, when the push is coming, the callback method onMessageReceived
in the class which extends FirebaseMessagingService
will be invoked.
2. When the app is in the background or is killed.
Under this case, the notification will show in the system tray first. when we click the notification, the app launcher will be open by default. We can get the push information from its intent
.
But if you don’t want the default action, for example, in my app, the launcher is a Splash activity, I don’t want to deal with push information in the activity, how we can do?
Now, let us see how to send push to devices.
1 | Method - POST |
The key-value click_action
is the key point. I want MainActivity to initialize when the push is coming, so I config MainActivity in AndroidManifest.xml
file
1 | <activity android:name=".activities.MainActivity" android:configChanges="keyboardHidden|orientation" |
Note the action’s name in intent-filter
is same with the value of the key click_action
.
Further more, if the MainActivity is in the background, I don’t want to initilize a new one, so I set android:launchMode="singleInstance"
. This way, when the push is coming out, we can get its intent
from the following callback method:
1 | override fun onNewIntent(intent: Intent?) { |
Done!