我將 Firebase Cloud Message 用于 Flutter 應用程序。我嘗試使用 php 代碼觸發(fā)推送通知。它給了我一個成功:1 個結(jié)果,但在我的設(shè)備上,我總是收到 2 條標題和正文相同但消息 ID 不同的消息(由 google firebase 提供)。我仔細檢查了一下,我只在服務器端觸發(fā)了這個功能一次。我在服務器上的代碼$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;我試圖從終端觸發(fā)它,它運行良好,并且只發(fā)送了 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"
3 回答

慕哥6287543
TA貢獻1831條經(jīng)驗 獲得超10個贊
目前這似乎是一個顫振問題。作為現(xiàn)在的解決方法,您可以創(chuàng)建一個布爾值并像這樣觸發(fā)它:
class _SomethScreenState extends State<SomeScreen> {
bool onNotificationTrigger = false;
...
_fcm.configure(
onMessage: (Map<String, dynamic> response) async {
setState(() {
onNotificationTrigger = !onNotificationTrigger;
if (onNotificationTrigger)
// Do Something
});
},
...

忽然笑
TA貢獻1806條經(jīng)驗 獲得超5個贊
我有一個類似的問題,但它是在 React-native 中。也許它可以幫助你。問題是有兩個服務負責在 AndroidManifest 中接收通知\消息。
- 3 回答
- 0 關(guān)注
- 159 瀏覽
添加回答
舉報
0/150
提交
取消