skip to Main Content

Flutter, Having some issues in Firebase notification receiving in iOS 17 versions.
While the app is opened Notification is received properly.
But when I put the app in the background or terminate it means the Notification is not received.
But In iOS 15.8.1 receiving the notifications properly.
Is there any issue with iOS 17?
Does anyone face this issue?

I’ve tried adding these functions in Appdelegate.swift, but still faced the issue…

 func function(application: UIApplication,
                 didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    Messaging.messaging().apnsToken = deviceToken
}

Following this thread, I’ve come to know that there was an issue with iOS 17 beta versions. Is this the actual problem or something else? Kindly guide me through…
Thanks in advance

2

Answers


  1. Chosen as BEST ANSWER

    Used the Push Message method for conversion of date time based on the user's timezone. I have fixed this issue by changing the Push Message to the Push Notification method. While using the Push message method it's hard to show the notification while the app is in a Background or Killed state. So Using the Push Notification method is the best way for notification. After that, I faced the issue of notification issue in iOS. For that, in info.plist file change the FirebaseAppDelegateProxyEnabled = NO into FirebaseAppDelegateProxyEnabled = YES.


  2. In iOS, when an app is terminated (i.e., not running in the background), it cannot receive notifications directly. However, you can still handle notifications that were delivered while the app was running in the background or terminated by using the userNotificationCenter(_:didReceive:withCompletionHandler:) method of the UNUserNotificationCenterDelegate.

    Here’s how you can handle notifications in Flutter when the app is terminated in iOS:

    Set up the UNUserNotificationCenterDelegate in your AppDelegate.swift file:

    import UIKit
    import Flutter
    import UserNotifications
    
    @UIApplicationMain
    @objc class AppDelegate: FlutterAppDelegate, UNUserNotificationCenterDelegate {
        override func application(
            _ application: UIApplication,
            didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
        ) -> Bool {
            GeneratedPluginRegistrant.register(with: self)
    
            // Set UNUserNotificationCenterDelegate
            UNUserNotificationCenter.current().delegate = self
    
            return super.application(application, didFinishLaunchingWithOptions: launchOptions)
        }
    
        // Handle notification when app is in foreground, background, or terminated
        func userNotificationCenter(
            _ center: UNUserNotificationCenter,
            didReceive response: UNNotificationResponse,
            withCompletionHandler completionHandler: @escaping () -> Void
        ) {
            // Handle notification here
            completionHandler()
        }
    }
    

    2.Implement the handle method in your main.dart file to handle notifications received when the app is in the background or terminated:

    import 'package:flutter/material.dart';
    import 'package:flutter/services.dart';
    import 'package:flutter_local_notifications/flutter_local_notifications.dart';
    import 'package:firebase_messaging/firebase_messaging.dart';
    
    final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
        FlutterLocalNotificationsPlugin();
    
    Future<void> main() async {
      WidgetsFlutterBinding.ensureInitialized();
      await initializeFlutterLocalNotificationsPlugin();
    
      runApp(MyApp());
    }
    
    Future<void> initializeFlutterLocalNotificationsPlugin() async {
      const AndroidInitializationSettings initializationSettingsAndroid =
          AndroidInitializationSettings('@mipmap/ic_launcher');
      final InitializationSettings initializationSettings =
          InitializationSettings(android: initializationSettingsAndroid);
      await flutterLocalNotificationsPlugin.initialize(initializationSettings);
    }
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Flutter Demo',
          home: MyHomePage(),
        );
      }
    }
    
    class MyHomePage extends StatefulWidget {
      @override
      _MyHomePageState createState() => _MyHomePageState();
    }
    
    class _MyHomePageState extends State<MyHomePage> {
      final FirebaseMessaging _firebaseMessaging = FirebaseMessaging.instance;
    
      @override
      void initState() {
        super.initState();
        _firebaseMessaging.requestPermission();
        _firebaseMessaging.getToken().then((token) {
          print('FCM Token: $token');
        });
    
        FirebaseMessaging.onMessage.listen((RemoteMessage message) {
          print("onMessage: $message");
          // Handle notification received while app is in foreground
        });
    
        FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
          print("onMessageOpenedApp: $message");
          // Handle notification tapped when app is in background or terminated
        });
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text('Flutter Demo'),
          ),
          body: Center(
            child: Text(
              'Push Notifications',
            ),
          ),
        );
      }
    }
    

    With this setup, you can handle notifications received while the app is running in the background or terminated. When the app is terminated, the userNotificationCenter(_:didReceive:withCompletionHandler:) method of the UNUserNotificationCenterDelegate will be called, and you can handle the notification accordingly.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search