简体   繁体   中英

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();
  }

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.

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),
      );
    }

我遇到了同样的问题,我使用https://pub.dev/packages/flutter_local_notifications插件解决了它,或者确保您正确调用 Flushbar API

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM