Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
370 views
in Technique[技术] by (71.8m points)

Flutter firebase messaging not work on android

i try do example flutter app get notification from firebase. I have done everything suggested in the various guides. but I'm in trouble, in ios the code works perfectly, while on android it doesn't and I can't explain why. It seems that when _firebaseMessaging.getToken () is executed it leads nowhere and waits for a response that never comes. some of you have some tips? I tried all the functions made available by _firebaseMessaging but without success.

  import 'package:firebase_messaging/firebase_messaging.dart';
  import 'package:flutter/material.dart';
  import 'dart:io' show Platform;
  import 'package:flutter/foundation.dart';

  class PushMessagingExample extends StatefulWidget {
    @override
    _PushMessagingExampleState createState() => _PushMessagingExampleState();
  }

  class _PushMessagingExampleState extends State<PushMessagingExample> {
    String _homeScreenText = "Waiting for token...";
    String _messageText = "Waiting for message...";
    final FirebaseMessaging _firebaseMessaging = FirebaseMessaging();

    @override
    void initState() {
      debugPrint('test firebase');

      super.initState();
      _firebaseMessaging.configure(
        onMessage: (Map<String, dynamic> message) async {
          setState(() {
            _messageText = "Push Messaging message: $message";
          });
          print("onMessage: $message");
        },
        onLaunch: (Map<String, dynamic> message) async {
          setState(() {
            _messageText = "Push Messaging message: $message";
          });
          print("onLaunch: $message");
        },
        onResume: (Map<String, dynamic> message) async {
          setState(() {
            _messageText = "Push Messaging message: $message";
          });
          print("onResume: $message");
        },
      );



      _firebaseMessaging.requestNotificationPermissions(
          const IosNotificationSettings(sound: true, badge: true, alert: true));
      _firebaseMessaging.onIosSettingsRegistered
          .listen((IosNotificationSettings settings) {
        print("Settings registered: $settings");
      });

        _firebaseMessaging.subscribeToTopic('all');

      debugPrint('token call');
      _firebaseMessaging.getToken().then((String token) {
        debugPrint('test firebase token: $token');
        print(token);
        assert(token != null);
        setState(() {
          _homeScreenText = "Push Messaging token: $token";
        });
        print(_homeScreenText);
      });
    }

    @override
    Widget build(BuildContext context) {
      return Scaffold(
          appBar: AppBar(
            title: const Text('Push Messaging Demo'),
          ),
          body: Material(
            child: Column(
              children: <Widget>[
                Center(
                  child: Text(_homeScreenText),
                ),
                Row(children: <Widget>[
                  Expanded(
                    child: Text(_messageText),
                  ),
                ])
              ],
            ),
          ));
    }
  }

  void main() {
    runApp(
      MaterialApp(
        home: PushMessagingExample(),
      ),
    );
  }
question from:https://stackoverflow.com/questions/65858138/flutter-firebase-messaging-not-work-on-android

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I think your problem is an asynchronous method, first, divide them into Future methods and into methods to create different FirebaseMessaging instances.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...