I see you are forcibly showing a notification when the onMessage
is triggered, you don't need to do that if the app is in background, the notifications will be automatically created.
The onMessage
is triggered when you receive a notification and the app is open, running in foreground. For instance, you have the Gmail app open, and a new e-mail is received, in this case you don't need a notification poping in the notification area. The app might choose to handle it directly, and the onMessage
is triggered as soon as the notification is received - which is good so you don't need to keep pooling the server.
The onResume
and onLaunch
are a bit different - these two events are not triggered when the notification is received. They are only triggered when the user selects/taps the notification from the notification area. So, in both cases, the app is currently hidden, either by not running at all (terminated), or the app is in background - not being shown. In this case, the notification is received in the phone and automatically placed in the notification area (you don't need to code a "showNotification
" for that). At this state, the user can see the notification but the app itself it not yet aware of it.
The app will only become aware of the notification when the user selects one of these notifications.
If the app is not running at all, the onLaunch
will be triggered when the user taps the notification. It means the app was not running and as a result of the notification it had to 'launch from scratch'.
If the app is in background, the onResume
will be triggered when the user selects the notification, resuming the app to the foreground state.
EDIT:
As pointed out by @boformer this only applies for 'Notification' messages. If you're sending 'Data' messages, no notification is created and the messages are delivered only through onMessage
. More details in the plugin readme and firebase docs.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…