I am using Firebase Cloud Messaging on Android.
If you force stop an app you don't get any notifications until the app is restarted. None of your code will work until the app is restarted.
All services of the app will also be stopped until user manually launches the app again
Use this is onCreate method of your MainActivity
if (getIntent().getExtras() != null) {
// I Wrote here.
Intent intent = new Intent(MainActivity.this, NotificationActivity.class);
startActivity(intent);
}
The problem is in your backend. See this answer. When the message contains notification
object, onMessageReceived is called only when the app is foreground. So you have to use only data
object. Eg:
var message = {
to: user.deviceId, // required
collapse_key: 'key',
data: {
title: 'Hello',
body: 'Body'
}
};
This works for every scenario.
To send a message using API, you can use a tool called AdvancedREST Client.
{ "data": {
"image": "https://ibin.co/2t1lLdpfS06F.png",
"message": "Firebase Push Message Using API"
"AnotherActivity": "True" },"to" : "f25gYF3***********************HLI"}
Add action in your manifest activity like below:
<activity
android:name=".ActivityFCM"
android:screenOrientation="sensor"
android:windowSoftInputMode="stateHidden">
<intent-filter>
<action android:name="AnyNameYouWant" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
And add click_action in your sending notification server(my example is PHP)
$notification= array(
'title' => '快訊',
"body" => "市場大特價",
'tickerText' => 'Ticker text here',
'vibrate' => 1,
'sound' => 1,
'largeIcon' => 'large_icon',
'smallIcon' => 'small_icon',
'click_action' => 'AnyNameYouWant');