skip to Main Content

FCM Push Notification is not received when the Flutter app runs on IOS devices. Once the app goes in the background or is minimized, the Push notification works properly.

I have used firebase_messaging and flutter_local_notifications plugins for this.

  initialize() async {
   FirebaseMessaging messaging = FirebaseMessaging.instance;
   SharedPreferences sharedPreferences = await 
   SharedPreferences.getInstance();
   NotificationSettings settings = await messaging.requestPermission(
    alert: true,
    announcement: false,
    badge: true,
    carPlay: false,
    criticalAlert: false,
    provisional: false,
    sound: true,
  );

  if (settings.authorizationStatus == AuthorizationStatus.authorized) {
    print('User granted permission');
  } else if (settings.authorizationStatus ==
    AuthorizationStatus.provisional) {
      print('User granted provisional permission');
  } else {
    print('User declined or has not accepted permission');
  }

  messaging.getToken().then((token) {
    print("FCM TOKEN:- $token");
    sharedPreferences.setString("fcmToken", token.toString());
  });

  FirebaseMessaging.onMessage.listen((RemoteMessage event) {
    print("message received");
    print(event.notification!.body);
    flutterLocalNotificationsPlugin.show(
      1,
      event.notification!.title.toString(),
      event.notification!.body.toString(),
      NotificationDetails(
        android: AndroidNotificationDetails(
          channel.id,
          channel.name,
          color: Colors.amber,
          playSound: true,
          icon: '@mipmap/ic_launcher',
        ),
      ));
  });
  FirebaseMessaging.onMessageOpenedApp.listen((message) {
    print('Message clicked!');
  });
}

Can anyone please help me out?

2

Answers


  1. When you are listen notification in onMessage,You have added only android specification. please add IOS specification as well.

    var iOSPlatformChannelSpecifics = const DarwinNotificationDetails(
    presentBadge: true,
    presentAlert: true,
    presentSound: true,
    interruptionLevel: InterruptionLevel.active);
    
    Login or Signup to reply.
  2. If you mean that you are not receiving while the app in foreground you need to add this

    await FirebaseMessaging.instance.setForegroundNotificationPresentationOptions(
          alert: true, // Required to display a heads up notification
          badge: true,
          sound: true,
        );
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search