我下面使用Fire Cloud Messaging在Android应用程序的flutter中有代码,当我收到通知图标更新数字时,它可以完美地工作,但是问题是当我收到通知并且应用程序未打开时图标不会更新收到的FCM数量有什么帮助? 并预先感谢
class MessageHandler extends StatefulWidget {
@override
_MessageHandlerState createState() => _MessageHandlerState();
}
class _MessageHandlerState extends State<MessageHandler> with ChangeNotifier {
final Firestore _db = Firestore.instance;
final FirebaseMessaging _fcm = FirebaseMessaging();
StreamSubscription iosSubscription;
@override
void initState() {
super.initState();
if (Platform.isIOS) {
iosSubscription = _fcm.onIosSettingsRegistered.listen((data) {
print(data);
_saveDeviceToken();
});
_fcm.requestNotificationPermissions(IosNotificationSettings());
} else {
_saveDeviceToken();
}
_fcm.configure(
onMessage: (Map<String, dynamic> message) async {
print("onMessage: $message");
notificationCounterValueNotifer.value++;
notificationCounterValueNotifer
.notifyListeners();
final snackbar = SnackBar(
content: Text(message['notification']['title']),
action: SnackBarAction(
label: 'Go',
onPressed: () => null,
),
);
Scaffold.of(context).showSnackBar(snackbar);
},
onLaunch: (Map<String, dynamic> message) async {
print("onLaunch: $message");
},
onResume: (Map<String, dynamic> message) async {
print("onResume: $message");
},
);
}
@override
void dispose() {
if (iosSubscription != null) iosSubscription.cancel();
super.dispose();
}
@override
Widget build(BuildContext context) {
// _handleMessages(context);
return MaterialApp(
home: Scaffold(body: HomeApp()
));
}
/// Get the token, save it to the database for current user
_saveDeviceToken() async {
// Get the current user
String uid = 'jeffd23';
// FirebaseUser user = await _auth.currentUser();
// Get the token for this device
String fcmToken = await _fcm.getToken();
// Save it to Firestore
if (fcmToken != null) {
var tokens = _db
.collection('users')
.document(uid)
.collection('tokens')
.document(fcmToken);
await tokens.setData({
'token': fcmToken,
'createdAt': FieldValue.serverTimestamp(), // optional
'platform': Platform.operatingSystem // optional
});
}
}
/// Subscribe the user to a topic
_subscribeToTopic() async {
// Subscribe the user to a topic
_fcm.subscribeToTopic('puppies');
}
}
class Message {
String title;
String body;
String message;
Message(title, body, message) {
this.title = title;
this.body = body;
this.message = message;
}
}
List<Message> messagesList;
Widget myAppBarIcon() {
return ValueListenableBuilder(
builder:
(BuildContext context, int newNotificationCounterValue, Widget child) {
return newNotificationCounterValue == 0
? Container(
child: IconButton(
icon: Icon(Icons.notifications),
color: Colors.white,
// size: 40,
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => Notifyy(),
),
);
}),
)
: Stack(
children: <Widget>[
IconButton(
icon: Icon(Icons.notifications),
color: Colors.white,
// size: 40,
onPressed: () {
notificationCounterValueNotifer.value--;
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => Notifyy(),
));
}),
Container(
width: 15,
height: 15,
alignment: Alignment.bottomRight,
margin: EdgeInsets.only(top: 8, left: 10),
child: Container(
width: 20,
height: 20,
decoration: BoxDecoration(
shape: BoxShape.circle,
color: Color(0xffc32c37),
border: Border.all(color: Colors.white, width: 1)),
child: Padding(
padding: const EdgeInsets.all(2.0),
child: Text(
newNotificationCounterValue.toString(),
textAlign: TextAlign.center,
style: TextStyle(fontSize: 8),
),
),
),
),
],
);
},
valueListenable: notificationCounterValueNotifer,
);
}