AyadRocketfy
AyadRocketfy

Reputation: 116

Flutter iOS FCM notifications onMessage

I'm having a problem trying to show onMessage notification on iOS, Android is working correctly. I send notification and the onMessage is trigered but not showing the Flushbar at it does on the Android device. The strange thing that in the console it prints the onMessage but it's not showing the Flushbar on the App.

  @override
  void initState() {
    _firebaseMessaging.configure(
      onMessage: (Map<String, dynamic> message) async {
        print("onMessage: $message");
        int idEvent = int.parse(message['data']['idevent']);
        String image = message['data']['image'];

        Flushbar(
          title: message['notification']['title'],
          message: message['notification']['body'],
          duration: Duration(seconds: 5),
          flushbarPosition: FlushbarPosition.TOP,
          flushbarStyle: FlushbarStyle.GROUNDED,
          onTap: (Flushbar fb) async {
            fb.dismiss();

            ServiceEvent.fetchEvent(idEvent).then((event) {
              if (event != null) {
                Navigator.push(
                  context,
                  MaterialPageRoute(
                    builder: (context) => EventDetailsPage(
                      event,
                      fromNotification: true,
                    ),
                  ),
                );
              } else {
                DialogUtility.dialogInfo(
                    context, "There was an error didn't find event");
              }
            }).catchError((error) {
              DialogUtility.dialogInfo(context, error.toString());
            });
          },
          icon: image != null
              ? Padding(
                padding: const EdgeInsets.symmetric(horizontal: 5.0),
                child: Image.network('$image', height: 40, width: 40,),
              )
              : Padding(
                  padding: const EdgeInsets.symmetric(horizontal: 5),
                  child: SvgPicture.asset(
                    'assets/icons/tarjeta-rocket-entries.svg',
                    semanticsLabel: 'Acme Logo',
                    width: 40,
                    height: 40,
                  ),
                ),
        )..show(buildContext);
      },
      onLaunch: (Map<String, dynamic> message) async {
        print("onLaunch: $message");
      },
      onResume: (Map<String, dynamic> message) async {
        print("onResume: $message");
      },
    );
    _firebaseMessaging.requestNotificationPermissions(
        const IosNotificationSettings(
            sound: true, badge: true, alert: true, provisional: true));
    _firebaseMessaging.onIosSettingsRegistered
        .listen((IosNotificationSettings settings) {
      print("Settings registered: $settings");
    });
    _firebaseMessaging.getToken().then((String token) {
      assert(token != null);
      setState(() {
        //_homeScreenText = "Push Messaging token: $token";
      });
      //print('token: '+token);
    });
    super.initState();
  }

Upvotes: 0

Views: 1487

Answers (4)

ahmed taha
ahmed taha

Reputation: 7

i have fixed this by request permission while you initialize firebase masseging

if(Platform.isAndroid){
  flutterLocalNotificationsPlugin.resolvePlatformSpecificImplementation<
      AndroidFlutterLocalNotificationsPlugin>()!.requestNotificationsPermission();
}else {
  flutterLocalNotificationsPlugin.resolvePlatformSpecificImplementation<
      IOSFlutterLocalNotificationsPlugin>()!.requestPermissions();

    }

Upvotes: 1

Ilyas Arafath
Ilyas Arafath

Reputation: 681

Modify the upcoming json

onMessage: (Map<String, dynamic> message) async {
        print("onMessage");
        if (Platform.isIOS) {
          message = _modifyNotificationJson(message);
        }
);
 /// This method will modify the message format of iOS Notification Data
  Map _modifyNotificationJson(Map<String, dynamic> message) {
    message['data'] = Map.from(message ?? {});
    message['notification'] = message['aps']['alert'];
    return message;
  }

Upvotes: 0

Atiq Ur Rehman
Atiq Ur Rehman

Reputation: 1235

App local notification works for me like this

 setUpLocalNotification() async {
    //Local Notification Configuration--------
    var initializationSettingsAndroid = AndroidInitializationSettings('logo');
    var initializationSettingsIOS = IOSInitializationSettings();
    var initializationSettings = InitializationSettings(
        initializationSettingsAndroid, initializationSettingsIOS);
    await flutterLocalNotificationsPlugin.initialize(initializationSettings,
        onSelectNotification: onSelectNotification);

    //end local notification
  }

call the function in init state and then checked the os type and show notification.

 if (Platform.isIOS) {
      await flutterLocalNotificationsPlugin.show(
        0,
        message['aps']['alert']['title'],
        message['aps']['alert']['body'],
        platformChannelSpecifics,
        payload: JsonEncoder.withIndent("    ").convert(message),
      );
    }

Upvotes: 1

AyadRocketfy
AyadRocketfy

Reputation: 116

Just noticied the problem. The json is different from Android and iOS when receiving the onMessage notification and doesn't show any exception on the firebase onMessage.

Upvotes: 1

Related Questions