[Python] How do I send an FCM notification from a backend when the Flutter app is terminated?

Discussão em 'Python' iniciado por Stack, Junho 5, 2025.

  1. Stack

    Stack Membro Participativo

    I'm working on a Flutter app that uses Firebase for notifications. Right now, the notifications work fine when the Flutter app is in the background. However, when I close the app, the notification doesn't go through. Right now, my target platform is Android.

    Here's my backend code, or at least the part that sends the notifications (it's a Flask server):

    import firebase_admin
    from firebase_admin import auth, messaging, credentials

    cred = credentials.Certificate("service-account.json")
    firebase_admin.initialize_app(cred)

    message = messaging.Message(
    notification=messaging.Notification(
    title="Wow!",
    body="This background push notification actually worked!"
    ),
    android=messaging.AndroidConfig(
    notification=messaging.AndroidNotification(
    title="Wow!",
    body="This background push notification actually worked!",
    channel_id="high_importance_channel",
    priority="high"
    ),
    priority="high"
    ),
    topic="current"
    )

    response = messaging.send(message)

    print(response)


    And here's a snippet of the Flutter app code:

    @pragma('vm:entry-point')
    Future<void> notificationHandler(RemoteMessage message) async {
    WidgetsFlutterBinding.ensureInitialized();

    await Firebase.initializeApp();

    const AndroidNotificationChannel channel = AndroidNotificationChannel(
    'high_importance_channel',
    'High Importance Notifications',
    description: 'This channel is used for important notifications.',
    importance: Importance.high
    );

    final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();

    await flutterLocalNotificationsPlugin.resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()?.createNotificationChannel(channel);

    await flutterLocalNotificationsPlugin.show(
    0,
    message.notification?.title,
    message.notification?.body,
    NotificationDetails(
    android: AndroidNotificationDetails(
    'high_importance_channel',
    'High Importance Notifications',
    importance: Importance.high
    )
    )
    );

    if (message.notification?.title == 'Awards') {
    await FirebaseMessaging.instance.unsubscribeFromTopic('current');
    }
    }

    void main() async {
    WidgetsFlutterBinding.ensureInitialized();

    await Firebase.initializeApp();

    await FirebaseAppCheck.instance.activate(
    androidProvider: AndroidProvider.playIntegrity,
    appleProvider: AppleProvider.appAttest
    );

    if (FirebaseAuth.instance.currentUser == null) {
    await FirebaseAuth.instance.signInAnonymously();
    }

    final messaging = FirebaseMessaging.instance;

    await messaging.requestPermission(
    alert: true,
    announcement: false,
    badge: true,
    carPlay: false,
    criticalAlert: false,
    provisional: false,
    sound: true
    );

    const AndroidNotificationChannel channel = AndroidNotificationChannel(
    'high_importance_channel',
    'High Importance Notifications',
    description: 'This channel is used for important notifications.',
    importance: Importance.high
    );

    final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();

    await flutterLocalNotificationsPlugin.resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()?.createNotificationChannel(channel);

    FirebaseMessaging.onBackgroundMessage(notificationHandler);


    And I know this question has been asked before, but the solutions have never really worked for me. I've tried modifying the AndroidManifest.xml, checking the battery optimization, etc. I'm also wondering if I'm sending the messages in the wrong way from the backend. And just to make it clear, when I say that "the notification doesn't go through", I mean that on the home screen of my device, when I send out the notification, there's no notification in the tray. I can't see any notifications in my notification history. And of course, there's no sound or display.

    Continue reading...

Compartilhe esta Página