iOS push notifications not working in flutter

2,760

I also have the same issue with firebase push notification in ios. My android push notification was working properly but not in ios.

What I did was to follow the instruction provided by firebase to integrate Push notification(Not the flutter guide but the ios one) Follow this link https://firebase.google.com/docs/cloud-messaging/ios/client

In ios user have to give permission to send push notification for them unlike android so the thing you want to do is add the below code to AppDelegate.swift

if #available(iOS 10.0, *) {
  // For iOS 10 display notification (sent via APNS)
  UNUserNotificationCenter.current().delegate = self
  let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
  UNUserNotificationCenter.current().requestAuthorization(
    options: authOptions,
    completionHandler: { _, _ in }
  )
} else {
  let settings: UIUserNotificationSettings =
    UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
  application.registerUserNotificationSettings(settings)
}
application.registerForRemoteNotifications()

Hope this works

Share:
2,760
Merlí Escarpenter Pérez
Author by

Merlí Escarpenter Pérez

My studies and titles: Professional Formation : DMA (Developing Multiplatform Apps) My develop experiencies: C# Developer Windows Phone 8.x Developer Android Developer

Updated on December 29, 2022

Comments

  • Merlí Escarpenter Pérez
    Merlí Escarpenter Pérez over 1 year

    My app needs push notifications. I just follow the flutterfire guide, with android working correctly but not for iOS app... I consider the error are in configuration of notifications, because the code never execute print("PUSH RECEIVED"); inside FirebaseMessaging.onMessage.listen() or FirebaseMessaging.onBackgroundMessage().

    My flutter doctor -v:

    [√] Flutter (Channel dev, 1.27.0-4.0.pre, on Microsoft Windows [Version 10.0.19042.985], locale es-ES)
        • Flutter version 1.27.0-4.0.pre at C:\SDKs\flutter
        • Framework revision f8cd24de95 (3 months ago), 2021-02-16 11:24:17 -0800
        • Engine revision 1d537824d6
        • Dart version 2.13.0 (build 2.13.0-30.0.dev)
    
    [√] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
        • Android SDK at C:\Users\Windows\AppData\Local\Android\sdk
        • Platform android-30, build-tools 30.0.3
        • Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java
        • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01)
        • All Android licenses accepted.
    
    [√] Chrome - develop for the web
        • Chrome at C:\Program Files\Google\Chrome\Application\chrome.exe
    
    [√] Android Studio (version 4.1.0)
        • Android Studio at C:\Program Files\Android\Android Studio
        • Flutter plugin can be installed from:
           https://plugins.jetbrains.com/plugin/9212-flutter
        • Dart plugin can be installed from:
           https://plugins.jetbrains.com/plugin/6351-dart
        • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01)
    
    [√] Connected device (3 available)
        • SM G986B (mobile) • ... • android-arm64  • Android 11 (API 30)
        • Chrome (web)      • chrome      • web-javascript • Google Chrome 90.0.4430.212
        • Edge (web)        • edge        • web-javascript • Microsoft Edge 90.0.818.62
    
    • No issues found!
    

    pubspec.yaml:

    #https://firebase.google.com/docs/flutter/setup?platform=android
    firebase_core: ^1.2.0
    firebase_analytics: ^8.1.0
    firebase_messaging: ^10.0.0
    #https://dev/packages/flutter_local_notifications/install
    flutter_local_notifications: ^5.0.0+4
    

    Main.dart:

    void main() async {
      ...
    
      await Firebase.initializeApp();
      bFirebaseMessaging.init();
    
      ...
    
      runApp(MyApp());
    }
    
    ...
    
    class _MyAppState extends State<MyApp> {
    
      ...
    
      static Future<void> _throwGetMessage(RemoteMessage message) async {
        print("PUSH RECEIVED");
        await Firebase.initializeApp();
        bFirebaseMessaging.showPushFromBackground(message);
      }
    
      @override
      Widget build(BuildContext context) {
        ...
    
        FirebaseMessaging.onMessage.listen((RemoteMessage message) {
          print("PUSH RECEIVED");
          bFirebaseMessaging.showPush(message);
        });
    
        FirebaseMessaging.onBackgroundMessage(_throwGetMessage);
    
        ...
      }
    
      ...
    }
    

    PodFile:

    # Uncomment this line to define a global platform for your project
    platform :ios, '12.0'
    $FirebaseSDKVersion = '8.0.0'
    
    ...
    
    target 'Runner' do
      use_frameworks!
      use_modular_headers!
    
      flutter_install_all_ios_pods File.dirname(File.realpath(__FILE__))
      pod 'FirebaseFirestore', :git => 'https://github.com/invertase/firestore-ios-sdk-frameworks.git', :tag => '8.0.0'
    end
    
    ...
    

    bFirebaseMessaging.dart:

    class bFirebaseMessaging {
    
      ...
    
      static Future init() async {
    
        // Declaration of variables
        FirebaseMessaging firebaseMessaging = FirebaseMessaging.instance;
        NotificationSettings settings = await firebaseMessaging.requestPermission(
          alert: true,
          announcement: false,
          badge: true,
          carPlay: false,
          criticalAlert: false,
          provisional: false,
          sound: true,
        );
    
        print('User granted permission: ${settings.authorizationStatus}');
    
        if(Platform.isIOS){
          await firebaseMessaging.setForegroundNotificationPresentationOptions(
            alert: true, // Required to display a heads up notification
            badge: true,
            sound: true,
          );
        }
      }
    
      ...
    
    }
    

    In the other hand I configured whole Firebase:

    • Created new iOS app
    • Added Team ID and Appstore ID
    • Added APNS autentication key with Key ID and Team ID

    Extra files: GoogleService-Info.plist, Signing & Capabilities

    Any ideas the reason that iOS devices not receiving puhs notifications? I think I gives all necessary information if you need something, tell me! Thanks in advance!

  • Prince Hodonou
    Prince Hodonou over 2 years
    helpful link, didn't find it in the docs