flutter_tts icon indicating copy to clipboard operation
flutter_tts copied to clipboard

Run text to speech service, when we receive certain key from notifications

Open mishraaditya595 opened this issue 1 year ago • 3 comments

💬 Questions and Help

I'm currently working on implementing a feature where the app should read the notification text when specific keys are present in the notification data. This functionality needs to work seamlessly in the foreground, background, and even when the app is closed. However, despite my efforts, I haven't been able to get it to work reliably.

Below is a snippet of the code I've been working on:

FirebaseMessaging.onMessage.listen((RemoteMessage message) async {
    await showNotification(message);
});

Future<void> showNotification(RemoteMessage message) async {
    if (message.data.containsKey("moe_channel_id") ||
        message.data.containsKey("moe_inapp")) {
        if (message.data.containsKey("voice") && message.data['voice'] == "true") {
            playSound(text: message.data['gcm_title'] ?? "");
        }
    } 
    // Additional code...
}

void playSound({required String text}) {
    try {
      FlutterTts flutterTts = FlutterTts();
      flutterTts.speak(text);
    } catch(e) {
      // Error Handling
    }
 }

I would greatly appreciate any insights or assistance from fellow developers on how to ensure this feature works reliably across different app states. Thank you in advance for your help!

mishraaditya595 avatar Mar 19 '24 06:03 mishraaditya595

@mishraaditya595 Is something not working related specifically to this plugin here?

dlutton avatar Mar 22 '24 07:03 dlutton

Yes, as mentioned, I am trying to trigger the speak() method when I receive a certain notification. But it is not working then.

mishraaditya595 avatar Mar 26 '24 09:03 mishraaditya595