【问题标题】:Flutter firebase messaging not working when app is killed当应用程序被杀死时,Flutter firebase 消息传递不起作用
【发布时间】:2020-11-17 02:56:49
【问题描述】:

这是我的 main.dart。当应用程序在前台或后台运行时,我会收到通知,但是当应用程序终止/关闭时,我没有收到任何通知。我到处找遍了。但没有一篇博客或帖子对我有帮助。

import 'dart:async';
​
import 'package:flutter/material.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'dart:io' show Platform;
​
void main() {
  runApp(MyApp());
}
​
void DialogBox(String Title, String message, context, Function action) {
  showDialog(
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(20.0),
          ),
          backgroundColor: Color(0XffC75A53),
          title: Text(
            Title,
            textAlign: TextAlign.center,
            style: TextStyle(
                color: Color(0xFF111328), fontWeight: FontWeight.w900),
          ),
          content: Text(
            message,
            textAlign: TextAlign.center,
            style: TextStyle(
              color: Colors.white,
            ),
          ),
          actions: <Widget>[
            FlatButton(
              child: Text(
                'OK',
                style: TextStyle(
                  color: Color(0xFF111328),
                ),
              ),
              onPressed: action,
            ),
          ],
        );
      });
}
​
class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}
​
class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);
​
  final String title;
​
  @override
  _MyHomePageState createState() => _MyHomePageState();
}
​
class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;
​
  final FirebaseMessaging _fcm = FirebaseMessaging();
  final FlutterLocalNotificationsPlugin _flutterLocalNotificationsPlugin =
      FlutterLocalNotificationsPlugin();
  StreamSubscription iosSubscription;
​
  @override
  void initState() {
    super.initState();
​
    var android = AndroidInitializationSettings('mipmap/ic_launcher');
    var ios = IOSInitializationSettings();
    var platform = InitializationSettings(android, ios);
    _flutterLocalNotificationsPlugin.initialize(platform);
​
    if (Platform.isIOS) {
      iosSubscription = _fcm.onIosSettingsRegistered.listen((data) {
        print(data);
      });
​
      _fcm.requestNotificationPermissions(IosNotificationSettings());
    }
​
    _fcm.configure(
      // app in fg
      onMessage: (Map<String, dynamic> message) async {
        print("onMessage: $message");
        showNotification(message);
        DialogBox("Data", message.toString(), context, () {
          Navigator.pop(context);
        });
      },
      onLaunch: (Map<String, dynamic> message) async {
        print("onLaunch: $message"); // app terminated
//        Navigator.pushReplacementNamed(context, HomeScreen.id);
      },
      onResume: (Map<String, dynamic> message) async {
        print("onResume: $message");
        DialogBox("Data", message.toString(), context, () {
          Navigator.pop(context);
        });
//        Navigator.pushReplacementNamed(context, HomeScreen.id);
//        Navigator.of(context).pushNamed(message['data']['status']);
      },
    );
  }
​
  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }
​
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.headline4,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }
​
  showNotification(Map<String, dynamic> message) async {
    var android = AndroidNotificationDetails(
        'channel_id', 'channel_name', 'channel_descriptions');
    var ios = IOSNotificationDetails();
    var platform = NotificationDetails(android, ios);
    await _flutterLocalNotificationsPlugin.show(
        0,
        message['notification']['title'].toString(),
        message['notification']['body'].toString(),
        platform);
  }
​
  @override
  void dispose() {
    if (iosSubscription != null) iosSubscription.cancel();
    super.dispose();
  }
}

【问题讨论】:

    标签: flutter firebase-cloud-messaging


    【解决方案1】:

    找到了!这是设备问题,某些设备不支持某些应用程序的后台通知。在其他设备上运行您的应用程序,它将按预期运行

    【讨论】:

      【解决方案2】:

      FlutterFire 包有一些限制,在 9.0.2 版本中我们可以在文档中看到这一点:

      在 iOS 上,如果用户从应用切换器中滑动应用程序,则必须再次手动重新打开它,后台消息才能重新开始工作。

      在 Android 上,如果用户从设备设置中强制退出应用,则必须再次手动重新打开应用才能开始工作。

      我认为第一个可能是发生在你身上的事情。

      来源:FlutterFire Docs

      【讨论】:

        猜你喜欢
        • 2022-01-22
        • 2020-08-12
        • 1970-01-01
        • 1970-01-01
        • 2020-07-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多