FCM 将消息从 php 复制到 iOS flutter app

FCM duplicated messages from php to iOS flutter app

我将 Firebase Cloud Message 用于 Flutter 应用程序。

我尝试使用 php 代码触发推送通知。它给了我一个成功:1 个结果,但在我的设备上我总是收到 2 条消息,标题相同&body但消息 ID 不同(由 google firebase 提供)。

我仔细检查过,我只在服务器端触发了一次这个函数。

我在服务器上的代码

$url = 'https://fcm.googleapis.com/fcm/send';
$msg = array
    (
          'body'  => $message,
          'title' => $title,
                  'badge' => 1,/*Default sound*/
                  'sound' => 'default',
    );
$fields = array
        (
            'registration_ids' => $id,
            'notification'  => $msg
        );

$fields = json_encode ( $fields );

$apiKey = 'XXXXXXX'; //IOS

$headers = array (
        'Authorization: key='.$apiKey,
        'Content-Type: application/json'
);

$ch = curl_init ();
curl_setopt ( $ch, CURLOPT_URL, $url );
curl_setopt ( $ch, CURLOPT_POST, true );
curl_setopt ( $ch, CURLOPT_HTTPHEADER, $headers );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $fields );

$result = curl_exec ( $ch );
curl_close ( $ch );
    echo $result;

我试图从终端触发这个,它工作正常,只发送了 1 条消息。我的终端代码:

DATA='{"notification": {"body": "this is a body","title": "this is a title"}, "priority": "high", "data": {"click_action": "FLUTTER_NOTIFICATION_CLICK", "id": "1", "status": "done"}, "to": "MYDEVICETOKEN"}'
curl https://fcm.googleapis.com/fcm/send -H "Content-Type:application/json" -X POST -d "$DATA" -H "Authorization: key=XXXXXXXXX"    

我有类似的问题,但它是在 React-native 中。也许它可以帮助你。问题是有两个服务负责在 AndroidManifest.

中接收 notifications\messages

目前这似乎是一个不稳定的问题。 作为目前的解决方法,您可以创建一个布尔值并像这样触发它:

class _SomethScreenState extends State<SomeScreen> {

  bool onNotificationTrigger = false;
...

_fcm.configure(
      onMessage: (Map<String, dynamic> response) async {
        setState(() {
          onNotificationTrigger = !onNotificationTrigger;

          if (onNotificationTrigger)
           // Do Something
        });
      },
...

对我来说,正在升级到:

  • Flutter 版本 1.17.5
  • Firebase_messaging: ^6.0.16