4

I am implemented Firebase Cloud Messaging for Flutter using this. all the things are working fine in Android, but iOS push notification only show when app is in foreground.

Please help me for show notification app in background and terminated.

Those are the steps I done so far

  1. Created firbase project.
  2. Add GoogleService-Info.plist to xcode.
  3. Add firebase_messaging: ^6.0.13 & flutter_local_notifications: ^1.3.0 into pubspec.yaml.
  4. Then I enable push notification and background modes

  5. In flutter

    FirebaseMessaging firebaseMessaging = new FirebaseMessaging();
    FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
    new FlutterLocalNotificationsPlugin();
    
    
    
    @override
    void initState() {
    super.initState();
    var android = new 
    AndroidInitializationSettings('mipmap/ic_launcher');
    var ios = new IOSInitializationSettings();
    var platform = new InitializationSettings(android, ios);
    flutterLocalNotificationsPlugin.initialize(platform);
    
    firebaseMessaging.configure(
    onLaunch: (Map<String, dynamic> msg) async{
    print(" onLaunch called ${(msg)}");
    },
    
    onResume: (Map<String, dynamic> msg) async{
    print(" onResume called ${(msg)}");
    },
    onMessage: (Map<String, dynamic> msg) async{
    showNotification(msg);
    print(" onMessage called ${(msg)}");
    },
    );
    firebaseMessaging.requestNotificationPermissions(
    const IosNotificationSettings(sound: true, alert: true, badge: 
    true));
    firebaseMessaging.onIosSettingsRegistered
    .listen((IosNotificationSettings setting) {
    print('IOS Setting Registed');
    });
    firebaseMessaging.getToken().then((token) {
    update(token);
    });
    getCashedInfo();
    }
    
    
    
    showNotification(Map<String, dynamic> msg) async {
    var android = new AndroidNotificationDetails(
    'sdffds dsffds',
    "CHANNLE NAME",
    "channelDescription",
    );
    var iOS = new IOSNotificationDetails();
    var platform = new NotificationDetails(android, iOS);
    await flutterLocalNotificationsPlugin.show(
     0, "This is title", "this is demo", platform);
    }
    
    update(String token) {
    print(token);
    
    }
    
  6. In Xcode I added

    if #available(iOS 10.0, *) {
      UNUserNotificationCenter.current().delegate = self as? 
    UNUserNotificationCenterDelegate
    }
    

1 Answers1

0

Upon posting this question, the feature of handling messages in background for iOS was previously not supported. But there are some workarounds mentioned in this GitHub post.

Using Notification Service Extension is one way to do it, yes.

Flutter is excellent framework that allows to build and ship beautiful UI in bizarre speed. However does this mean it provides everything we want (from our perspective)? No. Does this mean we have drop Flutter and use SwiftUI (buggy) or storyboards? No.

We made decision to use Flutter for UI and we are handling pretty much all business logic there except when it comes to push notifications and some other minor features as well - this is what you are better of rolling your own.

Implementing Notification Service Extension and making it work with Flutter is matter of a few hours and considering how many fail to do so is somewhat amusing but not shocking considering the entry level.

We did use Notification Service Extension with Notification Content Extension for fancier notifications for some months but at the end we dropped it and moved to PushKit (w/ CallKit obviously) and rolled our own notification presenter logic which resulted us dropping both Notification Service and Content Extension.

My advice for those who need to add push notifications support for Flutter - solve it on native side, you will thank yourself later.

How about using the NotificationServiceExtension (native code) with firebase_messaging?

And on the same GitHub post, specifically in this thread last August 4, 2020. It was mentioned that this feature is now supported.

Hey all, this is now supported in the current dev release, along with macOS platform support landing as well (See the migration guide doc for a full changelog and how to upgrade).

For discussions/feedback around trying out this dev release please see #4023 - would love feedback.

MαπμQμαπkγVπ.0
  • 5,887
  • 1
  • 27
  • 65
  • It's confusing to see `is not yet supported` and then a few lines below say `this feature is now supported.`. Like you said, it **is** supported, so the first sentence is not correct, and might confuse readers. You might want to say something like "was **previously** not supported". – Ben Butterworth Jun 28 '21 at 22:51
  • Thanks for raising, I will update my answer. – MαπμQμαπkγVπ.0 Jun 28 '21 at 22:53