我在flutter中实现了以下代码,并将其连接到firebase,它可以正常工作并根据需要显示通知,但是, 问题:每当收到Firebase Cloud Messaging发出的通知时,我想更新通知徽章(红色圆圈,带有通知号)。 有什么帮助吗?
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:mycafe/main.dart';
import 'Custom_Text.dart';
import 'Pasta.dart';
import 'Burger.dart';
import 'Pizza.dart';
import 'AboutUs.dart';
import 'dart:async';
import 'dart:math';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'ContactUs.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'dart:async';
import 'dart:io';
import 'package:flushbar/flushbar.dart';
import 'package:flushbar/flushbar_helper.dart';
// import 'package:flutter/material.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'constants.dart' as Constants;
import 'ui/auth/AuthScreen.dart';
import 'ui/onBoarding/OnBoardingScreen.dart';
import 'package:flutter/cupertino.dart';
import 'package:mycafe/ui/auth/AuthScreen.dart';
var bannerItems = ["Burger", "cheesechilly", "Noodles", "Pizza"];
var bannerImages = [
"images/burger.jpg",
"images/cheesechilly.jpg",
"images/noodles.jpg",
"images/pizza.jpg"
];
class Notify extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'FlutterBase',
home: Scaffold(
body: MessageHandler(),
),
);
}
}
class MessageHandler extends StatefulWidget {
@override
_MessageHandlerState createState() => _MessageHandlerState();
}
class _MessageHandlerState extends State<MessageHandler> {
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");
// RaisedButton(
// child: Text(message['notification']['title']),
// onPressed: () {
// Flushbar(
// flushbarPosition: FlushbarPosition.TOP,
// icon: Icon(
// Icons.notifications_active,
// color: Colors.white,
// ),
// mainButton: FlatButton(
// onPressed: () {
// Navigator.pop(context);
// // Flush.showGoodFlushbar(context, 'login successful!');
// },
// // child: Text(
// // "ADD",
// // style: TextStyle(color: Colors.amber),
// // ),
// ),
// // duration: Duration(seconds: 7))
// ).show(context);
// },
// );
final snackbar = SnackBar(
content: Text(message['notification']['title']),
action: SnackBarAction(
label: 'Go',
onPressed: () => null,
),
);
Scaffold.of(context).showSnackBar(snackbar);
// showDialog(
// context: context,
// builder: (context) => AlertDialog(
// content: ListTile(
// title: Text(message['notification']['title']),
// subtitle: Text(message['notification']['body']),
// ),
// actions: <Widget>[
// FlatButton(
// color: Colors.amber,
// child: Text('Ok'),
// onPressed: () => Navigator.of(context).pop(),
// ),
// ],
// ),
// );
},
onLaunch: (Map<String, dynamic> message) async {
print("onLaunch: $message");
// TODO optional
},
onResume: (Map<String, dynamic> message) async {
print("onResume: $message");
// TODO optional
},
);
}
@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');
}
}