'How to to show a popup dialog when I receive a notification no matter if the app is kill/In bg or in fg?
Hi I have created a flutter App and I want to show a popup dialog when I receive a notification no matter if the app is kill state or background or foreground the dialog will have interactive buttons, Is it possible to show popup like this if yes please point me towards and example or an article, Thanks
Solution 1:[1]
when app in foreground and click notification to open app:
void handleMessageOnForeground() {
FirebaseMessaging.onMessage.listen((remoteMessage) {
debugPrint('onMessage = remoteMessage ${remoteMessage.data}');
//show pop up
}
FirebaseMessaging.onMessageOpenedApp.listen((remoteMessage) {
PrefManager.saveReadNoti(false);
debugPrint('onMessageOpenedApp = remoteMessage ${remoteMessage.data}');
String payload = json.encode(remoteMessage.data);
//show pop up
}
}
Init function handleMessageOnForeground in splash_screen.dart:
class _SplashScreenState extends State<SplashScreen> {
@override
void initState() {
super.initState();
handleMessageOnForeground();
}
When app in background:
void handleMessageOnBackground() {
FirebaseMessaging.instance.getInitialMessage().then(
(remoteMessage) {
if (remoteMessage != null) {
String payload = json.encode(remoteMessage.data);
//show pop up
}
},
);
}
Init function handleMessageOnBackground in home_screen.dart:
class _HomeScreenState extends State<HomeScreen> {
@override
void initState() {
super.initState();
handleMessageOnBackground();
}
Read document of FCM to know more infomation
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Saitoh Akira |
