我如何仅使用本地通知并防止使用Firebase Messaging在iOS上使用Flutter上的Firebase Messaging进行自动弹出式

发布于 2025-01-21 08:02:34 字数 3544 浏览 4 评论 0原文

我正在尝试使用iOS设备上的颤音上的本地通知,它可以正常工作,但是该通知弹出窗口两次显示,因为一个来自我的flutterlocalnotificationplugin.show(...),一个是捕获通知时自动显示。

就我而言,我想仅使用局部通知显示通知,因此我必须防止显示自动通知。我试图在没有“通知”对象的情况下发送通知,而只使用“数据”对象发送:在这种情况下,自动通知不会出现,但是未从方法firebasemessaging.onmessage中捕获事件。 listan((EmeloteMessage Message)异步{...}),因此本地通知也无法正常工作。

在颤音上:

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


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

      FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
          FlutterLocalNotificationsPlugin();

      const IOSInitializationSettings initializationSettingsIOS =
          IOSInitializationSettings();

      final InitializationSettings initializationSettings =
          InitializationSettings(
        iOS: initializationSettingsIOS,
      );
      await flutterLocalNotificationsPlugin.initialize(
        initializationSettings,
        onSelectNotification: (payload) async {
          print("selected");
        },
      );

      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(
          message.data['image'], 'bigPicture.jpg');
      final IOSNotificationDetails iOSPlatformChannelSpecifics =
          IOSNotificationDetails(attachments: <IOSNotificationAttachment>[
        IOSNotificationAttachment(bigPicturePath)
      ]);
      NotificationDetails platformChannelSpecifics =
          NotificationDetails(iOS: iOSPlatformChannelSpecifics);
      await flutterLocalNotificationsPlugin.show(0, message.data['title'],
          message.data['body'], platformChannelSpecifics,
          payload: 'item x');
    }

    print("Notification arrived");
  });
}

在服务器上(PHP):

 $fields = array(

         'notification'    => [
            'content_avaible' => true,
            'body'     => $message,
            'title'    => $title,
            'priority' => "high",
            "image" =>  'image' => "https://foo.bar/image.png"
        ],
        "apns" => [

            "payload" => [
                "aps" => [
                    "mutable-content" => 1
                ]
            ],
            "fcm_options" => [
                 'image' => "https://foo.bar/image.png"
            ]
        ],
        'registration_ids'    => $alltokens,
        'data' => [
            'image' => "https://foo.bar/image.png"
        ],
    );
    $headers = array(
        'Authorization: key=' . API_ACCESS_KEY_IOS,
        'Content-Type: application/json'
    );
    $curlCall = curl_init();
    curl_setopt($curlCall, CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send');
    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);
    curl_close($curlCall);

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 flutterLocalNotificationsPlugin.show(...) 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 =
          FlutterLocalNotificationsPlugin();

      const IOSInitializationSettings initializationSettingsIOS =
          IOSInitializationSettings();

      final InitializationSettings initializationSettings =
          InitializationSettings(
        iOS: initializationSettingsIOS,
      );
      await flutterLocalNotificationsPlugin.initialize(
        initializationSettings,
        onSelectNotification: (payload) async {
          print("selected");
        },
      );

      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(
          message.data['image'], 'bigPicture.jpg');
      final IOSNotificationDetails iOSPlatformChannelSpecifics =
          IOSNotificationDetails(attachments: <IOSNotificationAttachment>[
        IOSNotificationAttachment(bigPicturePath)
      ]);
      NotificationDetails platformChannelSpecifics =
          NotificationDetails(iOS: iOSPlatformChannelSpecifics);
      await flutterLocalNotificationsPlugin.show(0, message.data['title'],
          message.data['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' => "https://foo.bar/image.png"
        ],
        "apns" => [

            "payload" => [
                "aps" => [
                    "mutable-content" => 1
                ]
            ],
            "fcm_options" => [
                 'image' => "https://foo.bar/image.png"
            ]
        ],
        'registration_ids'    => $alltokens,
        'data' => [
            'image' => "https://foo.bar/image.png"
        ],
    );
    $headers = array(
        'Authorization: key=' . API_ACCESS_KEY_IOS,
        'Content-Type: application/json'
    );
    $curlCall = curl_init();
    curl_setopt($curlCall, CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send');
    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);
    curl_close($curlCall);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文