Skip to content

[firebase_messaging] FCM messaging using Flutter does not work on iPhone, but it works fine on Android device #2885

Closed
@ghost

Description

I built the flutter live_chat app using plugin "firebase_messaging: ^5.1.8". The app uses topic notification for the live chatting. it works fine on android devices but does not work on iphones. if the sender sends the notification to ipone device ( or from cloud messaging in firebase project sends the notification), the receiver can see the push notification in his iphone device but the callback function in app to receive the notification does not work. I upgraded the version of "firebase-messaging" plugin from "^5.1.8" to "^6.0.9" but the issue is same.

The following is the main class for FCM messaging.

FCMManager() {
    _firebaseMessaging = FirebaseMessaging();


    _firebaseMessaging.configure(
      onMessage: (Map<String, dynamic> message) async {
      
          SoundManager.getInstance().playNewMessageSound();
      
          Map data;
      
          if(Platform.isIOS) {
            data = message;
          } else {
            data = message['data'] as Map;
          }
          
          if(data['type'] == 'notification') {
       
            DashboardScreen.getInstance().increaseUnreadNotificationCount(1);
       
            String notificationData = data['data'] as String;
            Map notificationJsonMap = json.decode(notificationData) as Map;
            SchoolNotification newNotification = SchoolNotification.fromJson(notificationJsonMap);
            if(_notificationReceivedCallback != null) {
              _notificationReceivedCallback(newNotification);
            }
          } else if (data['type'] == 'message') {
            String messageData = data['data'] as String;
            Map messageJsonMap = json.decode(messageData) as Map;
            Message newMessage = Message.fromJson(messageJsonMap);
        
            if(newMessage.senderId == SessionManager.getUserID()) return;
        
            if(_messageRecievedOnChatRoomCallback != null && SessionManager.getCurrentChatRoomID() == 
newMessage.classId) {
              _messageRecievedOnChatRoomCallback(newMessage);
            } else {
              DashboardScreen.getInstance().increaseUnreadMessageCount(1);
              if(_messageReceivedCallback != null) {
                _messageReceivedCallback(newMessage);
              }
            }
          } else if(data['type'] == 'school_status') {
            var _status = data['value'];
            int status = -1;

            if(_status is int) {
              status = _status;
            } else if(_status is String) {
              status = int.parse(_status);
            }
            if(status == -1) {
              return;
            }
            if(_activationCallback != null) {
              _activationCallback(status);
            }
          }
      },
    );
     _firebaseMessaging.requestNotificationPermissions(
        const IosNotificationSettings(
            sound: true, badge: true, alert: true));
    _firebaseMessaging.onIosSettingsRegistered
        .listen((IosNotificationSettings settings) {
      //print("Settings registered: $settings");
    });

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

  void subscribe({SubscribeCallback onSubscribe}) {
    if(onSubscribe != null) {
      onSubscribe(_firebaseMessaging);
    }
  }

  void unSubscribe({UnsubscribeCallback onUnsubscribe}) {
    if(onUnsubscribe != null) {
      onUnsubscribe(_firebaseMessaging);
    } 
  }

  void setTokenRefreshCallback(FCMTokenRefreshCallback callback) {
    this._onTokenRefresh = callback;
  }

  void setNotificationReceivedCallback(NotificationReceivedCallback callback) {
    this._notificationReceivedCallback = callback;
  }

  void setMessageReceivedCallback(MessageReceivedCallback callback) {
    this._messageReceivedCallback = callback;
  }

  void setChatRoomMessageReceivedCallback(MessageRecievedOnChatRoomCallback callback) {
    this._messageRecievedOnChatRoomCallback = callback;
  }

  void setActivationCallback(SchoolActivationCallback callback) {
    this._activationCallback = callback;
  }
}

Flutter doctor

MISs-Mac:myschoolapp_frontend mis$ flutter doctor -v 
[✓] Flutter (Channel stable, v1.12.13+hotfix.8, on Mac OS X 10.14.4 18E226, locale en-AF) • Flutter version 1.12.13+hotfix.8 at /Users/mis/Desktop/flutter • Framework revision 0b8abb4724 (4 months ago), 2020-02-11 11:44:36 -0800 • Engine revision e1e6ced81d • Dart version 2.7.0

[✗] Android toolchain - develop for Android devices ✗ Unable to locate Android SDK. Install Android Studio from: https://developer.android.com/studio/index.html On first launch it will assist you in installing the Android SDK components. (or visit https://flutter.dev/setup/#android-setup for detailed instructions). If the Android SDK has been installed to a custom location, set ANDROID_HOME to that location. You may also want to add it to your PATH environment variable.

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

[!] Android Studio (not installed) • Android Studio not found; download from https://developer.android.com/studio/index.html (or visit https://flutter.dev/setup/#android-setup for detailed instructions).

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

[!] Connected device ! No devices available

Please help me from this issue.

Best Regards

Metadata

Metadata

Assignees

No one assigned

    Labels

    StaleIssue with no recent activityblocked: customer-responseWaiting for customer response, e.g. more information was requested.closed-by-bot

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions