Flutter FCM notfications + text to speech plugin

Flutter FCM notfications + text to speech plugin



i am trying to make an app that reads notifications from firebase cloud messaging
i am sending notifications with a node program and reading it in flutter


import 'package:flutter/material.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter_tts/flutter_tts.dart';




void main()runApp(MyApp());

class MyApp extends StatelessWidget
@override
Widget build(BuildContext context)

return new MaterialApp(
title: 'Flutter Demo',
theme: new ThemeData(
primarySwatch: Colors.blue,
),
home: new MyHomePage(title: ' Demo Home Page'),
);



class MyHomePage extends StatefulWidget

MyHomePage(Key key, this.title) : super(key: key);


final String title;

@override
_MyHomePageState createState() => new _MyHomePageState();



class _MyHomePageState extends State<MyHomePage>
FirebaseMessaging _firebaseMessaging = new FirebaseMessaging();

FlutterTts flutterTts = new FlutterTts();
int _counter = 0;
String notificationtext;

void _incrementCounter()
setState(()

_counter++;
);

@override

void initState()
super.initState();
_firebaseMessaging.subscribeToTopic("bavo");

_firebaseMessaging.configure(

onMessage : (Map<String,dynamic> message)

print('on message $message');

setState(()
_counter = _counter + 100;
notificationtext = message.toString();
);
,
onResume : (Map<String, dynamic> message)
print('on resume $message');
setState(()
_counter = _counter + 100;
notificationtext = message.toString();
);
,
onLaunch : (Map<String, dynamic> message)
print('on launch $message');
setState(()
_counter = _counter + 100;
notificationtext = message.toString();
);
,
);
_firebaseMessaging.getToken().then((token)
print(token);

);

speak() async
flutterTts.speak(notificationtext);


@override
Widget build(BuildContext context)
flutterTts.speak(notificationtext);
return new Scaffold(
appBar: new AppBar(
title: new Text(widget.title),
),
body: new Center(
child: new Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new Text(
'You have pushed the button this many times:',
),
new RaisedButton(
onPressed: speak,
child: new Text('Say Hello'),
),
new Text(
'$_counter',
style: Theme.of(context).textTheme.display1,
),
],
),
),
floatingActionButton: new FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: new Icon(Icons.add),
),
);




as you can see in onmessage i set a variable equal to the datapayload of the message and the i let it play with the initstate ( this reloads the widget so it runs the text to speech code, this was to only working way i found)



my problem is how can i make it so if the variable changes it plays it with the text to speech even if the phone is off ,
now it tells it when the app is open and only then



hope you guyss can help me




1 Answer
1



I'm not sure how firebase messaging works, but why not just run the flutterTts.speak() method inside your listener.



Or you can call the speak method within the listener.



I haven't tested this, but might be worth a shot.



example:


onMessage : (Map<String,dynamic> message)

print('on message $message');

setState(()
_counter = _counter + 100;
notificationtext = message.toString();
);
flutterTts.speak(message.toString());
,






I have already tried this and now i retried it and it still didn't work, but thnx foer the answer

– Bavo Paepens
Sep 26 '18 at 19:39



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)