繁体   English   中英

flutter 通知不会出现在 ios 当应用程序正在杀死 state 但当我们打开应用程序时通知即将到来

[英]flutter notification is not coming in ios when app is on kill state but when we open the app notification is coming

Hello i have an issue in flutter IOS notification, when app is on background or kill state notification is showing only when we click to open the app otherWise notification is not showing in IOS device !

请试试这个

class 名称 FCM

import 'dart:async';
    
    import 'package:firebase_core/firebase_core.dart';
    import 'package:firebase_messaging/firebase_messaging.dart';
    
    Future<void> onBackgroundMessage(RemoteMessage message) async {
      await Firebase.initializeApp();
    
      if (message.data.containsKey('data')) {
        // Handle data message
        final data = message.data['data'];
      }
    
      if (message.data.containsKey('notification')) {
        // Handle notification message
        final notification = message.data['notification'];
      }
      // Or do other work.
    }
    
    class FCM {
      final _firebaseMessaging = FirebaseMessaging.instance;
    
      final streamCtlr = StreamController<String>.broadcast();
      final titleCtlr = StreamController<String>.broadcast();
      final bodyCtlr = StreamController<String>.broadcast();
    
      setNotifications() {
        FirebaseMessaging.onBackgroundMessage(onBackgroundMessage);
        FirebaseMessaging.onMessage.listen(
              (message) async {
            if (message.data.containsKey('data')) {
              // Handle data message
              streamCtlr.sink.add(message.data['data']);
            }
            if (message.data.containsKey('notification')) {
              // Handle notification message
              streamCtlr.sink.add(message.data['notification']);
            }
            // Or do other work.
            titleCtlr.sink.add(message.notification!.title!);
            bodyCtlr.sink.add(message.notification!.body!);
          },
        );
        // With this token you can test it easily on your phone
        final token =
        _firebaseMessaging.getToken().then((value) => print('Token: $value'));
      }
    
      dispose() {
        streamCtlr.close();
        bodyCtlr.close();
        titleCtlr.close();
      }
    }

和主Class

    void main() async {
      await init();
      runApp(const MyApp1());
    }
    
    Future init() async {
      WidgetsFlutterBinding.ensureInitialized();
      await Firebase.initializeApp();
    }
    
    class MyApp extends StatelessWidget {
      const MyApp({Key? key}) : super(key: key);
    
      @override
      Widget build(BuildContext context) {
        return const MaterialApp(
          home: HomePage(),
        );
      }
    }
    
    class HomePage extends StatefulWidget {
      const HomePage({Key? key}) : super(key: key);
    
      @override
      State<HomePage> createState() => _HomePageState();
    }
    
    class _HomePageState extends State<HomePage> {
      String notificationTitle = 'No Title';
      String notificationBody = 'No Body';
      String notificationData = 'No Data';
    
      @override
      void initState() {
        final firebaseMessaging = FCM();
        firebaseMessaging.setNotifications();
    
        firebaseMessaging.streamCtlr.stream.listen(_changeData);
        firebaseMessaging.bodyCtlr.stream.listen(_changeBody);
        firebaseMessaging.titleCtlr.stream.listen(_changeTitle);
    
        super.initState();
      }
    
      _changeData(String msg) => setState(() => notificationData = msg);
      _changeBody(String msg) => setState(() => notificationBody = msg);
      _changeTitle(String msg) => setState(() => notificationTitle = msg);
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Text(
                  notificationTitle,
                  style: Theme.of(context).textTheme.headline4,
                ),
                Text(
                  notificationBody,
                  style: Theme.of(context).textTheme.headline6,
                ),
                Text(
                  notificationData,
                  style: Theme.of(context).textTheme.headline6,
                ),
              ],
            ),
          ),
        );
      }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM