首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >程序包更新后,Firebase消息包无法工作吗?

程序包更新后,Firebase消息包无法工作吗?
EN

Stack Overflow用户
提问于 2021-04-02 13:22:03
回答 1查看 834关注 0票数 0
代码语言:javascript
复制
    _firebaseMessaging.*configure*( onMessage: (Map<String, dynamic> message) async {
    print("\n\n on1Message: ${message.toString()}");
    Map<String, dynamic> object = json.decode(
        message['data']['notification'].toString());

    print(
        '\n\n Object==${message['data']}\n\n object===$object');
    object['work'] = 'updateCount';
    Stream<Map<String, dynamic>> stream =
        Stream.value(object);

    streamController.addStream(stream);

    print("\n\n object ---> ${object}");

控制台722:24中的错误:错误:未为类“FirebaseMessaging”定义“配置”方法。

  • 'FirebaseMessaging‘来自'package:firebase_messaging/firebase_messaging.dart’('/E:/flutterSDK/flutter/.pub-cache/hosted/pub.dartlang.org/firebase_messaging-9.1.0/lib/firebase_messaging.dart').尝试将名称更正为现有方法的名称,或定义名为“配置”的方法。_firebaseMessaging.configure

(^

Firebase消息传递包更新后,配置()方法无法工作。我尝试了与堆栈溢出不同的解决方案,但没有任何效果。在我的案子里我该怎么办。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-04-03 22:57:51

新的FirebaseMessaging有点不同。这里有两个有趣的链接:https://firebase.google.com/docs/flutter/setup?platform=android https://firebase.flutter.dev/docs/messaging/usage/

在向应用程序添加了firebase之后,这就是我要做的事情(NotificationDetails是我编写的一个类,用于显示通知的细节。你可以写你自己的课):

代码语言:javascript
复制
void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();

  runApp(
    MaterialApp(
      debugShowCheckedModeBanner: false,
      routes: {
      '/': (context) =>  AppStarter(),
      '/message': (context) => NotificationDetails(),
      },
     ),
    );
   }



class AppStarter extends StatefulWidget{
   @override
   _AppStarterState createState() => _AppStarterState();
  }



class _AppStarterState extends State<AppStarter>
   {

     FirebaseMessaging messaging = FirebaseMessaging.instance;

    Future<void> showMeMyToken()
    async {
      var myToken = await messaging.getToken();
      print("My Token is: " + myToken.toString());
    }


    @override
    void initState() {
       super.initState();

       showMeMyToken();

      FirebaseMessaging.instance.getInitialMessage().then((value) {
       if(value != null)
        {
          Navigator.push(context,
          MaterialPageRoute(
              builder: (context){return NotificationDetails();},
          settings: RouteSettings(arguments: value.data,),
         ),
        );
       }
     });


     FirebaseMessaging.onMessage.listen((RemoteMessage message) {


        if (message.notification != null) {
           print('Message on Foreground: ${message.notification}');
              }
         });


      FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message)
       {
         Navigator.push(
           context,
           MaterialPageRoute(
               builder: (context) {return NotificationDetails();},
               settings: RouteSettings(arguments: message.data,)
          ),
        );
     });

     FirebaseMessaging.onBackgroundMessage(firebaseMessagingBackgroundHandler);
   }


   @override
    Widget build(BuildContext context) {

      return MaterialApp(
        debugShowCheckedModeBanner: false,
        title: 'Just a Test',
        
        home: AppHome(),
       );
      }
   }




   Future<void> firebaseMessagingBackgroundHandler(RemoteMessage message) async {
     await Firebase.initializeApp();

      print("Handling a background message :-): ${message.data}");
      //Here you can do what you want with the message :-)
     }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66919969

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档