FCM 将消息从 php 复制到 iOS Flutter 应用程序

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


我尝试使用 php 代码触发推送通知。它给了我一个成功:1 个结果,但在我的设备上,我总是收到 2 条标题和正文相同但消息 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"    



猛跑小猪
浏览 124回答 3
3回答

慕哥6287543

目前这似乎是一个颤振问题。作为现在的解决方法,您可以创建一个布尔值并像这样触发它:class _SomethScreenState extends State<SomeScreen> {&nbsp; bool onNotificationTrigger = false;..._fcm.configure(&nbsp; &nbsp; &nbsp; onMessage: (Map<String, dynamic> response) async {&nbsp; &nbsp; &nbsp; &nbsp; setState(() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; onNotificationTrigger = !onNotificationTrigger;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (onNotificationTrigger)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Do Something&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; },...

波斯汪

对我来说,升级到:颤振版本 1.17.5Firebase_messaging:^6.0.16

忽然笑

我有一个类似的问题,但它是在 React-native 中。也许它可以帮助你。问题是有两个服务负责在 AndroidManifest 中接收通知\消息。
打开App,查看更多内容
随时随地看视频慕课网APP