【发布时间】:2019-02-20 03:37:23
【问题描述】:
我正在尝试制作一个从 firebase 云消息中读取通知的应用 我正在使用节点程序发送通知并在颤动中阅读它
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),
),
);
}
}
正如您在 onmessage 中看到的那样,我设置了一个等于消息的数据有效负载的变量,并且我让它与 initstate 一起玩(这会重新加载小部件,以便它运行文本到语音代码,这是我唯一的工作方式找到)
我的问题是我怎样才能做到这一点,所以如果变量发生变化,即使手机关机,它也会将它与文本一起播放, 现在它告诉它应用程序何时打开,然后才
希望大家帮帮我
【问题讨论】:
标签: android firebase firebase-cloud-messaging flutter text-to-speech