简体   繁体   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 ! 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 !

Please try this请试试这个

class name FCM 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();
      }
    }

And Main Class和主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.

相关问题 当我尝试以终止状态(当应用程序终止时)发送通知时,我的颤振 IOS 应用程序出现问题 - I have an issue in my flutter IOS app when i try to send notification in kill state(when App Terminated) notification is not coming 当应用程序在后台或终止时,推送通知未进入 IOS 设备。 在 Flutter 的 IOS 设备中导航到下一个屏幕也不起作用? - Push notification not coming in IOS device when app in background or kill. Navigate to next screen also not working In IOS device in Flutter? 通过推送通知Objective C iOS从“终止”状态打开时,应用崩溃 - App crash when open from Kill state through Push notification Objective C iOS 通过“推送通知”从“终止”状态打开应用程序时,iOS Web View App崩溃 - iOS Web View App crashed when Application is open from kill state through Push Notification 当应用未处于运行状态iOS时通过通知横幅打开应用 - Open app through notification banner when app is not running state iOS 当应用程序在8小时左右处于非活动状态时,不会发出voip通知 - voip notification not coming when the app is inactive for around 8 hours 推送通知横幅根本不会出现,即使应用程序在带有Swift 3.0的iOS中处于后台 - Push notification banner not at all coming even when app is in background in iOS with Swift 3.0 有时,当应用程序被ios中的用户杀死时,应用程序不提供iBeacon通知 - Sometimes App is not giving notification of iBeacon when app is kill by user in ios 处于“未运行”状态时在iOS应用中推送通知 - Push notification in iOS app when “Not running” state 当应用程序在 iOS 14 中终止或终止时,未收到 Voip 通知 - Voip notification did not receive when app kill or terminated in iOS 14
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM