Flutterfire: [firebase_messaging] onResume fires when notification arrive not on click

Created on 3 Jan 2020  Â·  14Comments  Â·  Source: FirebaseExtended/flutterfire

Hello everyone, I installed firebase_messaging on my flutter app following the official guide.
Everything Is working as expected but when notification arrives on iOS they fire the onResume function, instead I would like to fire onResume when user click notification.

This is the code where I initialize the plugin


code

Future<void> init(User loggedUser) async {
    _loggedUser = loggedUser;

    if (Platform.isIOS) { await askIOSPermissions(); }

    _firebaseMessaging.configure(
      onMessage: _handleNotificationReceived,
      onLaunch: _handleNotificationOpened,
      onResume: _handleNotificationOpened
    );

    _tokenSubscription = _firebaseMessaging.onTokenRefresh.listen((token){
      if (token != null) _storeToken(token);
    });
  }

Nothing special here I followed the official guide.

in the onMessage callback I'm updating app badge accordingly


code

Future<void> _handleNotificationReceived(Map<String, dynamic> message) async {

    final data = message['data'] ?? message;

    if(data != null) {
      if(data['type'] == 'message'){
        _messageCount = _messageCount + 1;
        notifyListeners();
      }
      else {
        _notificationCount = _notificationCount + 1;
        notifyListeners();
      }
    }
  }

in the onLaunch and in onResume callback I'm navigating user to the correct screen


code

Future<void> _handleNotificationOpened(Map<String, dynamic> message) {

    final dynamic navigationBar = bottomNavigationKey.currentWidget;

    final data = message['data'] ?? message; 
    if (data != null) {
      switch (data['type']) {
        case 'message': 
          navigationBar.onTap(TabsPage.PROFILE_PAGE_INDEX); 
          navigatorKey.currentState.pushNamed(ConversationsPage.route);
          break;
        case 'post': 
          final int postId = int.parse(data['id']);
          if (postId != null) {
            navigatorKey.currentState.pushNamed(PostPage.route, arguments: PostPageArguments(postId: postId, pageTitle: 'Big Art Wall'));
          }
          break;
        case 'user': 
          final int userId = int.parse(data['id']);
          if (userId != null) {
            navigatorKey.currentState.pushNamed(ProfilePage.route, arguments: ProfilePageArguments(userId: userId));
          }
          break;
        default: navigationBar.onTap(TabsPage.NOTIFICATION_PAGE_INDEX);
      }
    }
    else {
      navigationBar.onTap(TabsPage.NOTIFICATION_PAGE_INDEX);
    }
  }

The problem is that the onResume is called every time a notification arrive, so when I open the app again I need to pop a lot of screen.
I could call popToRoot before push again in the onResume callback but in the official guide it is written that onResume is fired only when the notification is currently clicked.

Thank you,
Fabrizio

flutter doctor -v

[✓] Flutter (Channel beta, v1.12.13+hotfix.6, on Mac OS X 10.15.1 19B88, locale
    it-IT)
    • Flutter version 1.12.13+hotfix.6 at
      /Users/fabriziotognetto/Library/flutter
    • Framework revision 18cd7a3601 (3 weeks ago), 2019-12-11 06:35:39 -0800
    • Engine revision 2994f7e1e6
    • Dart version 2.7.0


[✓] Android toolchain - develop for Android devices (Android SDK version 28.0.3)
    • Android SDK at /Users/fabriziotognetto/Library/Android/sdk
    • Android NDK location not configured (optional; useful for native profiling
      support)
    • Platform android-28, build-tools 28.0.3
    • ANDROID_HOME = /Users/fabriziotognetto/Library/Android/sdk
    • ANDROID_SDK_ROOT = /Users/fabriziotognetto/Library/Android/sdk
    • Java binary at: /Applications/Android
      Studio.app/Contents/jre/jdk/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build
      1.8.0_202-release-1483-b49-5587405)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 11.3)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Xcode 11.3, Build version 11C29
    • CocoaPods version 1.8.4

[✓] Android Studio (version 3.5)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin version 40.0.2
    • Dart plugin version 191.8423
    • Java version OpenJDK Runtime Environment (build
      1.8.0_202-release-1483-b49-5587405)

[✓] IntelliJ IDEA Ultimate Edition (version 2018.1.4)
    • IntelliJ at /Applications/IntelliJ IDEA.app
    • Flutter plugin version 31.3.1
    • Dart plugin version 181.4892.1

[✓] VS Code (version 1.41.1)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.7.1

[✓] Connected device (2 available)
    • iPhone   • 279cd1e557ee696c72364c8b7a37b7a4a1be0098 • ios • iOS 13.2.2
    • iPhone 8 • DDBEA521-590C-463B-800D-33023EE7DB18     • ios •
      com.apple.CoreSimulator.SimRuntime.iOS-13-3 (simulator)

• No issues found!
customer messaging bug documentation

Most helpful comment

This is happening only on iOS, on Android it works as expected

All 14 comments

This is happening only on iOS, on Android it works as expected

I noticed that removing

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

in ios/Runner/AppDelegate.swift it starts to fire onLaunch / onResume callback when I click notification.

Anyway the problem persist because the callback is fired also when the notification arrives (without clicking)

UPDATE

i was sending notification with content-available: 1
Removing that everything works as expected. Notification arrives (no callback) and when I click notification callback fires.

I confirm that this is working because I removed

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

from ios/Runner/AppDelegate.swift

I leave this issue opened because this is written in the official guide here

@quantosapplications so are you saying that the code above is unnecessary or are there any implications/side effects when removing it?

In my case adding that code to the AppDelegate the onResume and onLaunch callbacks does not fire when I click on notification.
Removing it the callbacks works as expected

So there are no side effects when removing the block?

@jbxbergdev i’ve tested with flutter beta on iOS 13 and I didn’t found side effects. Everything working as expected..

@quantosapplications Thanks for sharing.
Could you share your notification push data structure?

https://github.com/FirebaseExtended/flutterfire/issues/1644
here many people suffering from bug, callback such as onMessage, onResume, onLaunch
not been called properly.

I've tried like you remove the lines. but not work in real devices.

Thanks in advance 😄

Hello @BansookNam, yes this is a typical notification structure I'm using

{ 
    tokens: [ 'USER_TOKEN_1', 'USER_TOKEN_2', ... ], 
    notification: { 
        body: 'BODY_OF_NOTIFICATION', 
        title: 'TITLE_OF_NOTIFICATION' 
    }, 
    data: { 
        notificationId: '123',
        ...OTHER_CUSTOM_DATA
    }, 
    android: { 
        notification: { 
            sound: 'default', 
            clickAction: 'FLUTTER_NOTIFICATION_CLICK', 
            notificationCount: 1, 
            imageUrl: 'https://url_of_image' 
        }, 
        priority: 'high' 
    }, 
    apns: { 
        payload: { 
            aps: {
                badge: NUMBER_OF_BADGE,
                mutableContent: true,
                sound: 'default'
            }
        }, 
        fcmOptions: { 
            imageUrl: 'https://url_of_image' 
        } 
    }
} 

I'm using a cloud function in node to send fcm push with firebase-admin.
Supposing to call message the object above:
const response = await admin.messaging().sendMulticast(message);

@quantosapplications Thanks for sharing!

My real problem was a conflict with plugin "flutter_local_notifications".
I had to remove it when Platform is IOS.

So there are no side effects when removing the block?

In my case, "flutter_local_notifications" stops working if I remove the snippet from ios/Runner/AppDelegate.swift

All the rest works just as @quantosapplications commented: removing "content_available" from the message payload and removing the snippet from ios/Runner/AppDelegate.swift makes onResume fire on notification click.

hey @quantosapplications or anyone, is it possible to somehow receive the background notification in onMessage so I can update the badge count even in background?

Hey all 👋

As part of our roadmap (#2582) we've just shipped a complete rework of the firebase_messaging plugin that aims to solve this and many other issues.

If you can, please try out the dev release (see the migration guide for upgrading and for changes) and if you have any feedback then join in the discussion here.

Given the scope of the rework I'm going to go ahead and close this issue in favor of trying out the latest plugin.

Thanks everyone.

Was this page helpful?
0 / 5 - 0 ratings