使用FCM更新Firestore DB时在flutter应用程序上获取通知

时间:2018-12-12 10:28:11

标签: firebase flutter google-cloud-firestore firebase-cloud-messaging

我想在我的应用程序中有更新时发送通知。请考虑以下代码,例如:

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();
                        },
                      ),

此操作后如何触发通知

0 个答案:

没有答案