我想在我的应用程序中有更新时发送通知。请考虑以下代码,例如:
import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
class ShoppingList extends StatefulWidget {
_ShoppingListState createState() => _ShoppingListState();
}
class _ShoppingListState extends State<ShoppingList> {
FirebaseMessaging _firebaseMessaging = new FirebaseMessaging();
void initState() {
super.initState();
_firebaseMessaging.configure(
onMessage: (Map<String, dynamic> message){
print('on message $message');
},
onResume: (Map<String, dynamic> message){
print('on resume $message');
},
onLaunch: (Map<String, dynamic> message){
print('on launch $message');
},
);
_firebaseMessaging.getToken().then((token){
print(token);
});
}
现在在build方法中,我有:
FlatButton(
child: Icon(Icons.add_shopping_cart),
onPressed: () {
Firestore.instance
.runTransaction((Transaction transaction) async {
CollectionReference reference =
Firestore.instance.collection('shopping_list');
await reference.add({"name": item});
});
Navigator.of(context).pop();
},
),
此操作后如何触发通知