'Cloud Messaging Messages not launched when app is closed
I am working with Cloud Messaging in my Flutter app.
I am using Cloud Messaging Panel to send the messages to the apps.
Now I am focused only in the Android part of the project.
For now, the app is getting and showing the Push Notifications when it is active, in foreground and in background.
The only issue is when the app is closed, in this case the app is not showing any Push Notification.
Here you have the code for the main.dart:
@override
void initState() {
getCurrentAppTheme();
super.initState();
//INICIALIZAMOS LOCAL NOTIFICATION SERVICE
LocalNotificationService.initialize(context);
//DEJAMOS PREPARADA LA APP PARA RECIBIR UNA PUSH Y ABRIRLA EN EK CASO DE QUE LA APP ESTE CERRADA
FirebaseMessaging.instance.getInitialMessage().then((message) {
if(message != null){
final routeFromMessage = message.data["route"];
Navigator.of(context).pushNamed(routeFromMessage);
}
});
///CON LA APP ACTIVA SE EJECUTA ESTA STREAM PARA OBTENER LOS DATOS DE LA PUSH
FirebaseMessaging.onMessage.listen((message) {
if(message.notification != null){
print(message.notification!.body);
print(message.notification!.title);
}
LocalNotificationService.display(message);
});
///CON LA APP ABIERTA PERO EN SEGUNDO PLANO Y EL USUARIO HACE CLICK
///SOBRE LA PUSH, LA APP SE REABRE ABRIENDO LA PANTALLA INDICADA EN ROUTES
FirebaseMessaging.onMessageOpenedApp.listen((message) {
final routeFromMessage = message.data["route"];
print("ruta recibida en la push ${routeFromMessage}");
Navigator.of(context).pushNamed(routeFromMessage);
});
}
And here LocationNotificationService:
class LocalNotificationService {
static final FlutterLocalNotificationsPlugin _notificationsPlugin =
FlutterLocalNotificationsPlugin();
static void initialize(BuildContext context) {
final InitializationSettings initializationSettings =
InitializationSettings(
android: AndroidInitializationSettings("@drawable/ic_stat_zf"));
_notificationsPlugin.initialize(initializationSettings,onSelectNotification: (String? route) async{
if(route != null){
Navigator.of(context).pushNamed(route);
}
});
}
static void display(RemoteMessage message) async {
try {
final id = DateTime.now().millisecondsSinceEpoch ~/1000;
const NotificationDetails notificationDetails = const NotificationDetails(
android: AndroidNotificationDetails(
"evanzero",
"evanzero channel",
importance: Importance.max,
priority: Priority.high,
)
);
await _notificationsPlugin.show(
id,
message.notification!.title,
message.notification!.body,
notificationDetails,
payload: message.data["route"],
);
} on Exception catch (e) {
print(e);
}
}
}
Here you have the part of AndroidManifest;
<meta-data
android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="evanzero"/>
What am I missing to get the Push Notifications shown when the app is closed?
EDIT:
It is also includedm at the top of the widget tree:
Future<void> backgroundHandler(RemoteMessage message) async{
print(message.data.toString());
print(message.notification!.title);
}
FirebaseMessaging.onBackgroundMessage(backgroundHandler);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
