Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
cgonzalezandrades opened this issue Nov 7, 2020 · 32 comments
Labels
platform: ios Issues / PRs which are specifically for iOS. plugin: messaging type: bug Something isn't working

Comments

@cgonzalezandrades
Copy link

cgonzalezandrades commented Nov 7, 2020

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();
  }
@Neil2847
Copy link

Neil2847 commented Nov 9, 2020

I have the above problems too with iOS real devices.

@Salakar
Copy link
Member

Salakar commented Nov 9, 2020

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.

@darshankawar darshankawar added the blocked: customer-response Waiting for customer response, e.g. more information was requested. label Nov 9, 2020
@muccy
Copy link
Contributor

muccy commented Nov 9, 2020

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

@PcolBP
Copy link

PcolBP commented Nov 9, 2020

As onMessage is not Triggered as onMessageOpenedApp.

@Salakar
Copy link
Member

Salakar commented Nov 9, 2020

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.

@muccy
Copy link
Contributor

muccy commented Nov 9, 2020

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

@Salakar
Copy link
Member

Salakar commented Nov 9, 2020

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

@muccy
Copy link
Contributor

muccy commented Nov 9, 2020

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.

@Salakar
Copy link
Member

Salakar commented Nov 9, 2020

@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?

@muccy
Copy link
Contributor

muccy commented Nov 9, 2020

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

@Salakar
Copy link
Member

Salakar commented Nov 9, 2020

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.

@cgonzalezandrades
Copy link
Author

@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...

@google-oss-bot google-oss-bot added Needs Attention This issue needs maintainer attention. and removed blocked: customer-response Waiting for customer response, e.g. more information was requested. labels Nov 9, 2020
@PcolBP
Copy link

PcolBP commented Nov 10, 2020

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

@Salakar
Copy link
Member

Salakar commented Nov 10, 2020

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).

@den-ree
Copy link

den-ree commented Nov 11, 2020

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

@darshankawar darshankawar added platform: ios Issues / PRs which are specifically for iOS. plugin: messaging type: bug Something isn't working and removed Needs Attention This issue needs maintainer attention. labels Nov 11, 2020
@darshankawar darshankawar changed the title version 8.0.0-dev.7 unable to hit message handler functions. [Firebase_messaging] : version 8.0.0-dev.7 unable to hit message handler functions. Nov 11, 2020
@den-ree
Copy link

den-ree commented Nov 12, 2020

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

@Salakar
Copy link
Member

Salakar commented Nov 12, 2020

@den-ree dropped you an email

@den-ree
Copy link

den-ree commented Nov 12, 2020

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

@den-ree
Copy link

den-ree commented Nov 16, 2020

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

@HugoHeneault
Copy link

Hey guys! Some time passed since last update, did you manage to get your notification through your app?

Thanks 🙏

@kuromukira
Copy link

kuromukira commented Jan 28, 2021

FirebaseMessaging.onMessage in my end does not work. Although I see a printed log of the notification I sent via FCM legacy HTTP and notification banner is shown.

This is on an iOS Device (14.4) - firebase_messaging 8.dev14

@PcolBP
Copy link

PcolBP commented Jan 28, 2021

FirebaseMessaging.onMessage in my end does not work. Although I see a printed log of the notification I sent via FCM legacy HTTP and notification banner is shown.

This is on an iOS Device (14.4) - firebase_messaging 8.dev14

Which FB SDK version do you use?
If you don't specify it look here.

@maares
Copy link

maares commented Feb 13, 2021

Hi just using the packages for the first time, after several hours, searching on this git issues, found that I was mistakenly sending the payload, thanks to #4300 :

Does the background callback work if you send a notification through FCM?

Such as

{
  "notification":{
    "title": "This is the title",
    "body": "This is the body",
  },
  "content_available": true,
  "priority": "high",
  "data": {
    "id": 15,
    "type": "comment"
  },
  "to": "FCM token"
}

From:

[
        "notification" => [
          "title" => $title,
          "body" => $message,
          "click_action" => "FLUTTER_NOTIFICATION_CLICK",
],

To:

"notification" => [
          "title" => $title,
          "body" => $message,
          // "click_action" => "FLUTTER_NOTIFICATION_CLICK",
        ],
        "priority": "high",

This was preventing handlers to get notify, and after changing them, complete example from repo started working!

Hope this helps any newcomer !

@santiotin
Copy link

I have the same issue using firebase_messaging: 9.0.0. When sending a notification I see through logs that is received (firebase messaging logs show: broadcast received for message). But the functions onMessage(foreground) or onMessageOpenedApp(background) are never executed. Someone knows why?

@atakangl
Copy link

I have the similar issue when upgrading iOS app.

I was using firebase_messaging: ^6.0.12 and i upgraded to firebase_messaging: ^9.0.0. In version 6.0.12 there is no foreground alert feature. I built foreground alert show with firebase local notification. Then I upgraded to 9.0.0 and added:

await FirebaseMessaging.instance
.setForegroundNotificationPresentationOptions(
alert: false,
badge: false,
sound: false,
);

There is no problem when I first installed or built to iOS device. But when I upgraded older version to last test version of my app in TestFlight setForegroundNotificationPresentationOptions is not working. Alert is showing twice. First firebase messaging default alert, second alert from my functions with firebase local notification. Any solution for this case?

@lucasalmeida-egsys
Copy link

I have the same issue using firebase_messaging: 9.0.0. When sending a notification I see through logs that is received (firebase messaging logs show: broadcast received for message). But the functions onMessage(foreground) or onMessageOpenedApp(background) are never executed. Someone knows why?

Same issue here.

@santiotin
Copy link

@lucasalmeida-egsys I resolved my issue navigating to RemoteMessage.fromMap() and changing contentAvailable: map['contentAvailable'], to contentAvailable: map['contentAvailable']??false and mutableContent: map['mutableContent'], to mutableContent: map['mutableContent']??false

@lucasalmeida-egsys
Copy link

@lucasalmeida-egsys I resolved my issue navigating to RemoteMessage.fromMap() and changing contentAvailable: map['contentAvailable'], to contentAvailable: map['contentAvailable']??false and mutableContent: map['mutableContent'], to mutableContent: map['mutableContent']??false

solved, thanks.

@juanektbb
Copy link

In my case, we can get background notifications on IOS real devices. But it does not invoke _firebaseMessagingBackgroundHandler which I use to do some tasks when the app receive a background notification.
(Android devices work perfect)

Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
  print("Background Notification");
}

mixin PushNotification<T extends StatefulWidget> on State<T>, DioField {
  static final _messaging = FirebaseMessaging.instance;

  @override
  initState() {
    super.initState();
    registerUpPushNotification();
    _listenToPushNotifications();
  }

  registerUpPushNotification() {
    //REGISTER REQUIRED FOR IOS
    if (Platform.isIOS) {
      _messaging.requestPermission();
    }

    _messaging.getToken().then((value) {
      if (value == null) return;
      print('token $value');
    });
  }

  _listenToPushNotifications() {
    FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);

    FirebaseMessaging.onMessage.listen((RemoteMessage message) {
      print("onMessage");
    });

    FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
      print('onMessageOpenedApp');
    });
  }
}

firebase_messaging 8.0.0-dev.12

@mario202k
Copy link

"7.3.0 - [Firebase/Messaging][I-FCM002022] APNS device token not set before retrieving FCM Token for Sender ID '708349719378'. Notifications to this FCM Token will not be delivered over APNS.Be sure to re-retrieve the FCM token once the APNS device token is set."
Nobody have this when running on Xcode? Is it related to "onIosSettingsRegistered" that is deprecated?

@mario202k
Copy link

"7.3.0 - [Firebase/Messaging][I-FCM002022] APNS device token not set before retrieving FCM Token for Sender ID '708349719378'. Notifications to this FCM Token will not be delivered over APNS.Be sure to re-retrieve the FCM token once the APNS device token is set."
Nobody have this when running on Xcode? Is it related to "onIosSettingsRegistered" that is deprecated?

Sorry a was forgetting await. Everything works on both side.

@russellwheatley
Copy link
Member

Hi guys, I'm going to close this issue for now as we're on firebase_messaging version 11.2.4 now. Please create a new issue if you feel there is a problem with the latest version. Thank you.

@firebase firebase locked and limited conversation to collaborators Jan 22, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
platform: ios Issues / PRs which are specifically for iOS. plugin: messaging type: bug Something isn't working
Projects
None yet
Development

No branches or pull requests