Flutterfire: [Firebase_messaging] : version 8.0.0-dev.7 unable to hit message handler functions.

Created on 7 Nov 2020  路  19Comments  路  Source: FirebaseExtended/flutterfire

Im unable to hit the message handle functions when sending a notification from Firebase cloud messaging console.

  1. Send message from firebase console.
  2. put print statements in message handler functions like onBackgroundMessage, onMessageOpenedApp and onMessage.listen

I was able to send notifications with the new version deployment (8.0.0-dev.7) in IOS . I can see the notification in IOS physical device but when I click the notification I dont see the print logs Im setting in the message handler functions.

Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
  print('message from background handler');
  print("Handling a background message: ${message.messageId}");
}`

@override
  void didChangeDependencies() async {
    if (!_isInit) {
      // FirebaseMessaging.onBackgroundMessage(
      //     _firebaseMessagingBackgroundHandler);
      // FirebaseMessaging messaging = FirebaseMessaging.instance;
      FirebaseMessaging.instance.getToken().then((value) {
        print(value);
      });

      FirebaseMessaging.onMessageOpenedApp.listen((event) {
        print('hey');
        print(event);
      });

      FirebaseMessaging.onBackgroundMessage(
          _firebaseMessagingBackgroundHandler);

      FirebaseMessaging.onMessage.listen((RemoteMessage message) {

        print('Got a message whilst in the foreground!');
        print('Message data: ${message.data}');

        if (message.notification != null) {
          print(
              'Message also contained a notification: ${message.notification}');
        }
      });

    }

    _isInit = true;
    super.didChangeDependencies();
  }
ios messaging bug

Most helpful comment

I have the above problems too with iOS real devices.

All 19 comments

I have the above problems too with iOS real devices.

Where have you defined the _firebaseMessagingBackgroundHandler - can you show me the content of the whole dart file where you've defined it. You need to define it outside any classes as a top-level/global function.

I can confirm that onMessage function is not triggered when app is foregrounded on iOS. On Android device I receive onMessage correctly

As onMessage is not Triggered as onMessageOpenedApp.

It's not really clear here what is not working,

Which one is not working, onMessage, onMessageOpenedApp or onBackgroundMessage?

Can you confirm you are also testing this with a real device, this will not work on an iOS simulator.

Can you also confirm APNs has been setup correctly in your project, iOS/macOS will not work at all without APNs - it must be configured.

Hi Salakar,
onMessage status according to my tests:

  • it doesn't work on iOS Simulator
  • it doesn't work on iOS device
  • it does work on Android
  • push notifications are configured properly

As a workaround, which is probably a better solution than the original, I've used FirebaseMessaging.instance.setForegroundNotificationPresentationOptions

Hi Salakar,
onMessage status according to my tests:

  • it doesn't work on iOS Simulator
  • it doesn't work on iOS device
  • it does work on Android
  • push notifications are configured properly

As a workaround, which is probably a better solution than the original, I've used FirebaseMessaging.instance.setForegroundNotificationPresentationOptions

  • it doesn't work on iOS Simulator

Correct - FCM will only ever work on a real device.

it doesn't work on iOS device

Could you explain specifically what doesn't work with onMessage? You don't get the event in Dart or it doesn't show notifications in the foreground.

  • push notifications are configured properly

Could you clarify what steps you've taken to configure, have you followed the APNs integration guide; https://firebase.flutter.dev/docs/messaging/apple-integration and how are you sending your messages.

@Muccy

I don't receive the Dart event.
I followed the guide, I'm sending the messages through Firebase Console. I'm really sure the implementation works because notifications are received by the device when app is backgrounded. What is more, I read a log printed (I assume) from the Flutter Fire plugin on VS Code console containing the message I've just sent.

@muccy What is more, I read a log printed (I assume) from the Flutter Fire plugin on VS Code console containing the message I've just sent.

Ah interesting, can you show me this log. Also can you show me how you've setup the onMessage code in your Dart code?

Not now because I'm quite busy, sorry. But I'll do it for sure as soon as I can.
Thank you very much

Not now because I'm quite busy, sorry. But I'll do it for sure as soon as I can.

Ok, thanks. Could you also let me know the version of iOS your device is running when you can also.

@Salakar My apologies for the late response.

Im using a reald device ( IOS 14.1) I have firebase console setup and Im able to send messages with firebase console and cloud functions. However, when my app in background, messages are not hitting onBackgroundMessage

Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async { print('message'); print("Handling a background message: ${message.messageId}"); }

_Outside my class_
_firebaseMessagingBackgroundHandler

Working for me

  • onMessage.
  • onMessageOpenedApp.

Not working

  • onBackgroundMessage.

flutter_local_notifications: ^3.0.1+2
firebase_messaging: ^8.0.0-dev.7

let me know if you need more information...

Sorry for lack of information. In my case:

Im testing on real devices:

Android:

  • Huawei P30 Pro

  • Android 10

  • FirebaseSDK 26.0.0

iOS:

  • IPhone X

  • iOS 14.2

  • FirebaseSDK 6.34.0

I was using previous version 7.0.3 in production, everything works well. After migrating to 8.0.0-dev.7 something goes wrong on iOS side. As I'am sending notification from Firebase I can see in my logs, that all that data is corect but functions which I pass to onMessage and onMessageOpenedApp wont be triggered.

I'll mention again that everything works on Android side. The problem only occurs when testing on iOS.
Notification will display only in case when application is in background but after click on notification application will come to foreground and onMessageOpenedApp isnt trigerred, as I said ealier i can see print data from my firebase console but onMessage functions or onMesssageOpenedApp won't be triggered.

Log print from console

2020-11-10 07:59:57.399924+0100 Media Creations[1059:481366] flutter: {messageId: 1604991596928874, data: {fcm_options: {image: https://media-creations.pl/assets_mobile/campaigns/DD_49.jpg}, image_url: https://media-creations.pl/assets_mobile/campaigns/DD_49.jpg, url: null, only_image: true}, sentTime: 1604991596, mutableContent: true, notification: {title: Tytu艂, apple: {}, body: Opis}}

Class with Notification

import 'package:Media_Creations/models/db_data.dart' show NotificationData;
import 'package:Media_Creations/models/strings_data.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/material.dart';

import './widgets/notification_full.dart';
import './widgets/notification_only_image.dart';

class PushNotificationManager {
  static final PushNotificationManager _instance = PushNotificationManager._();

  final FirebaseMessaging _firebaseMessaging = FirebaseMessaging.instance;

  PushNotificationManager._();

  factory PushNotificationManager() => _instance;

  bool _initialized = false;

  Future<dynamic> _buildDialog(Widget child, BuildContext context) async =>
      showDialog(
        context: context,
        useRootNavigator: true,
        useSafeArea: true,
        builder: (ctx) => child,
        barrierDismissible: true,
      );

  void _fetchMessage(RemoteMessage message, BuildContext context) async {
    if (message == null || message.notification == null) return;
    final Map<String, String> _data = message.data;
    if (_data.containsKey(NotificationData.keyOnlyImage))
      _buildDialog(
          NotificationOnlyImage(
            imageUrl: _data[NotificationData.keyImageUrl],
            innerUrl: _data[NotificationData.keyUrl],
          ),
          context);
    if (_data.containsKey(NotificationData.keyFullNotification))
      _buildDialog(
          NotificationFull(
            imageUrl: _data[NotificationData.keyImageUrl],
            url: _data[NotificationData.keyUrl],
            title: _data[NotificationData.keyTitle],
            description: _data[NotificationData.keyDescription],
            btnCloseText: _data[NotificationData.keyBtnCancel],
            btnOpenText: _data[NotificationData.keyBtnOk],
          ),
          context);
  }

  Future<void> init(BuildContext context) async {
    if (!_initialized) {
      await _firebaseMessaging.requestPermission();
      _firebaseMessaging
          .getInitialMessage()
          .then((message) => _fetchMessage(message, context));
      FirebaseMessaging.onMessage
          .listen((message) => _fetchMessage(message, context));
      FirebaseMessaging.onMessageOpenedApp
          .listen((message) => _fetchMessage(message, context));

      String token = await _firebaseMessaging.getToken();
      print("${Strings.firebaseToken}$token");

      _initialized = true;
    }
  }
}

Let me know if you need any additional information. @Salakar

I think I'd probably need to screenshare with someone as I still don't full understand where it's going wrong and I'd like to see the full environment/project setup. With FCM there's so many moving parts it's hard to get the complete picture down in a GitHub issue without going back and forth on replies forever 馃槄

Or if someone is able to add me to their project on git I can also look there (confidentially of course).

I'm using 8.0.0-dev.8 and it doesn't work on iOS, but on 7.0.3 everything was okay.

@Salakar I'm okay to schedule a zoom call and go through the project.

@Den-Ree dropped you an email

@Salakar replied, I'm planning to do testing on iOS 14.2.

@Salakar no luck, I don't receive any notifications on iOS 14.2, tested on a real device.

Was this page helpful?
0 / 5 - 0 ratings