How can I use only Local Notification and prevent the automatic popup using Firebase Messaging on Ios with Flutter


I’m trying to use Local Notification on Flutter with an Ios device, it works, but the notification popup is shown two times, because one is from my and one is showed automatically when a notification is catched.

In my case, I would like to show the notification only using Local Notifications, so I have to prevent showing the automatic notification. I’ve tried to send a notification without the “notification” object but only with a “data” object: in this case the automatic notification doesn’t appears, but the event is not catched from the method FirebaseMessaging.onMessage.listen((RemoteMessage message) async {...}), so also Local Notifications are not working.

On Flutter:

FirebaseMessaging.onMessage.listen((RemoteMessage message) async {

      await FirebaseMessaging.instance
          .setForegroundNotificationPresentationOptions(alert: false);

      FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =

      const IOSInitializationSettings initializationSettingsIOS =

      final InitializationSettings initializationSettings =
        iOS: initializationSettingsIOS,
      await flutterLocalNotificationsPlugin.initialize(
        onSelectNotification: (payload) async {

      Future<String> _downloadAndSaveFile(
          String url, String fileName) async {
        final Directory directory =
            await getApplicationDocumentsDirectory();
        final String filePath="${directory.path}/$fileName";
        final http.Response response = await http.get(Uri.parse(url));
        final File file = File(filePath);
        await file.writeAsBytes(response.bodyBytes);
        return filePath;

      final String bigPicturePath = await _downloadAndSaveFile(
['image'], 'bigPicture.jpg');
      final IOSNotificationDetails iOSPlatformChannelSpecifics =
          IOSNotificationDetails(attachments: <IOSNotificationAttachment>[
      NotificationDetails platformChannelSpecifics =
          NotificationDetails(iOS: iOSPlatformChannelSpecifics);
['body'], platformChannelSpecifics,
          payload: 'item x');

    print("Notification arrived");

On server (PHP):

 $fields = array(

         'notification'    => [
            'content_avaible' => true,
            'body'     => $message,
            'title'    => $title,
            'priority' => "high",
            "image" =>  'image' => ""
        "apns" => [

            "payload" => [
                "aps" => [
                    "mutable-content" => 1
            "fcm_options" => [
                 'image' => ""
        'registration_ids'    => $alltokens,
        'data' => [
            'image' => ""
    $headers = array(
        'Authorization: key=' . API_ACCESS_KEY_IOS,
        'Content-Type: application/json'
    $curlCall = curl_init();
    curl_setopt($curlCall, CURLOPT_URL, '');
    curl_setopt($curlCall, CURLOPT_POST, true);
    curl_setopt($curlCall, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($curlCall, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curlCall, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curlCall, CURLOPT_POSTFIELDS, json_encode($fields));
    $result = curl_exec($curlCall);


Source link

Leave a Reply

Your email address will not be published.