flutterfirebasefirebase-notificationsflutter-notification

Cannot receive the notificaion from firebase when I use flutter to developing on iOS


Cannot receive the notificaion from firebase when I use flutter to developing on iOS

I'd granted permission and used postman to test.

First, I'd get token

Restarted application in 516ms.
flutter: User granted permission
flutter: This token is eWQJLcN1zEQ_rQ79sBqXI6:APA91bHMnuhkPaf8JpcTbLQavxqq5erUIeRTQYPv9NAhsdfasdfasdaDSA0BTXgVb2RseAmUsi5uAa5SDvGWA7wgIcWPP5xDqTyo4s3xEdWkhHoQc77G59GGZptafaxNIW5QG

Secode: when I go to postman to test, I got the success

{
    "multicast_id": 2305740741419558081,
    "success": 1,
    "failure": 0,
    "canonical_ids": 0,
    "results": [
        {
            "message_id": "0:1654957148235526%2734e82c2734e82c"
        }
    ]
}

But I still could not got any notification.

Where has issues?

This is code.

late int _totalNotifications;
  late final FirebaseMessaging _messaging;
  PushNotification? _notificationInfo;

  void requestAndRegisterNotification() async {
    await Firebase.initializeApp();

    _messaging = FirebaseMessaging.instance;
    FirebaseMessaging.onBackgroundMessage(_firebaseMessageingBackgroundHandler);

    NotificationSettings settings = await _messaging.requestPermission(
      alert: true,
      badge: true,
      provisional: false,
      sound: true,
    );

    if (settings.authorizationStatus == AuthorizationStatus.authorized) {
      print('User granted permission');
      String? token = await _messaging.getToken();
      print('This token is ' + token!);

      FirebaseMessaging.onMessage.listen(
        (RemoteMessage message) {
          PushNotification notification = PushNotification(
            title: message.notification?.title,
            body: message.notification?.body,
          );
          setState(() {
            _notificationInfo = notification;
            _totalNotifications++;
          });
          if (_notificationInfo != null) {
            showSimpleNotification(
              Text(_notificationInfo!.title!),
              leading:
                  NotificationBadge(totalNotification: _totalNotifications),
              subtitle: Text(_notificationInfo!.body!),
              background: Colors.cyan.shade700,
              duration: Duration(seconds: 2),
            );
          }
        },
      );
    } else {
      print('User decliend or has not appected permission');
    }
  }

  @override
  void initState() {
    super.initState();
    requestAndRegisterNotification();
    _totalNotifications = 0;
  }

Solution

  • If you are using iOS simulator, it is unable to recieve push notifications. You need to have a real device to test this.