簡體   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