我将 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"
3 回答
慕哥6287543
TA贡献1831条经验 获得超10个赞
目前这似乎是一个颤振问题。作为现在的解决方法,您可以创建一个布尔值并像这样触发它:
class _SomethScreenState extends State<SomeScreen> {
bool onNotificationTrigger = false;
...
_fcm.configure(
onMessage: (Map<String, dynamic> response) async {
setState(() {
onNotificationTrigger = !onNotificationTrigger;
if (onNotificationTrigger)
// Do Something
});
},
...
- 3 回答
- 0 关注
- 123 浏览
添加回答
举报
0/150
提交
取消