接收推送通知两次 firebase PHP 脚本
Posted
技术标签:
【中文标题】接收推送通知两次 firebase PHP 脚本【英文标题】:Receive Push Notification Twice firebase PHP Script 【发布时间】:2021-03-22 07:16:10 【问题描述】:我是 Firebase 发送推送通知的新手。我正在使用 php 脚本向多个用户发送推送通知。
PHP 脚本
function sendPushNotification($fb_key_array, $title)
$finalPostArray = array('registration_ids' => $fb_key_array,
'notification' => array(
'title' => $title,
'sound' => 'default',
'badge' => '1'
),
'priority' => 'high',
);
$server_key = env("SERVER_KEY");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://fcm.googleapis.com/fcm/send");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($finalPostArray)); //Post Fields
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Authorization: key=' . $server_key));
$server_output = curl_exec($ch);
print_r($server_output);
return $server_output;
curl_close($ch);
我在特定时间使用队列作业发送通知。 当我同时发送三个通知而不是通知接收两次时,我遇到了问题。 来自这个卷发的回应
App\Jobs\PushNotification
<pre>"multicast_id":4668247729883149905,"success":1,"failure":0,"canonical_ids":0,"results":["message_id":"0:1616386021446151%e609af1cf9fd7ecd"][2021-03-22 04:07:01][261] Processed: App\Jobs\PushNotification
[2021-03-22 04:07:01][262] Processing: App\Jobs\PushNotification
<pre>"multicast_id":4539206321688711164,"success":2,"failure":0,"canonical_ids":0,"results":["message_id":"0:1616386022124068%e609af1cf9fd7ecd","message_id":"0:1616386022123922%e609af1cf9fd7ecd"][2021-03-22 04:07:01][262] Processed: App\Jobs\PushNotification
[2021-03-22 04:07:02][263] Processing: App\Jobs\PushNotification
<pre>"multicast_id":1591082090876608595,"success":3,"failure":0,"canonical_ids":0,"results":["message_id":"0:1616386023195991%e609af1cf9fd7ecd","message_id":"0:1616386023196003%e609af1cf9fd7ecd","message_id":"0:1616386023195724%e609af1cf9fd7ecd"][2021-03-22 04:07:02][263] Processed: App\Jobs\PushNotification
并收到 6 条通知。但我只发送了 3 条通知。我的错在哪里?
帮忙谢谢
【问题讨论】:
你能显示你调用这个函数的代码吗?我认为它处于某种循环中? 感谢您的评论,不,我确定它不在任何循环中@Darren 【参考方案1】:我不太确定你的 func 有没有问题,但你可以试试这个,它是一样的,但如果不工作的话,差别不大,这意味着你的代码中某处有一个循环..
$url = 'https://fcm.googleapis.com/fcm/send';
$fields = array (
'registration_ids' => $target,
// fix new json format
'notification' => array(
'body' => 'this is firebase test mesging',
'title' => 'hello from firebase',
'badge' => 1,
'sound' => 'default',
'priority' => 'high',
)
);
$fields = json_encode($fields);
$server_key = 'ur_server_kay';
// inherited key
$headers = array(
'Content-Type:application/json',
'Authorization:key='.$server_key
);
$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_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
$result = curl_exec($ch);
var_dump($result);
if ($result === FALSE)
die('Oops! FCM Send Error: ' . curl_error($ch));
curl_close($ch);
【讨论】:
以上是关于接收推送通知两次 firebase PHP 脚本的主要内容,如果未能解决你的问题,请参考以下文章
php 简单的PHP FireBase(FCM)脚本,显示如何发送Android推送通知。请务必使用prope替换SERVER_API_ACCESS_KEY