1

I've added FCM in my Flutter app and it works, but I can't find a way to send notifications in different languages.

I saw that you can set the conditions for the language on the Firebase console when sending a new notification, but I'm not quite sure how it works. I would like to know if I need to add something to my code to make it work.

Here's the code I have:

push_notification_service.dart

import 'package:firebase_messaging/firebase_messaging.dart';

enum AppState {
    foreground,
    background,
    terminated,
}

class PushNotificationsManager {
    PushNotificationsManager._();

    factory PushNotificationsManager() => _instance;

    static final PushNotificationsManager _instance = PushNotificationsManager._();


    Future<void> init() async {
        await _setFCMToken();
        _configure();
    }

    _setFCMToken() async {
        FirebaseMessaging messaging = FirebaseMessaging.instance;

        NotificationSettings settings = await messaging.requestPermission(
            alert: true,
            badge: true,
            sound: true,
        );

        if (settings.authorizationStatus == AuthorizationStatus.authorized) {
            String? token = await messaging.getToken();
            print('FirebaseMessaging token: $token');
        }
    }

    void _configure() async {

    await FirebaseMessaging.instance.setForegroundNotificationPresentationOptions(
        alert: true,
        badge: true,
        sound: true,
    );

    FirebaseMessaging.onMessage.listen((RemoteMessage message) {
        _showForegroundNotificationInAndroid(message);
    });

    FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
        _handleNotification(message: message.data, appState: AppState.foreground);
    });

    RemoteMessage? initialMessage = await FirebaseMessaging.instance.getInitialMessage();

    if (initialMessage != null) {
        _handleNotification(message: initialMessage.data, appState: AppState.terminated);
    }
}

    void _showForegroundNotificationInAndroid(RemoteMessage message) async {}

    void _handleNotification({
        Map<String, dynamic>? message,
        AppState? appState,
    }) async {
        print('PushNotificationsManager: _handleNotification ${message.toString()} ${appState.toString()}');
    }
}

and in main.dart I have:

initState(){
    super.initState();
    PushNotificationsManager().init();
}

Do I need to use flutter_analytics package?

l1z0b
  • 63
  • 7

0 Answers0